Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

swing property editor



The PropertyEditor class is a component which presents datas in a two-dimensional table format. It can be used to show and modify every types of properties.

There are only two columns in this Table:
  • the left column shows the name of the property to be shown
  • the right column shows the value of this property to edit. All editors managed by the ExtendedCellEditor class can be used
It extends the functionalities of the generic JTable component by allowing to:
  • add every type of editor / renderer for each row, providing that they are JComponents managed by the ExtendedCellEditor class

Creation

The process of the creation of a PropertyEditor must follow these rules: The component can then be used as any JPanel.

Note: Events fired by each rows are managed by the editor added in the row by the PropertyEditor.addProperty(JComponent, Object, String) method.

Example

  String[] fill_values = { "NONE", "HORIZONTAL", "VERTICAL VALUE", "BOTH" };
  JComboBox cb = new JComboBox(fill_values);
  cb.setSelectedItem("VERTICAL VALUE");

  JTextField tf = new JTextField("2");

  JButton bo = new JButton("1");

  JComboBox cb2 = new JComboBox(fill_values);
  cb2.setSelectedItem("VERTICAL VALUE");

  JListChooser.ListChooserHandler handler = new JListChooser.ListChooserHandler() {
    @Override
    public Object getCurrentValue(JComponent c) {
      JComboBox cb = (JComboBox) c;
      return cb.getSelectedItem();
    }
  };
  JListChooser lc = new JListChooser(cb2, handler);
  lc.setTitle("test");

  JListSelector ls = new JListSelector(lc);
  JFileSelector fs = new JFileSelector("tata");
  JFileSelector fs2 = new JFileSelector();
  JColorSelector cs = new JColorSelector();
  cs.setColor(Color.RED);

  cb.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
      System.out.println("comboBox changed ");
    }
  });

  cs.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
      System.out.println("color changed ");
    }
  });

  ls.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
      System.out.println("List changed ");
    }
  });

  PropertyEditor p = new PropertyEditor();
  p.addProperty(cb, "VERTICAL VALUE", "combo box");
  p.addProperty(tf, "2", "text");
  p.addProperty(bo, "1", "button");
  p.addProperty(fs, "", "file 1");
  p.addProperty(fs2, "", "file 2");
  p.addProperty(cs, "", "Color");
  p.addProperty(ls, "", "List");
  p.setVisible(true);

  JFrame f = new JFrame("test");
  f.setSize(300, 300);
  f.getContentPane().add(p, BorderLayout.CENTER);
  f.setVisible(true);
Result:
propertyeditor

Property tree panel

The JPropertyTreePanel class is a component presents settings in a tree. Each Node in the tree can present several associaetd properties, managed by a Property editor.

There are three ways to add property nodes to the tree:

Example

  • Create the PropertyEditors and set them visible:
       PropertyEditor p1 = new PropertyEditor();
       JTextField tf = new JTextField("1");
       p1.addProperty(tf,"1","prop 1");
       p1.setVisible(true);
    
       PropertyEditor p2 = new PropertyEditor();
       JTextField tf2 = new JTextField("2");
       p2.addProperty(tf2,"2","prop 2");
       p2.setVisible(true);
    
  • Create the JPropertyTreePanel
      JPropertyTreePanel panel = new JPropertyTreePanel();
    
  • Associate editors with nodes:
       panel.addPropertyListNode(panel.getRootNode(), "node 1", p1);
       DefaultMutableTreeNode node = panel.addEmptyNode(panel.getRootNode(), "sub-node");
       panel.addPropertyListNode(node, "node 2", p2);
    
complete code:
   PropertyEditor p1 = new PropertyEditor();
   JTextField tf = new JTextField("1");
   p1.addProperty(tf,"1","prop 1");
   p1.setVisible(true);

   PropertyEditor p2 = new PropertyEditor();
   JTextField tf2 = new JTextField("2");
   p2.addProperty(tf2,"2","prop 2");
   p2.setVisible(true);

   JPropertyTreePanel panel = new JPropertyTreePanel();

   panel.addPropertyListNode(panel.getRootNode(), "node 1", p1);
   DefaultMutableTreeNode node = panel.addEmptyNode(panel.getRootNode(), "sub-node");
   panel.addPropertyListNode(node, "node 2", p2);

   JFrame f = new JFrame("test");
   f.setSize(300, 300);
   f.getContentPane().add(p, BorderLayout.CENTER);
   f.setVisible(true);
Result:
propertytreepanel

Extended Cell Editor

Main Article: Extended Cell Editor

The ExtendedCellEditor class allows to put custom components in JTable cells.

Categories: packages | swing

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences