Skip to content

Commit 9eeb86d

Browse files
author
Alisen Chung
committed
8354341: Open some JTable bugs 7
Reviewed-by: kizune, serb
1 parent 7cd084c commit 9eeb86d

File tree

4 files changed

+448
-0
lines changed

4 files changed

+448
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 javax.swing.JFrame;
25+
import javax.swing.JScrollPane;
26+
import javax.swing.JTable;
27+
import javax.swing.table.AbstractTableModel;
28+
import javax.swing.table.TableModel;
29+
30+
/*
31+
* @test
32+
* @bug 4128506
33+
* @summary Tests that JTable with AUTO_RESIZE_ALL_COLUMNS correctly compute width of columns
34+
* @library /java/awt/regtesthelpers
35+
* @build PassFailJFrame
36+
* @run main/manual bug4128506
37+
*/
38+
39+
public class bug4128506 {
40+
private static final String INSTRUCTIONS = """
41+
If the columns of JTable have the same width the test passes, else test fails.
42+
""";
43+
44+
public static void main(String[] args) throws Exception {
45+
PassFailJFrame.builder()
46+
.instructions(INSTRUCTIONS)
47+
.columns(50)
48+
.testUI(bug4128506::createTestUI)
49+
.build()
50+
.awaitAndCheck();
51+
}
52+
53+
public static JFrame createTestUI() {
54+
final Object[][] data = {
55+
{"cell_1_1", "cell_1_2", "cell_1_3"},
56+
{"cell_2_1", "cell_2_2", "cell_2_3"},
57+
{"cell_3_1", "cell_3_2", "cell_3_3"},
58+
{"cell_4_1", "cell_4_2", "cell_4_3"},
59+
};
60+
61+
TableModel dataModel = new AbstractTableModel() {
62+
public int getColumnCount() {
63+
return 3;
64+
}
65+
66+
public int getRowCount() {
67+
return data.length;
68+
}
69+
70+
public Object getValueAt(int row, int col) {
71+
return data[row][col];
72+
}
73+
};
74+
75+
JFrame frame = new JFrame("bug4128506");
76+
JTable tableView = new JTable(dataModel);
77+
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
78+
tableView.getColumnModel().getColumn(1).setMinWidth(5);
79+
JScrollPane scrollpane = new JScrollPane(tableView);
80+
frame.add(scrollpane);
81+
frame.pack();
82+
return frame;
83+
}
84+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.Dimension;
25+
import java.awt.Robot;
26+
import java.util.Vector;
27+
import javax.swing.JFrame;
28+
import javax.swing.JPanel;
29+
import javax.swing.JScrollPane;
30+
import javax.swing.JTable;
31+
import javax.swing.SwingUtilities;
32+
import javax.swing.table.DefaultTableModel;
33+
34+
/*
35+
* @test
36+
* @bug 4190222
37+
* @summary Setting data vector on the model correctly repaint table
38+
* @key headful
39+
* @run main bug4190222
40+
*/
41+
42+
public class bug4190222 {
43+
static JFrame frame;
44+
static DefaultTableModel dtm;
45+
static JTable tbl;
46+
47+
static Vector data;
48+
static Vector colNames;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
Robot robot = new Robot();
53+
robot.setAutoDelay(250);
54+
55+
SwingUtilities.invokeAndWait(() -> createTestUI());
56+
robot.waitForIdle();
57+
robot.delay(1000);
58+
59+
SwingUtilities.invokeAndWait(() -> {
60+
Dimension preResize = tbl.getSize();
61+
dtm.setDataVector(data, colNames);
62+
63+
if (!preResize.equals(tbl.getSize())) {
64+
throw new RuntimeException("Size of table changed after resizing.");
65+
}
66+
});
67+
} finally {
68+
SwingUtilities.invokeAndWait(() -> {
69+
if (frame != null) {
70+
frame.dispose();
71+
}
72+
});
73+
}
74+
}
75+
76+
public static void createTestUI() {
77+
frame = new JFrame("bug4190222");
78+
79+
data = new Vector(1, 1);
80+
colNames = new Vector(3);
81+
for (int i = 1; i < 4; i++) {
82+
Vector row = new Vector(1, 1);
83+
row.addElement("Row " + i + ", Col 1");
84+
row.addElement("Row " + i + ", Col 2");
85+
row.addElement("Row " + i + ", Col 3");
86+
data.addElement(row);
87+
}
88+
colNames.addElement("Col 1");
89+
colNames.addElement("Col 2");
90+
colNames.addElement("Col 3");
91+
92+
dtm = new DefaultTableModel(data, colNames);
93+
tbl = new JTable(dtm);
94+
JScrollPane scrollPane = new JScrollPane(tbl);
95+
frame.add("Center", scrollPane);
96+
JPanel panel = new JPanel();
97+
frame.add("South", panel);
98+
99+
frame.pack();
100+
frame.setLocationRelativeTo(null);
101+
frame.setVisible(true);
102+
}
103+
}

0 commit comments

Comments
 (0)