Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Extended Cell Editor



The ExtendedCellEditor class allows to put custom components in JTable cells[1]
The Java DefaultCellEditor class only allows to use checkbox, textfields, or combobox
.

There are two ways to use a component in an ExtendedCellEditor:
  • Using one of the ready-made constructors or the ExtendedCellEditor class
  • Create a custom GenericEditor component and use it in the specific ExtendedCellEditor GenericEditor constructor

Using one of the ready-made constructors

The ExtendedCellEditor class has several specific constructors, allowing to use more components that allowed in the DefaultCellEditor class: The ExtendedEditorTable.addEditor(int, int, TableCellEditor) allows to set an Editor for a specific cell in the table.

Example

  DefaultTableModel model = new DefaultTableModel();
  model.addColumn("Value");
  Vector<Object> v = new Vector<>();
  v.add(true);
  model.addRow(v);

  ExtendedEditorTable table = new ExtendedEditorTable(model);
  table.addEditor(0, 0, new ExtendedCellEditor(new JCheckBox()));

Using a custom component

It is possible to use a custom component to integrate in the table. The associated constructor is the GenericEditor constructor.

Example

For example, we could define a custom editor which would allow to set a value by clicking on a checkbox on a specific dialog:
   public class MyGenericEditor extends AbstractGenericEditor<MyGenericPanel> {
     public MyGenericEditor(JTable table) {
       super(table);
       editor = new TestGenericPanel();
     }

     public Object getValue() {
       return editor.getValue();
     }

     public void setValue(Object value) {
       editor.setValue((Boolean) value);
     }

     public class MyGenericPanel extends JPanel {
       private final JCheckBox cb = new JCheckBox("CheckBox");

       private TestGenericPanel() {
         this.setLayout(new BorderLayout());
         this.add(cb, BorderLayout.CENTER);
       }

       public boolean getValue() {
         return cb.isSelected();
       }

       public int getClickCountToStart() {
         return 2;
       }

       public void setValue(boolean b) {
         cb.setSelected(b);
       }
     }
   }
And to integrate this component in the table:
   DefaultTableModel model = new DefaultTableModel();
   model.addColumn("Value");
   Vector<Object> v = new Vector<>();
   v.add(true);
   model.addRow(v);
   v = new Vector<>();
   v.add(1);
   model.addRow(v);

   ExtendedEditorTable table = new ExtendedEditorTable(model);
   TestGenericEditor editor = new TestGenericEditor(table);
   table.addEditor(0, 0, new ExtendedCellEditor(editor));
   table.addOpenEditorListener(0, 0, editor.getAppproveListener());

Notes

  1. ^ The Java DefaultCellEditor class only allows to use checkbox, textfields, or combobox

Categories: packages | swing

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