It works fine for the first time you select the cell & choose a value, however, I suspect then that the cell is becoming a String object, because for second and subsequent edits, the standard text editor is being used.
Any ideas? I tried looking for an easier way to do the above code, but couldn't find one.
Cheers Neil
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I thought that I was doing this through the code above - 'Then set the editor for this custom class. ' - it works for the first time the wipes out the editor on the second try.
Will it work if I extend JComboBox for my new class? IE sm.setEditor(MyNewCombo.class, new GenericCellEditor(ed));
I suspect there is something else happening behind the scenes...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was meaning to use a custom ComboBoxModel. However, you can do it with the default model.
Here is an example:
// NOTE: Choice class implements the toString() method
ed=new JComboBox();
ed.addItem(new Choice("ham"));
ed.addItem(new Choice("spam"));
sheet1.setValueAt(c, 3, 3);
JGrid grid=sheet1.getGrid();
DefaultStyleModel sm=(DefaultStyleModel)grid.getStyleModel();
sm.setEditor(Choice.class, new GenericCellEditor(ed));
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I tried changing one of the editors on a spreadsheet for a new module I'm working on to be a JComboBox. I used the following code:
ed=new JComboBox();
ed.addItem("hello");
ed.addItem("xxx");
sheet1.setValueAt(ed, 3, 3);
JGrid grid=sheet1.getGrid();
DefaultStyleModel sm=(DefaultStyleModel)grid.getStyleModel();
sm.setEditor(JComboBox.class, new GenericCellEditor(ed));
It works fine for the first time you select the cell & choose a value, however, I suspect then that the cell is becoming a String object, because for second and subsequent edits, the standard text editor is being used.
Any ideas? I tried looking for an easier way to do the above code, but couldn't find one.
Cheers Neil
An easy way would be use a custom model for the JComboBox that uses a custom class. Then set the editor for this custom class.
Another way would be to implement a StyleModel that allowed the setting of an editor for specific cells or rows/columns.
I thought that I was doing this through the code above - 'Then set the editor for this custom class. ' - it works for the first time the wipes out the editor on the second try.
Will it work if I extend JComboBox for my new class? IE sm.setEditor(MyNewCombo.class, new GenericCellEditor(ed));
I suspect there is something else happening behind the scenes...
I was meaning to use a custom ComboBoxModel. However, you can do it with the default model.
Here is an example:
// NOTE: Choice class implements the toString() method
ed=new JComboBox();
ed.addItem(new Choice("ham"));
ed.addItem(new Choice("spam"));
sheet1.setValueAt(c, 3, 3);
JGrid grid=sheet1.getGrid();
DefaultStyleModel sm=(DefaultStyleModel)grid.getStyleModel();
sm.setEditor(Choice.class, new GenericCellEditor(ed));
Thanks for the example will try it out today and reportback.
It worked great.
Here is a complete working example:
.
.
JComboBox ed=new JComboBox();
ed.addItem(new Choice("ham"));
ed.addItem(new Choice("spam"));
sheet1.setValueAt(new Choice("ham"), 3, 3);
JGrid grid=sheet1.getGrid();
DefaultStyleModel sm=(DefaultStyleModel)grid.getStyleModel();
sm.setEditor(Choice.class, new GenericCellEditor(ed));
.
.
private class Choice {
private String choice;
public Choice(String choice) {
this.choice=choice;
}
public String toString() {
return choice;
}
}