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