JCheckBox wont receive initial focus in JTable under 1.6

If I have a JCheckBox in a JTable, clicking the checkbox has no effect unless the table already has focus. This is new to 1.6 I believe.

The code example below should illustrate the problem. With focus on the JTextField at the top, try to click a checkbox in the table. It won't take respond. If you click another field in the table first, the checkbox will then begin responding. Two strange things I've noticed with it are.

1) removing the call the table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); makes it work correctly. Of course then it won't give up focus correctly.

2) My example has the JTable within a JInternalFrame. If the table is just in a JFrame, the problem disappears. You can commment/uncomment these two lines to see the difference:

initInteralFrame(panel);

//initNoInteralFrame(panel);

I'm very surprised nobody else has noticed this, but I didn't see any postings or bugs on it. Can anyone see something I am doing wrong or find a workaround?

I submitted this bug a while back which is probably related, but this case with the checkbox is a bit less obscure:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6568959

import java.awt.Dimension;

import javax.swing.BoxLayout;

import javax.swing.JDesktopPane;

import javax.swing.JFrame;

import javax.swing.JInternalFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

publicclass TableTest2extends JFrame

{

public TableTest2()

{

addWindowListener(new java.awt.event.WindowAdapter()

{

publicvoid windowClosing(java.awt.event.WindowEvent evt)

{

System.exit(0);

}

});

JPanel panel =new JPanel();

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

String[] columnNames ={"First Name","Last Name","Sport","# of Years","Vegetarian"};

//Object[][] data= new Object[3][5];

Object[][] data ={

{"Mary","Campione","Snowboarding",new Integer(5),new Boolean(false)},

{"Alison","Huml","Rowing",new Integer(3),new Boolean(true)},

{"Kathy","Walrath","Knitting",new Integer(2),new Boolean(false)},

{"Sharon","Zakhour","Speed reading",new Integer(20),new Boolean(true)},

{"Philip","Milne","Pool",new Integer(10),new Boolean(false)}

};

DefaultTableModel myModel =new DefaultTableModel(data, columnNames)

{

public Class getColumnClass(int c)

{

return getValueAt(0, c).getClass();

}

};

JTable table =new JTable(myModel);

table.setSurrendersFocusOnKeystroke(true);

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

JScrollPane jScrollPane =new JScrollPane();

jScrollPane.setViewportView(table);

panel.add(new JLabel("Field 1"));

panel.add(new JTextField(15));

panel.add(jScrollPane);

// ************ comment out one or the other of these to see error

initInteralFrame(panel);

//initNoInteralFrame(panel);

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

setSize(new Dimension(800, 600));

setLocation((screenSize.width-800)/2,(screenSize.height-600)/2);

}

privatevoid initInteralFrame(JPanel panel)

{

JInternalFrame jif =new JInternalFrame();

JDesktopPane desktop =new JDesktopPane();

jif.setVisible(true);

desktop.add(jif);

setContentPane(desktop);

jif.add(panel);

jif.pack();

}

privatevoid initNoInteralFrame(JPanel panel)

{

getContentPane().add(panel);

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[])

{

new TableTest2().setVisible(true);

}

}

[7386 byte] By [gracklemanna] at [2007-11-27 9:55:30]
# 1
> or find a workaround? It appears to me that the first click is "selecting" the internal frame to make it active and therefore have focus.So try making the internal frame active by invoking the setSelected() method after the JFrame is made visible.
camickra at 2007-7-13 0:25:29 > top of Java-index,Desktop,Core GUI APIs...
# 2

> It appears to me that the first click is "selecting"

> the internal frame to make it active and therefore

> have focus.

Under 1.5, that appears to be the behavior. Under 1.6 the focus starts out in the internal frame on the JTextField above the JTable. You can click into any field in the table from there fine, but you can't transfer focus from the JTextField outside the table to the JCheckbox in the table... no matter how many clicks. The checkbox only works after the table becomes focused by clicking on one of the other fields or tabbing into the table.

gracklemanna at 2007-7-13 0:25:29 > top of Java-index,Desktop,Core GUI APIs...