1/* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.awt.Color; 25import java.awt.Container; 26import java.awt.Dimension; 27 28import javax.swing.DefaultCellEditor; 29import javax.swing.JApplet; 30import javax.swing.JComboBox; 31import javax.swing.JLabel; 32import javax.swing.JScrollPane; 33import javax.swing.JTable; 34import javax.swing.SwingUtilities; 35import javax.swing.UIManager; 36import javax.swing.border.BevelBorder; 37import javax.swing.table.AbstractTableModel; 38import javax.swing.table.DefaultTableCellRenderer; 39import javax.swing.table.TableCellRenderer; 40import javax.swing.table.TableColumn; 41import javax.swing.table.TableModel; 42 43 44/** 45 * @test 46 * @bug 4112270 47 * @summary 48 * Keyboard Navigation in JTable 49 * @author milne 50 * @run applet/manual=yesno KeyBoardNavigation.html 51 */ 52public class KeyBoardNavigation extends JApplet 53{ 54 static void initTest(Container contentPane) 55 { 56 // Take the dummy data from SwingSet. 57 final String[] names = {"First Name", "Last Name", "Favorite Color", 58 "Favorite Number", "Vegetarian"}; 59 final Object[][] data = { 60 {"Mark", "Andrews", "Red", new Integer(2), new Boolean(true)}, 61 {"Tom", "Ball", "Blue", new Integer(99), new Boolean(false)}, 62 {"Alan", "Chung", "Green", new Integer(838), new Boolean(false)}, 63 {"Jeff", "Dinkins", "Turquois", new Integer(8), new Boolean(true)}, 64 {"Amy", "Fowler", "Yellow", new Integer(3), new Boolean(false)}, 65 {"Brian", "Gerhold", "Green", new Integer(0), new Boolean(false)}, 66 {"James", "Gosling", "Pink", new Integer(21), new Boolean(false)}, 67 {"David", "Karlton", "Red", new Integer(1), new Boolean(false)}, 68 {"Dave", "Kloba", "Yellow", new Integer(14), new Boolean(false)}, 69 {"Peter", "Korn", "Purple", new Integer(12), new Boolean(false)}, 70 {"Phil", "Milne", "Purple", new Integer(3), new Boolean(false)}, 71 {"Dave", "Moore", "Green", new Integer(88), new Boolean(false)}, 72 {"Hans", "Muller", "Maroon", new Integer(5), new Boolean(false)}, 73 {"Rick", "Levenson", "Blue", new Integer(2), new Boolean(false)}, 74 {"Tim", "Prinzing", "Blue", new Integer(22), new Boolean(false)}, 75 {"Chester", "Rose", "Black", new Integer(0), new Boolean(false)}, 76 {"Ray", "Ryan", "Gray", new Integer(77), new Boolean(false)}, 77 {"Georges", "Saab", "Red", new Integer(4), new Boolean(false)}, 78 {"Willie", "Walker", "Phthalo Blue", new Integer(4), new Boolean(false)}, 79 {"Kathy", "Walrath", "Blue", new Integer(8), new Boolean(false)}, 80 {"Arnaud", "Weber", "Green", new Integer(44), new Boolean(false)} 81 }; 82 83 // Create a model of the data. 84 TableModel dataModel = new AbstractTableModel() { 85 // These methods always need to be implemented. 86 public int getColumnCount() { return names.length; } 87 public int getRowCount() { return data.length;} 88 public Object getValueAt(int row, int col) {return data[row][col];} 89 90 // The default implementations of these methods in 91 // AbstractTableModel would work, but we can refine them. 92 public String getColumnName(int column) {return names[column];} 93 public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} 94 public boolean isCellEditable(int row, int col) {return true;} 95 public void setValueAt(Object aValue, int row, int column) { 96 System.out.println("Setting value to: " + aValue); 97 data[row][column] = aValue; 98 } 99 }; 100 101 // Create the table 102 JTable tableView = new JTable(dataModel); 103 // Turn off auto-resizing so that we can set column sizes programmatically. 104 // In this mode, all columns will get their preferred widths, as set blow. 105 tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 106 107 // Create a combo box to show that you can use one in a table. 108 JComboBox comboBox = new JComboBox(); 109 comboBox.addItem("Red"); 110 comboBox.addItem("Orange"); 111 comboBox.addItem("Yellow"); 112 comboBox.addItem("Green"); 113 comboBox.addItem("Blue"); 114 comboBox.addItem("Indigo"); 115 comboBox.addItem("Violet"); 116 117 TableColumn colorColumn = tableView.getColumn("Favorite Color"); 118 // Use the combo box as the editor in the "Favorite Color" column. 119 colorColumn.setCellEditor(new DefaultCellEditor(comboBox)); 120 121 // Set a pink background and tooltip for the Color column renderer. 122 DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer(); 123 colorColumnRenderer.setBackground(Color.pink); 124 colorColumnRenderer.setToolTipText("Click for combo box"); 125 colorColumn.setCellRenderer(colorColumnRenderer); 126 127 // Set a tooltip for the header of the colors column. 128 TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer(); 129 if (headerRenderer instanceof DefaultTableCellRenderer) 130 ((DefaultTableCellRenderer)headerRenderer).setToolTipText("Hi Mom!"); 131 132 // Set the width of the "Vegetarian" column. 133 TableColumn vegetarianColumn = tableView.getColumn("Vegetarian"); 134 vegetarianColumn.setPreferredWidth(100); 135 136 // Show the values in the "Favorite Number" column in different colors. 137 TableColumn numbersColumn = tableView.getColumn("Favorite Number"); 138 DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() { 139 public void setValue(Object value) { 140 int cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0; 141 setForeground((cellValue > 30) ? Color.black : Color.red); 142 setText((value == null) ? "" : value.toString()); 143 } 144 }; 145 numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT); 146 numbersColumn.setCellRenderer(numberColumnRenderer); 147 numbersColumn.setPreferredWidth(110); 148 149 // Finish setting up the table. 150 JScrollPane scrollpane = new JScrollPane(tableView); 151 scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED)); 152 scrollpane.setPreferredSize(new Dimension(430, 200)); 153 154 contentPane.add(scrollpane); 155 } 156 157 158 public void init() { 159 SwingUtilities.invokeLater(() -> { 160 try { 161 UIManager.setLookAndFeel( 162 "javax.swing.plaf.metal.MetalLookAndFeel"); 163 } catch (Exception e) { 164 throw new RuntimeException(e); 165 } 166 167 initTest(getContentPane()); 168 }); 169 } 170} 171