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 / September 2005

Tip: Looking for answers? Try searching our database.

Using a JComboBox in certain cells in a JTable

Thread view: 
Leif Bloomquist - 29 Sep 2005 17:51 GMT
Hi all,

Using the techniques from the following page, I've set up a JTable with
JComboBoxes in one column of cells.
http://javaalmanac.com/egs/javax.swing.table/ComboBox.html

However, what I'd really like to do is have a JComboBox as the editor/render
for only certain cells in the column, not the entire column.  I found a
verbal solution here:
https://lists.xcf.berkeley.edu/lists/advanced-java/2000-March/007761.html

So I get what they're saying - check the row that is currently being
edited/rendered, and return either a JComboBox for those rows, or a "normal"
JTable cell.  (Is it a JTextField?)  But I'm a bit fuzzy on where/when I
should actually put in the code that does this.  Can anyone help or provide
a short example?

Thanks,
Leif

Signature

Leif Bloomquist
leif (at) schema factor (dot) com
http://home.ica.net/~leifb/

Thomas Fritsch - 29 Sep 2005 18:47 GMT
> 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/

Vova Reznik - 29 Sep 2005 18:54 GMT
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


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.