Menu

Jeppers + JComboBox

2004-11-23
2013-04-15
  • Neil Gordon

    Neil Gordon - 2004-11-23

    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

     
    • Cameron Zemek

      Cameron Zemek - 2004-11-23

      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.

       
    • Neil Gordon

      Neil Gordon - 2004-11-23

      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...

       
    • Cameron Zemek

      Cameron Zemek - 2004-11-23

      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));

       
    • Neil Gordon

      Neil Gordon - 2004-11-24

      Thanks for the example will try it out today and  reportback.

       
    • Neil Gordon

      Neil Gordon - 2004-11-25

      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;
              }
          }

       

Log in to post a comment.