
Signature
Leif Bloomquist
leif (at) schema factor (dot) com
http://home.ica.net/~leifb/
> Hi all,
>
[quoted text clipped - 5 lines]
> for only certain cells in the column, not the entire column. I found a
> verbal solution here:
JComboBox makes sense only as cell editor, not as cell renderer. As cell
renderer the default renderer (a subclass of JLabel) will probably be
sufficient.
> https://lists.xcf.berkeley.edu/lists/advanced-java/2000-March/007761.html
>
[quoted text clipped - 3 lines]
> should actually put in the code that does this. Can anyone help or provide
> a short example?
You'll have to develop an implementation of the
javax.swing.table.TableCellEditor interface, most probably by extending
class javax.swing.DefaultCellEditor. The code mentioned above is to be
in its getTableCellEditorComponent(...) method. Then you create an
instance of this class and set it onto your table, either with
JTable#setDefaultEditor(Class, TableCellEditor) or with
JTable#setCellEditor(TableCellEditor)

Signature
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Leif Bloomquist - 29 Sep 2005 21:11 GMT
Hi Thomas, thanks for the quick reply.
> JComboBox makes sense only as cell editor, not as cell renderer. As cell
> renderer the default renderer (a subclass of JLabel) will probably be
> sufficient.
From a usability point of view, I want the cell to look like a JComboBox
even when it's not in use, so the user knows which rows are dropdowns and
which are normal text entry. But I think the renderer will be easy compared
to the editor.
> You'll have to develop an implementation of the
> javax.swing.table.TableCellEditor interface, most probably by extending
> class javax.swing.DefaultCellEditor. The code mentioned above is to be
> in its getTableCellEditorComponent(...) method. Then you create an
Thanks, I got it working. Here's the code. Some of it seems really
sketchy though, especially the 'lastrow' business to remember which row is
being edited. Is there a cleaner way to go about this?
private class MyCustomEditor extends DefaultCellEditor
{
// Internal array of editors.
private Component[] editors = null;
// Maintain a reference to the last selected row
private int lastrow = -1;
// This gets called immediately before getCellEditorValue(), below.
public Component getTableCellEditorComponent(JTable table, Object
value, boolean isSelected, int row, int column)
{
lastrow=row;
return editors[row]; // This works!
}
// Assume that lastrow is set immediately before this
public Object getCellEditorValue()
{
Object o = null;
if (editors[lastrow] instanceof JTextField)
{
o = ((JTextField)editors[lastrow]).getText();
}
else if (editors[lastrow] instanceof JComboBox)
{
o =
((JComboBox)editors[lastrow]).getSelectedItem().toString();
}
return o;
}
// Constructor
public MyCustomEditor(int[] parameters, int rows)
{
super(new JTextField("dummy"));
editors = new Component[rows];
for (int i=0; i<rows; i++)
{
if ( parameters[i].isEnumeration() )
{
editors[i]= new JComboBox(
parameters[i].getPossibleValues() );
}
else
{
editors[i]= new JTextField();
}
}
}
}

Signature
Leif Bloomquist
leif (at) schema factor (dot) com
http://home.ica.net/~leifb/
1. Override JTable
public Component prepareRenderer(TableCellRenderer,int,int)
2. Override DefautlTableCellRenderer
public Component getTableCellRendererComponent(JTable,Object,
boolean,boolean,int,int)
3. Create your own renderer (has to implement TableCellRenderer)
I prefer #3.
> Hi all,
>
[quoted text clipped - 15 lines]
> Thanks,
> Leif