< prev index next >

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

Print this page




  58  * for some of the columns in the SwingSet demo table.
  59  *
  60  * @author Philip Milne
  61  */
  62 public class TableExample4 {
  63 
  64     public TableExample4() {
  65         JFrame frame = new JFrame("Table");
  66         frame.addWindowListener(new WindowAdapter() {
  67 
  68             @Override
  69             public void windowClosing(WindowEvent e) {
  70                 System.exit(0);
  71             }
  72         });
  73 
  74         // Take the dummy data from SwingSet.
  75         final String[] names = { "First Name", "Last Name", "Favorite Color",
  76             "Favorite Number", "Vegetarian" };
  77         final Object[][] data = {
  78             { "Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE },
  79             { "Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE },
  80             { "Alan", "Chung", "Green", new Integer(838), Boolean.FALSE },
  81             { "Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE },
  82             { "Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE },
  83             { "Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE },
  84             { "James", "Gosling", "Pink", new Integer(21), Boolean.FALSE },
  85             { "David", "Karlton", "Red", new Integer(1), Boolean.FALSE },
  86             { "Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE },
  87             { "Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE },
  88             { "Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE },
  89             { "Dave", "Moore", "Green", new Integer(88), Boolean.FALSE },
  90             { "Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE },
  91             { "Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE },
  92             { "Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE },
  93             { "Chester", "Rose", "Black", new Integer(0), Boolean.FALSE },
  94             { "Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE },
  95             { "Georges", "Saab", "Red", new Integer(4), Boolean.FALSE },
  96             { "Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE },
  97             { "Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE },
  98             { "Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE }
  99         };
 100 
 101         // Create a model of the data.
 102         @SuppressWarnings("serial")
 103         TableModel dataModel = new AbstractTableModel() {
 104             // These methods always need to be implemented.
 105 
 106             public int getColumnCount() {
 107                 return names.length;
 108             }
 109 
 110             public int getRowCount() {
 111                 return data.length;
 112             }
 113 
 114             public Object getValueAt(int row, int col) {
 115                 return data[row][col];
 116             }
 117 
 118             // The default implementations of these methods in
 119             // AbstractTableModel would work, but we can refine them.
 120             @Override
 121             public String getColumnName(int column) {
 122                 return names[column];
 123             }
 124 
 125             @Override
 126             public Class getColumnClass(int c) {
 127                 return getValueAt(0, c).getClass();
 128             }
 129 
 130             @Override
 131             public boolean isCellEditable(int row, int col) {
 132                 return true;
 133             }
 134 
 135             @Override
 136             public void setValueAt(Object aValue, int row, int column) {
 137                 System.out.println("Setting value to: " + aValue);
 138                 data[row][column] = aValue;
 139             }
 140         };
 141 
 142         // Create the table
 143         JTable tableView = new JTable(dataModel);
 144         // Turn off auto-resizing so that we can set column sizes
 145         // programmatically. In this mode, all columns will get their preferred
 146         // widths, as set blow.
 147         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 148 
 149         // Create a combo box to show that you can use one in a table.
 150         JComboBox comboBox = new JComboBox();
 151         comboBox.addItem("Red");
 152         comboBox.addItem("Orange");
 153         comboBox.addItem("Yellow");
 154         comboBox.addItem("Green");
 155         comboBox.addItem("Blue");
 156         comboBox.addItem("Indigo");
 157         comboBox.addItem("Violet");
 158 
 159         TableColumn colorColumn = tableView.getColumn("Favorite Color");
 160         // Use the combo box as the editor in the "Favorite Color" column.
 161         colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
 162 
 163         // Set a pink background and tooltip for the Color column renderer.
 164         DefaultTableCellRenderer colorColumnRenderer =
 165                 new DefaultTableCellRenderer();
 166         colorColumnRenderer.setBackground(Color.pink);
 167         colorColumnRenderer.setToolTipText("Click for combo box");
 168         colorColumn.setCellRenderer(colorColumnRenderer);
 169 
 170         // Set a tooltip for the header of the colors column.




  58  * for some of the columns in the SwingSet demo table.
  59  *
  60  * @author Philip Milne
  61  */
  62 public class TableExample4 {
  63 
  64     public TableExample4() {
  65         JFrame frame = new JFrame("Table");
  66         frame.addWindowListener(new WindowAdapter() {
  67 
  68             @Override
  69             public void windowClosing(WindowEvent e) {
  70                 System.exit(0);
  71             }
  72         });
  73 
  74         // Take the dummy data from SwingSet.
  75         final String[] names = { "First Name", "Last Name", "Favorite Color",
  76             "Favorite Number", "Vegetarian" };
  77         final Object[][] data = {
  78             { "Mark", "Andrews", "Red", Integer.valueOf(2), Boolean.TRUE },
  79             { "Tom", "Ball", "Blue", Integer.valueOf(99), Boolean.FALSE },
  80             { "Alan", "Chung", "Green", Integer.valueOf(838), Boolean.FALSE },
  81             { "Jeff", "Dinkins", "Turquois", Integer.valueOf(8), Boolean.TRUE },
  82             { "Amy", "Fowler", "Yellow", Integer.valueOf(3), Boolean.FALSE },
  83             { "Brian", "Gerhold", "Green", Integer.valueOf(0), Boolean.FALSE },
  84             { "James", "Gosling", "Pink", Integer.valueOf(21), Boolean.FALSE },
  85             { "David", "Karlton", "Red", Integer.valueOf(1), Boolean.FALSE },
  86             { "Dave", "Kloba", "Yellow", Integer.valueOf(14), Boolean.FALSE },
  87             { "Peter", "Korn", "Purple", Integer.valueOf(12), Boolean.FALSE },
  88             { "Phil", "Milne", "Purple", Integer.valueOf(3), Boolean.FALSE },
  89             { "Dave", "Moore", "Green", Integer.valueOf(88), Boolean.FALSE },
  90             { "Hans", "Muller", "Maroon", Integer.valueOf(5), Boolean.FALSE },
  91             { "Rick", "Levenson", "Blue", Integer.valueOf(2), Boolean.FALSE },
  92             { "Tim", "Prinzing", "Blue", Integer.valueOf(22), Boolean.FALSE },
  93             { "Chester", "Rose", "Black", Integer.valueOf(0), Boolean.FALSE },
  94             { "Ray", "Ryan", "Gray", Integer.valueOf(77), Boolean.FALSE },
  95             { "Georges", "Saab", "Red", Integer.valueOf(4), Boolean.FALSE },
  96             { "Willie", "Walker", "Phthalo Blue", Integer.valueOf(4), Boolean.FALSE },
  97             { "Kathy", "Walrath", "Blue", Integer.valueOf(8), Boolean.FALSE },
  98             { "Arnaud", "Weber", "Green", Integer.valueOf(44), Boolean.FALSE }
  99         };
 100 
 101         // Create a model of the data.
 102         @SuppressWarnings("serial")
 103         TableModel dataModel = new AbstractTableModel() {
 104             // These methods always need to be implemented.
 105 
 106             public int getColumnCount() {
 107                 return names.length;
 108             }
 109 
 110             public int getRowCount() {
 111                 return data.length;
 112             }
 113 
 114             public Object getValueAt(int row, int col) {
 115                 return data[row][col];
 116             }
 117 
 118             // The default implementations of these methods in
 119             // AbstractTableModel would work, but we can refine them.
 120             @Override
 121             public String getColumnName(int column) {
 122                 return names[column];
 123             }
 124 
 125             @Override
 126             public Class<?> getColumnClass(int c) {
 127                 return getValueAt(0, c).getClass();
 128             }
 129 
 130             @Override
 131             public boolean isCellEditable(int row, int col) {
 132                 return true;
 133             }
 134 
 135             @Override
 136             public void setValueAt(Object aValue, int row, int column) {
 137                 System.out.println("Setting value to: " + aValue);
 138                 data[row][column] = aValue;
 139             }
 140         };
 141 
 142         // Create the table
 143         JTable tableView = new JTable(dataModel);
 144         // Turn off auto-resizing so that we can set column sizes
 145         // programmatically. In this mode, all columns will get their preferred
 146         // widths, as set blow.
 147         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 148 
 149         // Create a combo box to show that you can use one in a table.
 150         JComboBox<String> comboBox = new JComboBox<>();
 151         comboBox.addItem("Red");
 152         comboBox.addItem("Orange");
 153         comboBox.addItem("Yellow");
 154         comboBox.addItem("Green");
 155         comboBox.addItem("Blue");
 156         comboBox.addItem("Indigo");
 157         comboBox.addItem("Violet");
 158 
 159         TableColumn colorColumn = tableView.getColumn("Favorite Color");
 160         // Use the combo box as the editor in the "Favorite Color" column.
 161         colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
 162 
 163         // Set a pink background and tooltip for the Color column renderer.
 164         DefaultTableCellRenderer colorColumnRenderer =
 165                 new DefaultTableCellRenderer();
 166         colorColumnRenderer.setBackground(Color.pink);
 167         colorColumnRenderer.setToolTipText("Click for combo box");
 168         colorColumn.setCellRenderer(colorColumnRenderer);
 169 
 170         // Set a tooltip for the header of the colors column.


< prev index next >