Implementing combo boxes in Java Client table columns worked out a bit more complex than check boxes – check boxes only have the possible values true or false, but combo boxes have variable lists, taken from different attributes in different tables. Here is the code I came up with for combo boxes. I wasn't too happy about having to pass in the editing context, but that's how my entity loading stuff works. I had to set the row height of the table larger because I could not seem to find a smaller (or more table-oriented) combo box – hence 'width' is a superfluous argument.
// Combo boxes in table columns -------------------------------------------
public static void set_column_combo (EOTable t, int n, EOEditingContext ec, NSArray selections, String attribute) { TableColumn tc = t.table ().getColumnModel ().getColumn (n); tc.setCellEditor (new Combo_box_editor (selections, attribute, tc.getWidth ())); tc.setCellRenderer (new Combo_box_renderer (selections, attribute, tc.getWidth ())); }
public static void set_column_combo (EOTable t, int n, EOEditingContext ec, String entity, String attribute) { set_column_combo (t, n, ec, load_entity (ec, entity), attribute); }
protected static class Combo_box_renderer extends JComboBox implements TableCellRenderer {
public Combo_box_renderer (NSArray /*[EOGenericRecord]*/ titles, String title_attribute, int width) { super (); // setPreferredSize (new Dimension (width, 14)); // Doesn't change height. setMaximumRowCount (Math.min (titles.count (), 15));
items = titles; attribute = title_attribute; java.util.Enumeration e = items.objectEnumerator ();
while (e.hasMoreElements ()) { addItem ((String)((EOGenericRecord)e.nextElement ()).valueForKey (attribute)); } }
protected NSArray /*[EOGenericRecord]*/ items; protected String attribute;
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground (table.getSelectionForeground ()); super.setBackground (table.getSelectionBackground ()); } else { setForeground (table.getForeground ()); setBackground (table.getBackground ()); }
if (value != null) setSelectedIndex (item_index (items, attribute, (String)((EOGenericRecord)value).valueForKey (attribute)));
return this; } }
protected static class Combo_box_editor extends JComboBox implements TableCellEditor {
protected EventListenerList listenerList = new EventListenerList (); protected ChangeEvent changeEvent = new ChangeEvent (this);
public Combo_box_editor (NSArray /*[EOGenericRecord]*/ titles, String title_attribute, int width) { super (); // setPreferredSize (new Dimension (width, 14)); // Doesn't change height.
setMaximumRowCount (Math.min (titles.count (), 15));
items = titles; attribute = title_attribute; java.util.Enumeration e = items.objectEnumerator ();
while (e.hasMoreElements ()) { addItem ((String)((EOGenericRecord)e.nextElement ()).valueForKey (attribute)); }
addActionListener (new ActionListener () { public void actionPerformed (ActionEvent event) { stop_editing (); } }); }
protected NSArray /*[EOGenericRecord]*/ items; protected String attribute;
public void addCellEditorListener (CellEditorListener listener) { listenerList.add (CellEditorListener.class, listener); }
public void removeCellEditorListener (CellEditorListener listener) { listenerList.remove (CellEditorListener.class, listener); }
protected void stop_editing () { CellEditorListener listener; Object [] listeners = listenerList.getListenerList ();
for (int i = 0; i < listeners.length; i++) { if (listeners [i] == CellEditorListener.class) { listener = (CellEditorListener)listeners [i + 1]; listener.editingStopped (changeEvent); } } }
public void cancelCellEditing () { CellEditorListener listener; Object [] listeners = listenerList.getListenerList ();
for (int i = 0; i < listeners.length; i++) { if (listeners [i] == CellEditorListener.class) { listener = (CellEditorListener)listeners [i + 1]; listener.editingCanceled (changeEvent); } } }
public boolean stopCellEditing () { stop_editing (); return true; }
public boolean isCellEditable (EventObject event) { return true; }
public boolean shouldSelectCell (EventObject event) { return true; }
public Object getCellEditorValue () { return (items.objectAtIndex (getSelectedIndex ())); }
public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column) { if (value != null) setSelectedIndex (item_index (items, attribute, (String)((EOGenericRecord)value).valueForKey (attribute))); return this; } }
protected static int item_index (NSArray /*[EOGenericRecord]*/ items, String attribute, String title) { int result = 0; java.util.Enumeration e = items.objectEnumerator ();
while (e.hasMoreElements ()) { if (((String)((EOGenericRecord)e.nextElement ()).valueForKey (attribute)).equals (title)) { return result; }
result = result + 1; }
return 0; }
Set up by calling:
Service.set_column_combo (telephones_table, 0, editingContext (), "Telephone_kind", "value"); Service.set_column_combo (telephones_table, 1, editingContext (), "Telephone_type", "value"); Service.set_column_combo (telephones_table, 2, editingContext (), "Country", "place");
Ian Joyner Sportstec |