Skip to content

Commit 7ef2dfd

Browse files
author
Satyen Subramaniam
committed
8354233: Open some JTable bugs 6
Backport-of: ade67df0f363cb95434832f750ac901c1c75c29c
1 parent 20e9f61 commit 7ef2dfd

File tree

3 files changed

+366
-0
lines changed

3 files changed

+366
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 javax.swing.DefaultCellEditor;
25+
import javax.swing.JComboBox;
26+
import javax.swing.JFrame;
27+
import javax.swing.JScrollPane;
28+
import javax.swing.JTable;
29+
import javax.swing.table.TableColumn;
30+
31+
/*
32+
* @test
33+
* @bug 4129401
34+
* @summary Tests that keystroking for combobox cell editor in JTable works
35+
* @library /java/awt/regtesthelpers
36+
* @build PassFailJFrame
37+
* @run main/manual bug4129401
38+
*/
39+
40+
public class bug4129401 {
41+
private static final String INSTRUCTIONS = """
42+
1. Move the mouse cursor to the cell "CELL 2 1",
43+
which contains JComboBox and click left mouse button
44+
to drop down combobox list.
45+
2. Change selected item in the combobox list
46+
using up and down arrows.
47+
3. Press Esc. JComboBox drop down list should hide.
48+
If all was successful then test passes, else test fails.
49+
""";
50+
51+
public static void main(String[] args) throws Exception {
52+
PassFailJFrame.builder()
53+
.instructions(INSTRUCTIONS)
54+
.columns(50)
55+
.testUI(bug4129401::createTestUI)
56+
.build()
57+
.awaitAndCheck();
58+
}
59+
60+
public static JFrame createTestUI() {
61+
Object data[][] = new Object[4][2];
62+
JComboBox cb = new JComboBox();
63+
cb.addItem("Item1");
64+
cb.addItem("Item2");
65+
cb.addItem("Item3");
66+
cb.addItem("Item4");
67+
data[0][0] = "CELL 0 0";
68+
data[0][1] = "CELL 0 1";
69+
data[1][0] = "CELL 1 0";
70+
data[1][1] = "CELL 1 1";
71+
data[2][0] = "CELL 2 0";
72+
data[2][1] = "CELL 2 1";
73+
data[3][0] = "CELL 3 0";
74+
data[3][1] = "CELL 3 1";
75+
String[] str = {"Column 0", "Column 1"};
76+
JTable tbl = new JTable(data, str);
77+
JScrollPane sp = new JScrollPane(tbl);
78+
79+
TableColumn col = tbl.getColumn("Column 1");
80+
col.setCellEditor(new DefaultCellEditor(cb));
81+
82+
JFrame f = new JFrame("4129401 test");
83+
f.getContentPane().add(sp);
84+
f.setBounds(100, 100, 300, 300);
85+
return f;
86+
}
87+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.BorderLayout;
25+
import java.awt.Component;
26+
import java.awt.FontMetrics;
27+
import java.util.Vector;
28+
import javax.swing.JButton;
29+
import javax.swing.JFrame;
30+
import javax.swing.JPanel;
31+
import javax.swing.JScrollPane;
32+
import javax.swing.JTable;
33+
import javax.swing.SwingUtilities;
34+
import javax.swing.table.TableCellRenderer;
35+
import javax.swing.table.TableColumn;
36+
37+
/*
38+
* @test
39+
* @bug 4193727
40+
* @summary Tests that resizing JTable via TableColumn's
41+
* setWidth(int) repaints correctly
42+
* @library /java/awt/regtesthelpers
43+
* @build PassFailJFrame
44+
* @run main/manual bug4193727
45+
*/
46+
47+
public class bug4193727 {
48+
static EnhancedJTable tblResults;
49+
static JButton bTest = new JButton("Resize");
50+
51+
private static final String INSTRUCTIONS = """
52+
Push button "Resize".
53+
If either of the following happen, test fails:
54+
1) The size of the columns change
55+
2) The JTable is not repainted correctly
56+
57+
Otherwise test passes.
58+
""";
59+
60+
public static void main(String[] args) throws Exception {
61+
PassFailJFrame.builder()
62+
.instructions(INSTRUCTIONS)
63+
.columns(50)
64+
.testUI(bug4193727::createTestUI)
65+
.build()
66+
.awaitAndCheck();
67+
}
68+
69+
public static JFrame createTestUI() {
70+
JFrame frame = new JFrame("bug4193727");
71+
Vector v = new Vector();
72+
Vector data = new Vector();
73+
Vector cols = new Vector();
74+
75+
cols.add("Name");
76+
cols.add("Address");
77+
data.add("Steve");
78+
data.add("100 East Main Street");
79+
v.add(data);
80+
81+
data.add("Richard");
82+
data.add("99 Main Road");
83+
v.add(data);
84+
85+
frame.setLayout(new BorderLayout());
86+
tblResults = new EnhancedJTable(v, cols);
87+
MyTableHeader mth = new MyTableHeader();
88+
for (int i = 0; i < tblResults.getColumnCount(); i++)
89+
tblResults.getColumnModel().getColumn(i).setHeaderRenderer(mth.getTHR());
90+
tblResults.setAutoResizeMode(EnhancedJTable.AUTO_RESIZE_OFF);
91+
92+
JScrollPane pane = new JScrollPane(tblResults);
93+
frame.add(pane, BorderLayout.CENTER);
94+
JPanel panel = new JPanel();
95+
panel.add(bTest);
96+
frame.add(panel, BorderLayout.EAST);
97+
bTest.addActionListener(e -> tblResults.autoSizeColumns());
98+
frame.setSize(300, 200);
99+
return frame;
100+
}
101+
}
102+
103+
class MyTableHeader extends TableColumn {
104+
public TableCellRenderer getTHR() {
105+
return createDefaultHeaderRenderer();
106+
}
107+
}
108+
109+
class EnhancedJTable extends JTable {
110+
public EnhancedJTable(Vector data, Vector colNames) {
111+
super(data, colNames);
112+
}
113+
114+
public synchronized void autoSizeColumns() {
115+
setAutoResizeMode(AUTO_RESIZE_OFF);
116+
int colcnt = getColumnCount();
117+
int rowcnt = getRowCount();
118+
119+
for (int i = 0; i < colcnt; i++) {
120+
// get the max column width needed
121+
Component cell = getColumnModel().getColumn(i).getHeaderRenderer()
122+
.getTableCellRendererComponent(this, null, false, false, -1, i);
123+
FontMetrics fm = cell.getFontMetrics(cell.getFont());
124+
int max = SwingUtilities.computeStringWidth(fm, getColumnModel().getColumn(i).getHeaderValue()
125+
.toString() + " ");
126+
for (int j = 0; j < rowcnt; j++) {
127+
// add 2 spaces to account for gutter
128+
int width = SwingUtilities.computeStringWidth(fm, getValueAt(j, i).toString() + " ");
129+
if (max < width) max = width;
130+
}
131+
// set the new column width
132+
getColumnModel().getColumn(i).setWidth(max);
133+
}
134+
}
135+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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.GridBagConstraints;
25+
import java.awt.GridBagLayout;
26+
import java.awt.Insets;
27+
import java.util.ArrayList;
28+
import javax.swing.JButton;
29+
import javax.swing.JFrame;
30+
import javax.swing.JScrollPane;
31+
import javax.swing.JTable;
32+
import javax.swing.table.AbstractTableModel;
33+
34+
/*
35+
* @test
36+
* @bug 4242631
37+
* @summary Tests that JTable repaints itself correctly after a record
38+
* has been removed and added to the table model.
39+
* @library /java/awt/regtesthelpers
40+
* @build PassFailJFrame
41+
* @run main/manual bug4242631
42+
*/
43+
44+
public class bug4242631 {
45+
private static JButton addButton;
46+
private static JButton removeButton;
47+
private static JButton bothButton;
48+
private static SimpleTableModel tableModel;
49+
50+
private static final String INSTRUCTIONS = """
51+
Press Add button to add a record to the table. The record added should
52+
have number 0. Then press Remove/Add button some times. The record number
53+
should increase as you press. If it does not, test fails.
54+
""";
55+
56+
public static void main(String[] args) throws Exception {
57+
PassFailJFrame.builder()
58+
.instructions(INSTRUCTIONS)
59+
.columns(50)
60+
.testUI(bug4242631::createTestUI)
61+
.build()
62+
.awaitAndCheck();
63+
}
64+
65+
public static JFrame createTestUI() {
66+
JFrame frame = new JFrame("bug4242631");
67+
GridBagLayout grid = new GridBagLayout();
68+
69+
frame.setLayout(grid);
70+
GridBagConstraints c = new GridBagConstraints();
71+
c.insets = new Insets(2, 2, 2, 2);
72+
73+
// Add button.
74+
c.gridx = 0;
75+
c.gridy = 0;
76+
grid.setConstraints(addButton = new JButton("Add"), c);
77+
frame.add(addButton);
78+
79+
// Edit button.
80+
c.gridx = 1;
81+
c.gridy = 0;
82+
grid.setConstraints(removeButton = new JButton("Remove"), c);
83+
frame.add(removeButton);
84+
85+
// Remove button.
86+
c.gridx = 2;
87+
c.gridy = 0;
88+
grid.setConstraints(bothButton = new JButton("Remove/Add"), c);
89+
frame.add(bothButton);
90+
91+
// Table.
92+
c.gridx = 0;
93+
c.gridy = 1;
94+
c.gridwidth = 6;
95+
c.gridheight = 0;
96+
c.anchor = GridBagConstraints.CENTER;
97+
c.fill = GridBagConstraints.BOTH;
98+
c.weightx = 1.0;
99+
c.weighty = 1.0;
100+
JScrollPane scroll = null;
101+
tableModel = new SimpleTableModel();
102+
grid.setConstraints(scroll = new JScrollPane(new JTable(tableModel)), c);
103+
frame.add(scroll);
104+
105+
// Create some action listeners.
106+
addButton.addActionListener(event -> tableModel.addRow());
107+
removeButton.addActionListener(event -> tableModel.removeRow());
108+
bothButton.addActionListener(event -> tableModel.removeThenAddRow());
109+
110+
frame.pack();
111+
return frame;
112+
}
113+
114+
static class SimpleTableModel extends AbstractTableModel {
115+
int counter = 0;
116+
ArrayList list = new ArrayList();
117+
118+
public SimpleTableModel() {}
119+
public int getColumnCount() { return 1; }
120+
public int getRowCount() { return list.size(); }
121+
122+
public Object getValueAt(int row, int col) {
123+
String str = (String) list.get(row);
124+
return str;// + "." + col;
125+
}
126+
127+
public void addRow() {
128+
list.add("" + counter++);
129+
fireTableRowsInserted(list.size() - 1, list.size() - 1);
130+
}
131+
132+
public void removeRow() {
133+
if (list.size() == 0) return;
134+
list.remove(list.size() - 1);
135+
fireTableRowsDeleted(list.size(), list.size());
136+
}
137+
138+
public void removeThenAddRow() {
139+
if (list.size() == 0) return;
140+
removeRow();
141+
addRow();
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)