Skip to content

Instantly share code, notes, and snippets.

@Fuud
Last active December 23, 2015 01:39
Show Gist options
  • Save Fuud/6561408 to your computer and use it in GitHub Desktop.
Save Fuud/6561408 to your computer and use it in GitHub Desktop.
bug6568959.java
import sun.awt.SunToolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.concurrent.atomic.AtomicReference;
/* @test
* @bug 6568959
* @summary If JTable and other focusable components are placed in internal frame one-click editors are not usable
* @author Fedor Bobin
* @run main bug6568959
*/
public class bug6568959 {
private static JTable table;
private static JFrame frame;
public static void main(String args[]) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(20);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createGUI();
frame.setVisible(true);
}
});
toolkit.realSync();
clickCell(robot, table, 0, 0);
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
if (table.getEditingColumn() != 0) {
throw new AssertionError("cell [0 0] should be under editing, but editing column=" + table.getEditingColumn());
}
if (table.getEditingRow() != 0) {
throw new AssertionError("cell [0 0] should be under editing, but editing column=" + table.getEditingRow());
}
if (table.getEditorComponent()==null){
throw new AssertionError("cell [0 0] should be under editing, but editor component is null");
}
if (table.getEditorComponent().getParent()!=table){
throw new AssertionError("cell [0 0] should be under editing, but editor component is not on table");
}
}
});
frame.setVisible(false);
}
public static void createGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
String[] columnNames = {"C1"};
Object[][] data = new Object[1][1];
table = new JTable(data, columnNames);
table.setSurrendersFocusOnKeystroke(true);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
DefaultCellEditor editor = new DefaultCellEditor(new JTextField());
editor.setClickCountToStart(1);
table.setDefaultEditor(Object.class, editor);
JScrollPane jScrollPane = new JScrollPane(table);
final JTextField comp = new JTextField(15);
panel.add(comp);
panel.add(jScrollPane);
JInternalFrame jif = new JInternalFrame();
JDesktopPane desktop = new JDesktopPane();
jif.add(panel);
jif.pack();
jif.setVisible(true);
desktop.add(jif);
frame.setContentPane(desktop);
frame.setSize(new Dimension(800, 600));
}
private static void clickCell(final Robot robot, final JTable table, final int row, final int column) throws Exception {
final AtomicReference<Point> pointRef = new AtomicReference<Point>();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
Rectangle rect = table.getCellRect(row, column, false);
Point point = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
SwingUtilities.convertPointToScreen(point, table);
pointRef.set(point);
}
});
robot.mouseMove(pointRef.get().x, pointRef.get().y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment