Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions test/jdk/javax/swing/JTable/bug4129401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

/*
* @test
* @bug 4129401
* @summary Tests that keystroking for combobox cell editor in JTable works
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4129401
*/

public class bug4129401 {
private static final String INSTRUCTIONS = """
1. Move the mouse cursor to the cell "CELL 2 1",
which contains JComboBox and click left mouse button
to drop down combobox list.
2. Change selected item in the combobox list
using up and down arrows.
3. Press Esc. JComboBox drop down list should hide.
If all was successful then test passes, else test fails.
""";

public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(bug4129401::createTestUI)
.build()
.awaitAndCheck();
}

public static JFrame createTestUI() {
Object data[][] = new Object[4][2];
JComboBox cb = new JComboBox();
cb.addItem("Item1");
cb.addItem("Item2");
cb.addItem("Item3");
cb.addItem("Item4");
data[0][0] = "CELL 0 0";
data[0][1] = "CELL 0 1";
data[1][0] = "CELL 1 0";
data[1][1] = "CELL 1 1";
data[2][0] = "CELL 2 0";
data[2][1] = "CELL 2 1";
data[3][0] = "CELL 3 0";
data[3][1] = "CELL 3 1";
String[] str = {"Column 0", "Column 1"};
JTable tbl = new JTable(data, str);
JScrollPane sp = new JScrollPane(tbl);

TableColumn col = tbl.getColumn("Column 1");
col.setCellEditor(new DefaultCellEditor(cb));

JFrame f = new JFrame("4129401 test");
f.getContentPane().add(sp);
f.setBounds(100, 100, 300, 300);
return f;
}
}
135 changes: 135 additions & 0 deletions test/jdk/javax/swing/JTable/bug4193727.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FontMetrics;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/*
* @test
* @bug 4193727
* @summary Tests that resizing JTable via TableColumn's
* setWidth(int) repaints correctly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4193727
*/

public class bug4193727 {
static EnhancedJTable tblResults;
static JButton bTest = new JButton("Resize");

private static final String INSTRUCTIONS = """
Push button "Resize".
If either of the following happen, test fails:
1) The size of the columns change
2) The JTable is not repainted correctly

Otherwise test passes.
""";

public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(bug4193727::createTestUI)
.build()
.awaitAndCheck();
}

public static JFrame createTestUI() {
JFrame frame = new JFrame("bug4193727");
Vector v = new Vector();
Vector data = new Vector();
Vector cols = new Vector();

cols.add("Name");
cols.add("Address");
data.add("Steve");
data.add("100 East Main Street");
v.add(data);

data.add("Richard");
data.add("99 Main Road");
v.add(data);

frame.setLayout(new BorderLayout());
tblResults = new EnhancedJTable(v, cols);
MyTableHeader mth = new MyTableHeader();
for (int i = 0; i < tblResults.getColumnCount(); i++)
tblResults.getColumnModel().getColumn(i).setHeaderRenderer(mth.getTHR());
tblResults.setAutoResizeMode(EnhancedJTable.AUTO_RESIZE_OFF);

JScrollPane pane = new JScrollPane(tblResults);
frame.add(pane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.add(bTest);
frame.add(panel, BorderLayout.EAST);
bTest.addActionListener(e -> tblResults.autoSizeColumns());
frame.setSize(300, 200);
return frame;
}
}

class MyTableHeader extends TableColumn {
public TableCellRenderer getTHR() {
return createDefaultHeaderRenderer();
}
}

class EnhancedJTable extends JTable {
public EnhancedJTable(Vector data, Vector colNames) {
super(data, colNames);
}

public synchronized void autoSizeColumns() {
setAutoResizeMode(AUTO_RESIZE_OFF);
int colcnt = getColumnCount();
int rowcnt = getRowCount();

for (int i = 0; i < colcnt; i++) {
// get the max column width needed
Component cell = getColumnModel().getColumn(i).getHeaderRenderer()
.getTableCellRendererComponent(this, null, false, false, -1, i);
FontMetrics fm = cell.getFontMetrics(cell.getFont());
int max = SwingUtilities.computeStringWidth(fm, getColumnModel().getColumn(i).getHeaderValue()
.toString() + " ");
for (int j = 0; j < rowcnt; j++) {
// add 2 spaces to account for gutter
int width = SwingUtilities.computeStringWidth(fm, getValueAt(j, i).toString() + " ");
if (max < width) max = width;
}
// set the new column width
getColumnModel().getColumn(i).setWidth(max);
}
}
}
144 changes: 144 additions & 0 deletions test/jdk/javax/swing/JTable/bug4242631.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/*
* @test
* @bug 4242631
* @summary Tests that JTable repaints itself correctly after a record
* has been removed and added to the table model.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4242631
*/

public class bug4242631 {
private static JButton addButton;
private static JButton removeButton;
private static JButton bothButton;
private static SimpleTableModel tableModel;

private static final String INSTRUCTIONS = """
Press Add button to add a record to the table. The record added should
have number 0. Then press Remove/Add button some times. The record number
should increase as you press. If it does not, test fails.
""";

public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(bug4242631::createTestUI)
.build()
.awaitAndCheck();
}

public static JFrame createTestUI() {
JFrame frame = new JFrame("bug4242631");
GridBagLayout grid = new GridBagLayout();

frame.setLayout(grid);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 2, 2);

// Add button.
c.gridx = 0;
c.gridy = 0;
grid.setConstraints(addButton = new JButton("Add"), c);
frame.add(addButton);

// Edit button.
c.gridx = 1;
c.gridy = 0;
grid.setConstraints(removeButton = new JButton("Remove"), c);
frame.add(removeButton);

// Remove button.
c.gridx = 2;
c.gridy = 0;
grid.setConstraints(bothButton = new JButton("Remove/Add"), c);
frame.add(bothButton);

// Table.
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 6;
c.gridheight = 0;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
JScrollPane scroll = null;
tableModel = new SimpleTableModel();
grid.setConstraints(scroll = new JScrollPane(new JTable(tableModel)), c);
frame.add(scroll);

// Create some action listeners.
addButton.addActionListener(event -> tableModel.addRow());
removeButton.addActionListener(event -> tableModel.removeRow());
bothButton.addActionListener(event -> tableModel.removeThenAddRow());

frame.pack();
return frame;
}

static class SimpleTableModel extends AbstractTableModel {
int counter = 0;
ArrayList list = new ArrayList();

public SimpleTableModel() {}
public int getColumnCount() { return 1; }
public int getRowCount() { return list.size(); }

public Object getValueAt(int row, int col) {
String str = (String) list.get(row);
return str;// + "." + col;
}

public void addRow() {
list.add("" + counter++);
fireTableRowsInserted(list.size() - 1, list.size() - 1);
}

public void removeRow() {
if (list.size() == 0) return;
list.remove(list.size() - 1);
fireTableRowsDeleted(list.size(), list.size());
}

public void removeThenAddRow() {
if (list.size() == 0) return;
removeRow();
addRow();
}
}
}