bug ? jdk 1.6 JTable focus issue JInternalFrame setClickCountToStart
OK. Trying to make the switch to JDK 1.6. Having a real hard time with JTables(suprise). It looks like they have fixed many of the issues with JTable, but here's something I can't figure out.
If I have a JTable, and I set all the columns to use the DefaultCellEditor with the clickCountToStart set to 1 (it defaults to 2), the table works fine if placed on a panel directly within a JFrame. But if it is placed within a JInternalFrame, the focus is all messed up.
The attached code illustrates the issue.
Run it as is, select the JTextField labeled Field 1, then try to click into a cell in the table. Generally you can't. If you click right on a cell border it will select. And if you tab into the table, you can select cells until you get out of the table and try to re-enter.
In the code, there is a line that sets clickCountToStart to 1, if you set that to 2 and rerun the code, it works pretty good.
Similarly, if you comment out the line:
initInteralFrame(panel);
and uncomment the next line:
initNoInteralFrame(panel);
It works fine with clickcount set to 1.
Why does it behave differently directly in a JFrame than it does in a JInternalFrame? Can anyone find a way to make it work correctly in the JInternalFrame.
Thanks.
import java.awt.Dimension;
import java.util.Enumeration;
import javax.swing.BoxLayout;
import javax.swing.DefaultCellEditor;
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.TableColumn;
import javax.swing.table.TableColumnModel;
publicclass TableTestextends JFrame
{
public TableTest()
{
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];
JTable table =new JTable(data, columnNames);
table.setSurrendersFocusOnKeystroke(true);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
DefaultCellEditor editor =new DefaultCellEditor(new JTextField());
// ************ change to 2 to make it work smoothly
editor.setClickCountToStart(1);
TableColumnModel tcm = table.getColumnModel();
Enumeration e = tcm.getColumns();
TableColumn col =null;
while( e.hasMoreElements() )
{
col = (TableColumn) e.nextElement();
col.setCellEditor(editor);
}
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 TableTest().setVisible(true);
}
}

