Re: Using a JComboBox in certain cells in a JTable
- From: "Leif Bloomquist" <spam@xxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 16:11:03 -0400
Hi Thomas, thanks for the quick reply.
"Thomas Fritsch" <i.dont.like.spam@xxxxxxxxxxx> wrote in message
news:1128016026.646361@xxxxxxxxxxxxxxxx
> 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();
}
}
}
}
--
Leif Bloomquist
leif (at) schema factor (dot) com
http://home.ica.net/~leifb/
.
- References:
- Using a JComboBox in certain cells in a JTable
- From: Leif Bloomquist
- Re: Using a JComboBox in certain cells in a JTable
- From: Thomas Fritsch
- Using a JComboBox in certain cells in a JTable
- Prev by Date: Re: Mouse Cursor with hourglass and pointer
- Next by Date: Re: Can I use an Action, but have diff text for JButton versus JMenuItem?
- Previous by thread: Re: Using a JComboBox in certain cells in a JTable
- Next by thread: Re: Using a JComboBox in certain cells in a JTable
- Index(es):