< prev index next >

src/demo/share/jfc/TableExample/TableExample3.java

Print this page




  56  * JTable the ability to sort.
  57  *
  58  * @author Philip Milne
  59  */
  60 public class TableExample3 {
  61 
  62     public TableExample3() {
  63         JFrame frame = new JFrame("Table");
  64         frame.addWindowListener(new WindowAdapter() {
  65 
  66             @Override
  67             public void windowClosing(WindowEvent e) {
  68                 System.exit(0);
  69             }
  70         });
  71 
  72         // Take the dummy data from SwingSet.
  73         final String[] names = { "First Name", "Last Name", "Favorite Color",
  74             "Favorite Number", "Vegetarian" };
  75         final Object[][] data = {
  76             { "Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE },
  77             { "Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE },
  78             { "Alan", "Chung", "Green", new Integer(838), Boolean.FALSE },
  79             { "Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE },
  80             { "Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE },
  81             { "Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE },
  82             { "James", "Gosling", "Pink", new Integer(21), Boolean.FALSE },
  83             { "David", "Karlton", "Red", new Integer(1), Boolean.FALSE },
  84             { "Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE },
  85             { "Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE },
  86             { "Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE },
  87             { "Dave", "Moore", "Green", new Integer(88), Boolean.FALSE },
  88             { "Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE },
  89             { "Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE },
  90             { "Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE },
  91             { "Chester", "Rose", "Black", new Integer(0), Boolean.FALSE },
  92             { "Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE },
  93             { "Georges", "Saab", "Red", new Integer(4), Boolean.FALSE },
  94             { "Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE },
  95             { "Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE },
  96             { "Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE }
  97         };
  98 
  99         // Create a model of the data.
 100         @SuppressWarnings("serial")
 101         TableModel dataModel = new AbstractTableModel() {
 102             // These methods always need to be implemented.
 103 
 104             public int getColumnCount() {
 105                 return names.length;
 106             }
 107 
 108             public int getRowCount() {
 109                 return data.length;
 110             }
 111 
 112             public Object getValueAt(int row, int col) {
 113                 return data[row][col];
 114             }
 115 
 116             // The default implementations of these methods in
 117             // AbstractTableModel would work, but we can refine them.
 118             @Override
 119             public String getColumnName(int column) {
 120                 return names[column];
 121             }
 122 
 123             @Override
 124             public Class getColumnClass(int col) {
 125                 return getValueAt(0, col).getClass();
 126             }
 127 
 128             @Override
 129             public boolean isCellEditable(int row, int col) {
 130                 return (col == 4);
 131             }
 132 
 133             @Override
 134             public void setValueAt(Object aValue, int row, int column) {
 135                 data[row][column] = aValue;
 136             }
 137         };
 138 
 139         // Instead of making the table display the data as it would normally
 140         // with:
 141         // JTable tableView = new JTable(dataModel);
 142         // Add a sorter, by using the following three lines instead of the one
 143         // above.
 144         TableSorter sorter = new TableSorter(dataModel);




  56  * JTable the ability to sort.
  57  *
  58  * @author Philip Milne
  59  */
  60 public class TableExample3 {
  61 
  62     public TableExample3() {
  63         JFrame frame = new JFrame("Table");
  64         frame.addWindowListener(new WindowAdapter() {
  65 
  66             @Override
  67             public void windowClosing(WindowEvent e) {
  68                 System.exit(0);
  69             }
  70         });
  71 
  72         // Take the dummy data from SwingSet.
  73         final String[] names = { "First Name", "Last Name", "Favorite Color",
  74             "Favorite Number", "Vegetarian" };
  75         final Object[][] data = {
  76             { "Mark", "Andrews", "Red", Integer.valueOf(2), Boolean.TRUE },
  77             { "Tom", "Ball", "Blue", Integer.valueOf(99), Boolean.FALSE },
  78             { "Alan", "Chung", "Green", Integer.valueOf(838), Boolean.FALSE },
  79             { "Jeff", "Dinkins", "Turquois", Integer.valueOf(8), Boolean.TRUE },
  80             { "Amy", "Fowler", "Yellow", Integer.valueOf(3), Boolean.FALSE },
  81             { "Brian", "Gerhold", "Green", Integer.valueOf(0), Boolean.FALSE },
  82             { "James", "Gosling", "Pink", Integer.valueOf(21), Boolean.FALSE },
  83             { "David", "Karlton", "Red", Integer.valueOf(1), Boolean.FALSE },
  84             { "Dave", "Kloba", "Yellow", Integer.valueOf(14), Boolean.FALSE },
  85             { "Peter", "Korn", "Purple", Integer.valueOf(12), Boolean.FALSE },
  86             { "Phil", "Milne", "Purple", Integer.valueOf(3), Boolean.FALSE },
  87             { "Dave", "Moore", "Green", Integer.valueOf(88), Boolean.FALSE },
  88             { "Hans", "Muller", "Maroon", Integer.valueOf(5), Boolean.FALSE },
  89             { "Rick", "Levenson", "Blue", Integer.valueOf(2), Boolean.FALSE },
  90             { "Tim", "Prinzing", "Blue", Integer.valueOf(22), Boolean.FALSE },
  91             { "Chester", "Rose", "Black", Integer.valueOf(0), Boolean.FALSE },
  92             { "Ray", "Ryan", "Gray", Integer.valueOf(77), Boolean.FALSE },
  93             { "Georges", "Saab", "Red", Integer.valueOf(4), Boolean.FALSE },
  94             { "Willie", "Walker", "Phthalo Blue", Integer.valueOf(4), Boolean.FALSE },
  95             { "Kathy", "Walrath", "Blue", Integer.valueOf(8), Boolean.FALSE },
  96             { "Arnaud", "Weber", "Green", Integer.valueOf(44), Boolean.FALSE }
  97         };
  98 
  99         // Create a model of the data.
 100         @SuppressWarnings("serial")
 101         TableModel dataModel = new AbstractTableModel() {
 102             // These methods always need to be implemented.
 103 
 104             public int getColumnCount() {
 105                 return names.length;
 106             }
 107 
 108             public int getRowCount() {
 109                 return data.length;
 110             }
 111 
 112             public Object getValueAt(int row, int col) {
 113                 return data[row][col];
 114             }
 115 
 116             // The default implementations of these methods in
 117             // AbstractTableModel would work, but we can refine them.
 118             @Override
 119             public String getColumnName(int column) {
 120                 return names[column];
 121             }
 122 
 123             @Override
 124             public Class<?> getColumnClass(int col) {
 125                 return getValueAt(0, col).getClass();
 126             }
 127 
 128             @Override
 129             public boolean isCellEditable(int row, int col) {
 130                 return (col == 4);
 131             }
 132 
 133             @Override
 134             public void setValueAt(Object aValue, int row, int column) {
 135                 data[row][column] = aValue;
 136             }
 137         };
 138 
 139         // Instead of making the table display the data as it would normally
 140         // with:
 141         // JTable tableView = new JTable(dataModel);
 142         // Add a sorter, by using the following three lines instead of the one
 143         // above.
 144         TableSorter sorter = new TableSorter(dataModel);


< prev index next >