-
Bug
-
Resolution: Fixed
-
P5
-
1.3.0
-
None
-
beta
-
x86
-
windows_nt
DefaultTreeCellEditor should install a CellEditorListener on the real editor so that it can properly clean things up after editing has completed when initiated from the real editor. Here is some code that illustrates the problem. To reproduce this do the following:
. click on the colors node, press F2 and type something in (don't press return)
. click on the stop button
. click on the sports node, press F2
notice that both the JTextField and JComboBox are active.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Test {
private JTree tree;
private TestEditor testEditor;
public static void main(String[] args) {
new Test();
}
public Test() {
tree = new JTree();
tree.setEditable(true);
testEditor = new TestEditor();
tree.setCellEditor(new DefaultTreeCellEditor(tree,
(DefaultTreeCellRenderer)tree.getCellRenderer(),
testEditor));
JButton button = new JButton("stop");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
testEditor.stopCellEditing();
}
});
JFrame frame = new JFrame("TEST");
frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
class TestEditor extends AbstractCellEditor implements TreeCellEditor {
private JComboBox comboBox;
private JTextField textField;
private boolean comboBoxActive;
TestEditor() {
comboBox = new JComboBox(new String[] { "one", "two" });
textField = new JTextField();
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected,
boolean expanded,
boolean leaf, int row) {
if (row % 2 == 0) {
comboBoxActive = false;
return comboBox;
}
comboBoxActive = true;
return textField;
}
public Object getCellEditorValue() {
if (comboBoxActive) {
return comboBox.getSelectedItem();
}
return textField.getText();
}
}
}
. click on the colors node, press F2 and type something in (don't press return)
. click on the stop button
. click on the sports node, press F2
notice that both the JTextField and JComboBox are active.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Test {
private JTree tree;
private TestEditor testEditor;
public static void main(String[] args) {
new Test();
}
public Test() {
tree = new JTree();
tree.setEditable(true);
testEditor = new TestEditor();
tree.setCellEditor(new DefaultTreeCellEditor(tree,
(DefaultTreeCellRenderer)tree.getCellRenderer(),
testEditor));
JButton button = new JButton("stop");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
testEditor.stopCellEditing();
}
});
JFrame frame = new JFrame("TEST");
frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
class TestEditor extends AbstractCellEditor implements TreeCellEditor {
private JComboBox comboBox;
private JTextField textField;
private boolean comboBoxActive;
TestEditor() {
comboBox = new JComboBox(new String[] { "one", "two" });
textField = new JTextField();
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected,
boolean expanded,
boolean leaf, int row) {
if (row % 2 == 0) {
comboBoxActive = false;
return comboBox;
}
comboBoxActive = true;
return textField;
}
public Object getCellEditorValue() {
if (comboBoxActive) {
return comboBox.getSelectedItem();
}
return textField.getText();
}
}
}