Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Dynamic combo box editor in JTable

Thread view: 
Mr Smith - 14 Mar 2005 15:48 GMT
Hello,

i have a JTable with 2 columns. The first column is "static". But i want to enable the user to select the value of the 2nd column from a ComboBox editor. The thing is that the combo box items depends on the first column (for a row).

So:

- i can't use:
 jTable.setDefaultEditor(MyObjct.class, new DefaultCellEditor(staticComboBox));

- i can't make this work:
 jTable.setDefaultEditor(MyObject.class, new MyObjectEditor());

    public class MyObjectEditor extends AbstractCellEditor implements TableCellEditor,
       ActionListener {

       private MyObject o;

       public Object getCellEditorValue() {
           return o;
       }

       public Component getTableCellEditorComponent(JTable table,
               Object value,
               boolean isSelected,
               int row,
               int column) {

           o = (MyObject)value;
           
       // creat a JComboBox, depending on the argument
           // column 0 -> static object
           // column 1 -> combo box
           return getCombo((MyObject)table.getValueAt(row, 0));
       }
   }

Any help apreciated.
Mr Smith - 14 Mar 2005 16:37 GMT
> Hello,
>
> i have a JTable with 2 columns. The first column is "static". But i want to enable the user to select the value of the 2nd column from a ComboBox editor. The thing is that the combo box items depends on the first column (for a row).

responding myself, this is working:

    public class MyObjectEditor extends DefaultCellEditor {

       
       public MyObjectEditor() {
           super(new JComboBox(new Object[] { null }));
       }

       public Component getTableCellEditorComponent(JTable table,
               Object value,
               boolean isSelected,
               int row,
               int column) {
           
           fillCombo((JComboBox)editorComponent, (MyObject)table.getValueAt(row, 0));
           
           ((JComboBox)editorComponent).setSelectedItem(value);
           
           return editorComponent;
       }
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.