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);

}

}

[6325 byte] By [gracklemanna] at [2007-11-27 2:24:29]
# 1
you may try this one,editor.setClickCountToStart(1); change totable.setClickCountToStart(1);
suryanto_soa at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 2
There's not a setClickCountToStart(..) method defined for JTable.
gracklemanna at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 3

This looks like a definite inconsistency between 1.5 and 1.6 to me. It is easy to show and your code sample proves it. I have had a quick look at the bugs database

http://bugs.sun.com/bugdatabase/index.jsp

and couldn't find anything, although there are quite a few outstanding bugs for JIFs.

I would raise a bug with your code example.

I know that isn't much help. If you want to work around you could perhaps add a selection listener and call editCellAt() manually - a bit hacky though!

stuckagaina at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 4

...

JTable table = new JTable(data, columnNames);

table.setSurrendersFocusOnKeystroke(true);

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

table.requestFocusInWindow(); // with this line added, it works

...

check out this for more info

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

suparenoa at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 5

table.requestFocusInWindow()

doesn't do anything different for me

Even if it did initially help, when you try to click back and forth between the TextField and the Table in the example, the problem re-appears. The problem has something to do with the editor responding to the down focus cycle I suspect, because if you click on a cell editor border you can get the table to start accepting focus normally, and similarly if you tab in to the table from the textfield it behaves normally. It is only when you try to change focus from the text field to a cell directly with the mouse that the problem appears. And for whatever reason is behaves differently in a JFrame than it does in a JInternalFrame.

gracklemanna at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 6
This has now been submitted as a bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6568959
gracklemanna at 2007-7-12 2:31:34 > top of Java-index,Desktop,Core GUI APIs...
# 7
I was using Solaris 10 x86 if you want to add that to the bug?
stuckagaina at 2007-7-12 2:31:35 > top of Java-index,Desktop,Core GUI APIs...