Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4330357

DefaultTreeCellEditor should install listener on real editor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 1.4.0
    • 1.3.0
    • client-libs
    • 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();
              }
          }
      }

            svioletsunw Scott Violet (Inactive)
            svioletsunw Scott Violet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: