Skip to content

Commit d167b4d

Browse files
author
Satyen Subramaniam
committed
8354465: Open some JTable bugs 8
Backport-of: 7a72f0fac9a0704c4a0ada781f1cadd7c4903b3e
1 parent b802991 commit d167b4d

File tree

5 files changed

+612
-0
lines changed

5 files changed

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

Comments
 (0)