I want to use a JComboBox
as a cell editor in a JXTreeTable
. It works fine with a standard DefaultCellEditor
(i.e. with a click count to start equal to 2).
Now I want the column to be editable on only one click. So I added a cellEditor.setClickCountToStart(1);
statement to my code.
Here is my SSCCE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
public class TestCellEditorForJXTreeTable {
/** The JXTreeTable */
JXTreeTable treeTable;
/** The model */
DefaultTreeTableModel treeTableModel;
/** Constructor */
public TestCellEditorForJXTreeTable() {
treeTable = new JXTreeTable();
treeTableModel = new DefaultTreeTableModel() {
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "A";
case 1:
return "B";
}
return null;
}
@Override
public Object getValueAt(Object node, int column) {
switch (column) {
case 0:
return ((DefaultMutableTreeTableNode) node).getUserObject();
case 1:
return "Value in B";
}
return null;
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public boolean isCellEditable(Object node, int column) {
return column == 1;
}
};
treeTable.setTreeTableModel(treeTableModel);
}
public static void main(String[] args) {
TestCellEditorForJXTreeTable test = new TestCellEditorForJXTreeTable();
// Root node
DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode("root");
test.treeTableModel.setRoot(root);
// New nodes/rows
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode("child_node");
test.treeTableModel.insertNodeInto(node, root, 0);
DefaultMutableTreeTableNode node2 = new DefaultMutableTreeTableNode("child_node2");
test.treeTableModel.insertNodeInto(node2, root, 1);
// Showing the frame
showTable(test.treeTable);
// Setting the cell editor
DefaultCellEditor cellEditor = new DefaultCellEditor(new JComboBox(new String[]{"1", "2", "3"}));
cellEditor.setClickCountToStart(1);
test.treeTable.getColumn(1).setCellEditor(cellEditor);
}
/** Shows a JXTreeTable in a frame */
private static void showTable(JXTreeTable table) {
JFrame frame = new JFrame("Testing cell editor for JXTreeTable");
frame.setPreferredSize(new Dimension(640, 480));
frame.setLayout(new BorderLayout());
frame.add(table, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
But now it looks pretty ugly:
When I click on an editable cell it opens the JComboBox
popup menu (Great! It's what I was expecting!), but this popup menu is immediately closed (Erf!). It flashes. I have to click a second time on the selected cell to get it definitively opened.
The problem repeats each time I select another cell in the editable column.
How could I get the JComboBox
popup menu really opened after the first click?
Thanks.
Edit 2014-01-24
Here is the same example, but using JTable
. The JComboBox
popup menu does not flash.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TestCellEditorForJTable {
/** The JTable */
JTable table;
/** The model */
DefaultTableModel tableModel;
/** Constructor */
public TestCellEditorForJTable() {
table = new JTable();
tableModel = new DefaultTableModel(new String[] {"A", "B"}, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 1;
}
};
table.setModel(tableModel);
}
public static void main(String[] args) {
TestCellEditorForJTable test = new TestCellEditorForJTable();
// New rows
test.tableModel.insertRow(0, new String[] {"Value1 in A", "Value1 in B"});
test.tableModel.insertRow(1, new String[] {"Value2 in A", "Value2 in B"});
// Showing the frame
showTable(test.table);
// Setting the cell editor
DefaultCellEditor cellEditor = new DefaultCellEditor(new JComboBox(new String[]{"1", "2", "3"}));
cellEditor.setClickCountToStart(1);
test.table.getColumnModel().getColumn(1).setCellEditor(cellEditor);
}
/** Shows a table in a frame */
private static void showTable(JTable table) {
JFrame frame = new JFrame("Testing cell editor for JTable");
frame.setPreferredSize(new Dimension(640, 480));
frame.setLayout(new BorderLayout());
frame.add(table, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And I forgot to mention that I'm using Java 1.6.
Edit 2014-01-24 (2)
Using the ContainerListener
and the FocusListener
of the kleopatra's answer, and running the same execution flow, I get the following output with the JXTreeTable
SSCCE:
// first click
24.01.2014 13:10:59 my.pkg.TestCellEditorForJXTreeTable$2 componentAdded
INFO: java.awt.event.ContainerEvent[COMPONENT_ADDED...JXTreeTable...
24.01.2014 13:10:59 my.pkg.TestCellEditorForJXTreeTable$3 focusGained
INFO: java.awt.FocusEvent[FOCUS_GAINED...JXTreeTable...
24.01.2014 13:10:59 my.pkg.TestCellEditorForJXTreeTable$2 componentRemoved
INFO: java.awt.event.ContainerEvent[COMPONENT_REMOVED...JXTreeTable...
24.01.2014 13:10:59 my.pkg.TestCellEditorForJXTreeTable$3 focusLost
INFO: java.awt.FocusEvent[FOCUS_LOST...JXTreeTable...
// second click
24.01.2014 13:11:02 my.pkg.TestCellEditorForJXTreeTable$2 componentAdded
INFO: java.awt.event.ContainerEvent[COMPONENT_ADDED...JXTreeTable...
24.01.2014 13:11:02 my.pkg.TestCellEditorForJXTreeTable$3 focusGained
INFO: java.awt.FocusEvent[FOCUS_GAINED...JXTreeTable...
24.01.2014 13:11:02 my.pkg.TestCellEditorForJXTreeTable$2 componentRemoved
INFO: java.awt.event.ContainerEvent[COMPONENT_REMOVED...JXTreeTable...
24.01.2014 13:11:02 my.pkg.TestCellEditorForJXTreeTable$3 focusLost
INFO: java.awt.FocusEvent[FOCUS_LOST...JXTreeTable...
Tricky bugger - and I think it's indeed a core issue.
Let's first define exactly what/when it is happening: take the plain table example (btw: +1 for the nice and concise SSCCE!)
Digging reveals the probable reason: it's an out-of-order focusLost received after the combo was added again as editing component. To see, register a containerListener to the table and a focusListener to the combo and print the events
The output:
A quick hack could be to force the popup open in the focusListener. Didn't check for side-effects, though.