|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2025, 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 | + |
| 24 | +import java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import javax.swing.DefaultCellEditor; |
| 27 | +import javax.swing.JComboBox; |
| 28 | +import javax.swing.JFrame; |
| 29 | +import javax.swing.JLabel; |
| 30 | +import javax.swing.JScrollPane; |
| 31 | +import javax.swing.JTable; |
| 32 | +import javax.swing.border.BevelBorder; |
| 33 | +import javax.swing.table.AbstractTableModel; |
| 34 | +import javax.swing.table.DefaultTableCellRenderer; |
| 35 | +import javax.swing.table.TableCellRenderer; |
| 36 | +import javax.swing.table.TableColumn; |
| 37 | +import javax.swing.table.TableModel; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 4201917 |
| 42 | + * @summary Shift Click in table before making selection |
| 43 | + * @library /java/awt/regtesthelpers |
| 44 | + * @build PassFailJFrame |
| 45 | + * @run main/manual ShiftClick |
| 46 | + */ |
| 47 | + |
| 48 | +public class ShiftClick { |
| 49 | + private static final String INSTRUCTIONS = """ |
| 50 | + Shift click in the table. Check that all cells |
| 51 | + from the first through where you clicked are selected. |
| 52 | + If the cells are selected, press pass, otherwise fail. |
| 53 | + """; |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + PassFailJFrame.builder() |
| 57 | + .instructions(INSTRUCTIONS) |
| 58 | + .columns(50) |
| 59 | + .testUI(ShiftClick::createTestUI) |
| 60 | + .logArea(6) |
| 61 | + .build() |
| 62 | + .awaitAndCheck(); |
| 63 | + } |
| 64 | + |
| 65 | + public static JFrame createTestUI() { |
| 66 | + JFrame frame = new JFrame("ShiftClick"); |
| 67 | + |
| 68 | + // Take the dummy data from SwingSet. |
| 69 | + final String[] names = {"First Name", "Last Name", "Favorite Color", |
| 70 | + "Favorite Number", "Vegetarian"}; |
| 71 | + final Object[][] data = { |
| 72 | + {"Mark", "Andrews", "Red", 2, true}, |
| 73 | + {"Tom", "Ball", "Blue", 99, false}, |
| 74 | + {"Alan", "Chung", "Green", 838, false}, |
| 75 | + {"Jeff", "Dinkins", "Turquois", 8, true}, |
| 76 | + {"Amy", "Fowler", "Yellow", 3, false}, |
| 77 | + {"Brian", "Gerhold", "Green", 0, false}, |
| 78 | + {"James", "Gosling", "Pink", 21, false}, |
| 79 | + {"David", "Karlton", "Red", 1, false}, |
| 80 | + {"Dave", "Kloba", "Yellow", 14, false}, |
| 81 | + {"Peter", "Korn", "Purple", 12, false}, |
| 82 | + {"Phil", "Milne", "Purple", 3, false}, |
| 83 | + {"Dave", "Moore", "Green", 88, false}, |
| 84 | + {"Hans", "Muller", "Maroon", 5, false}, |
| 85 | + {"Rick", "Levenson", "Blue", 2, false}, |
| 86 | + {"Tim", "Prinzing", "Blue", 22, false}, |
| 87 | + {"Chester", "Rose", "Black", 0, false}, |
| 88 | + {"Ray", "Ryan", "Gray", 77, false}, |
| 89 | + {"Georges", "Saab", "Red", 4, false}, |
| 90 | + {"Willie", "Walker", "Phthalo Blue", 4, false}, |
| 91 | + {"Kathy", "Walrath", "Blue", 8, false}, |
| 92 | + {"Arnaud", "Weber", "Green", 44, false} |
| 93 | + }; |
| 94 | + |
| 95 | + // Create a model of the data. |
| 96 | + TableModel dataModel = new AbstractTableModel() { |
| 97 | + // These methods always need to be implemented. |
| 98 | + public int getColumnCount() { |
| 99 | + return names.length; |
| 100 | + } |
| 101 | + |
| 102 | + public int getRowCount() { |
| 103 | + return data.length; |
| 104 | + } |
| 105 | + |
| 106 | + public Object getValueAt(int row, int col) { |
| 107 | + return data[row][col]; |
| 108 | + } |
| 109 | + |
| 110 | + // The default implementations of these methods in |
| 111 | + // AbstractTableModel would work, but we can refine them. |
| 112 | + public String getColumnName(int column) { |
| 113 | + return names[column]; |
| 114 | + } |
| 115 | + |
| 116 | + public Class getColumnClass(int c) { |
| 117 | + return getValueAt(0, c).getClass(); |
| 118 | + } |
| 119 | + |
| 120 | + public boolean isCellEditable(int row, int col) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + public void setValueAt(Object aValue, int row, int column) { |
| 125 | + System.out.println("Setting value to: " + aValue); |
| 126 | + data[row][column] = aValue; |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + // Create the table |
| 131 | + JTable tableView = new JTable(dataModel); |
| 132 | + // Turn off auto-resizing so that we can set column sizes programmatically. |
| 133 | + // In this mode, all columns will get their preferred widths, as set blow. |
| 134 | + tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
| 135 | + |
| 136 | + // Create a combo box to show that you can use one in a table. |
| 137 | + JComboBox comboBox = new JComboBox(); |
| 138 | + comboBox.addItem("Red"); |
| 139 | + comboBox.addItem("Orange"); |
| 140 | + comboBox.addItem("Yellow"); |
| 141 | + comboBox.addItem("Green"); |
| 142 | + comboBox.addItem("Blue"); |
| 143 | + comboBox.addItem("Indigo"); |
| 144 | + comboBox.addItem("Violet"); |
| 145 | + |
| 146 | + TableColumn colorColumn = tableView.getColumn("Favorite Color"); |
| 147 | + // Use the combo box as the editor in the "Favorite Color" column. |
| 148 | + colorColumn.setCellEditor(new DefaultCellEditor(comboBox)); |
| 149 | + |
| 150 | + // Set a pink background and tooltip for the Color column renderer. |
| 151 | + DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer(); |
| 152 | + colorColumnRenderer.setBackground(Color.pink); |
| 153 | + colorColumnRenderer.setToolTipText("Click for combo box"); |
| 154 | + colorColumn.setCellRenderer(colorColumnRenderer); |
| 155 | + |
| 156 | + // Set a tooltip for the header of the colors column. |
| 157 | + TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer(); |
| 158 | + if (headerRenderer instanceof DefaultTableCellRenderer) |
| 159 | + ((DefaultTableCellRenderer) headerRenderer).setToolTipText("Hi Mom!"); |
| 160 | + |
| 161 | + // Set the width of the "Vegetarian" column. |
| 162 | + TableColumn vegetarianColumn = tableView.getColumn("Vegetarian"); |
| 163 | + vegetarianColumn.setPreferredWidth(100); |
| 164 | + |
| 165 | + // Show the values in the "Favorite Number" column in different colors. |
| 166 | + TableColumn numbersColumn = tableView.getColumn("Favorite Number"); |
| 167 | + DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() { |
| 168 | + public void setValue(Object value) { |
| 169 | + int cellValue = (value instanceof Number) ? ((Number) value).intValue() : 0; |
| 170 | + setForeground((cellValue > 30) ? Color.black : Color.red); |
| 171 | + setText((value == null) ? "" : value.toString()); |
| 172 | + } |
| 173 | + }; |
| 174 | + numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT); |
| 175 | + numbersColumn.setCellRenderer(numberColumnRenderer); |
| 176 | + numbersColumn.setPreferredWidth(110); |
| 177 | + |
| 178 | + // Finish setting up the table. |
| 179 | + JScrollPane scrollpane = new JScrollPane(tableView); |
| 180 | + scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED)); |
| 181 | + scrollpane.setPreferredSize(new Dimension(430, 200)); |
| 182 | + |
| 183 | + frame.add(scrollpane); |
| 184 | + frame.setSize(500, 200); |
| 185 | + return frame; |
| 186 | + } |
| 187 | +} |
0 commit comments