Skip to content

Commit 7eab2d9

Browse files
author
Alisen Chung
committed
8354095: Open some JTable bugs 5
Reviewed-by: kizune, honkar
1 parent 0b2a2f3 commit 7eab2d9

File tree

4 files changed

+460
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)