我想在JXTreeTable
中使用JComboBox
作为单元格编辑器。它与标准DefaultCellEditor
一起工作正常(即点击次数开始等于2)。
现在,我希望在 只有一个 点击时可以修改该列。所以我在代码中添加了cellEditor.setClickCountToStart(1);
语句。
这是我的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);
}
}
但现在看起来很丑陋:
当我点击一个可编辑的单元格时,它会打开JComboBox
弹出菜单(太棒了!这就是我所期待的!),但是此弹出式菜单立即关闭( Erf !)。它闪烁。我必须在所选单元格上再次单击以使其最终打开。
每次在可编辑列中选择另一个单元格时,问题都会重复。
首次点击后如何才能打开JComboBox
弹出式菜单 ?
感谢。
以下是相同的示例,但使用JTable
。 JComboBox
弹出式菜单不会闪烁。
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);
}
}
我忘了提到我正在使用Java 1.6。
使用kleopatra's answer的ContainerListener
和FocusListener
,并运行相同的执行流程,我使用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...
答案 0 :(得分:4)
Tricky bugger - 我认为这确实是一个核心问题。
让我们首先确定它究竟发生了什么/何时发生:采用普通表示例(btw:+1为漂亮而简洁的SSCCE!)
挖掘揭示了可能的原因:在组合再次添加为编辑组件之后,它是一个无序焦点已收到。要查看,请将containerListener注册到表,并将focusListener注册到组合并打印事件
ContainerListener containerL = new ContainerListener() {
@Override
public void componentRemoved(ContainerEvent e) {
LOG.info("" + e);
}
@Override
public void componentAdded(ContainerEvent e) {
LOG.info("" + e);
}
};
table.addContainerListener(containerL);
FocusListener focusL = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
LOG.info("" + e);
// following line is a hack around: force the popup open
// ((JComboBox) cellEditor.getComponent()).setPopupVisible(true);
}
@Override
public void focusLost(FocusEvent e) {
LOG.info("" + e);
}
};
cellEditor.getComponent().addFocusListener(focusL);
输出:
// first click
24.01.2014 12:13:44 org.jdesktop.swingx.table.TestCellEditorForJTable$2 componentAdded
INFO: java.awt.event.ContainerEvent[COMPONENT_ADDED,child=null] on javax.swing.JTable...
24.01.2014 12:13:44 org.jdesktop.swingx.table.TestCellEditorForJTable$3 focusGained
INFO: java.awt.FocusEvent[FOCUS_GAINED,permanent,opposite=javax.swing.JTable
// second click
24.01.2014 12:13:49 org.jdesktop.swingx.table.TestCellEditorForJTable$2 componentRemoved
INFO: java.awt.event.ContainerEvent[COMPONENT_REMOVED,child=null] on javax.swing.JTable
24.01.2014 12:13:49 org.jdesktop.swingx.table.TestCellEditorForJTable$2 componentAdded
INFO: java.awt.event.ContainerEvent[COMPONENT_ADDED,child=null] on javax.swing.JTable
// here's the problem: focusLost _after_ added again
24.01.2014 12:13:49 org.jdesktop.swingx.table.TestCellEditorForJTable$3 focusLost
INFO: java.awt.FocusEvent[FOCUS_LOST,permanent,opposite=javax.swing.JTable
24.01.2014 12:13:49 org.jdesktop.swingx.table.TestCellEditorForJTable$3 focusGained
INFO: java.awt.FocusEvent[FOCUS_GAINED,permanent,opposite=javax.swing.JTable
快速破解可能是强制在focusListener中打开弹出窗口。但是,没有检查副作用。
答案 1 :(得分:3)
有趣的事实
如果从组合框中选择一个值,则编辑器按预期工作(下一次单击可编辑单元格将根据需要打开该单元格的组合框),但如果在不选择值的情况下关闭组合框,则其行为与你形容。所以问题似乎是组合框不会停止编辑,直到你选择一个值或焦点在一些其他组件。因此,第一次单击到另一个单元格会使其自己的编辑器请求焦点而不是开始编辑。
仔细观察DefaultCellEditor实施问题,只有ActionListener附加到组合框,导致fireEditingStopped()在选择项目时通过EditorDelegate进行调用但是组合框在没有选择值的情况下关闭或取消时没有任何反应:
public DefaultCellEditor(final JComboBox comboBox) {
editorComponent = comboBox;
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
delegate = new EditorDelegate() {...}
comboBox.addActionListener(delegate); // delegate is the ActionListener
}
protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
...
public void actionPerformed(ActionEvent e) {
DefaultCellEditor.this.stopCellEditing(); // This will finally call fireEditingStopped();
}
}
使用组合框作为编辑器制作您自己的TableCellEditor并附加PopupMenuListener以根据需要致电fireEditingStopped()或fireEditingCanceled()。例如:
class ComboBoxEditor extends AbstractCellEditor implements TableCellEditor {
private JComboBox editor;
private int clickCountToStart = 2;
private Object selectedValue;
public ComboBoxEditor(Object[] selectableValues) {
editor = new JComboBox(selectableValues);
editor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedValue = editor.getSelectedItem();
ComboBoxEditor.this.fireEditingStopped();
}
});
editor.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
// Nothing to do here, it's not relevant to your purpose
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
ComboBoxEditor.this.fireEditingStopped();
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
ComboBoxEditor.this.fireEditingCanceled();
}
});
}
public void setClickCountToStart(int clickCountToStart) {
this.clickCountToStart = clickCountToStart;
}
public int getClickCountToStart() {
return clickCountToStart;
}
// Rest of implementation is up to you, look into DefaultCellEditor implementation
}