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 / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

JComboBoxes in a JTable

Thread view: 
Tony Burrows - 13 Mar 2006 23:28 GMT
I have an app where cells in a table each need to display a set of values.
I can put a JComboBox into the cells of a column by setting the column
cell editor and renderers, but it is always the same set of values.  I
need to have different sets for each box, and I ony know the precise
values in response to user actions. It looks a bit like the properties
window in NetBeans or JBuilder IDEs.

I'm sure there must be a way to do this, but so far I can't work out how.
Any pointers would be welcome.

Tony
Rhino - 14 Mar 2006 00:55 GMT
>I have an app where cells in a table each need to display a set of values.
> I can put a JComboBox into the cells of a column by setting the column
[quoted text clipped - 5 lines]
> I'm sure there must be a way to do this, but so far I can't work out how.
> Any pointers would be welcome.

I found this article quite helpful when I had to wrestle with renderers and
editors in JTables a while back; I don't remember many of the details but I
was careful to bookmark this link in case I had to mess with them again:
http://www-128.ibm.com/developerworks/library/j-jtable/. Maybe the article
will help you.

--
Rhino
Tony Burrows - 14 Mar 2006 16:34 GMT
> I found this article quite helpful when I had to wrestle with renderers and
> editors in JTables a while back; I don't remember many of the details but I
> was careful to bookmark this link in case I had to mess with them again:
> http://www-128.ibm.com/developerworks/library/j-jtable/. Maybe the article
> will help you.

Thanks Rhino and Roland
Unfortunately neither does quite what I need.  It just seems to force all
the entries in a cloumn to have the same set of values.  What I needed was
in (say) row 1 a combo box of allowed java classes, in row 2 a textbox, in
row 3 a combo of allowed colors and so on.

Cracked the problem in a way by using a panel with a GridLayout using two
columns, so that I could put anything I liked in any grid box.  But I'd
still like to use a table.

Tony
Rhino - 14 Mar 2006 18:13 GMT
>> I found this article quite helpful when I had to wrestle with renderers
>> and
[quoted text clipped - 14 lines]
> columns, so that I could put anything I liked in any grid box.  But I'd
> still like to use a table.

Okay, I see what you're trying to do now. I've never tried to do what you
want in a JTable myself; that would seem to be stretching the idea of a
JTable well beyond what it was designed to do.

It sounds to me like you just want the JTable so that it keeps the various
lists in very close proximity and minimizes the amount of screen real estate
you are using. That's a reasonable goal but I don't think a JTable is the
best way to do it.

If it were me, I'd be looking at the various layout managers and trying to
figure out a way to display this information in a form of some kind,
probably not too different from your GridLayout panel, although I tend to
prefer like BoxLayout myself.

But if you want to stay with a JTable approach, there is an extensive set of
JTable examples that explore many of the more advanced things you can do
with JTables. They were developed by Nobuo Tamemasa in the early days of
Swing. Some of the code is a bit dated now due to deprecation but there's
still a lot of neat things illustrated in the examples he (?) created. You
can find the source code here: http://www.physci.org/codes/tame/ (click on
the Source link in the index on the left.) The "EachRowEditor.java" and
"EachRowRenderer.java" examples may be especially useful for your purposes.

Your other option might be to find the source code for programs that display
this information as properties the way you described in your initial post
for this thread; maybe you can find the source for NetBeans. for example,
or, failing that, for something that renders similar information in a
similar way.

--
Rhino
Tony Burrows - 15 Mar 2006 15:31 GMT
..snip..
> If it were me, I'd be looking at the various layout managers and trying to
> figure out a way to display this information in a form of some kind,
[quoted text clipped - 15 lines]
> or, failing that, for something that renders similar information in a
> similar way.

Thanks for that.  A quick look at the site was interesting.  Don't really
know why I thought of tables first, except that there are two data
columns.  I agree a lyout manager is probably the way to go.

Tony
Roland de Ruiter - 14 Mar 2006 11:25 GMT
> I have an app where cells in a table each need to display a set of values.
>  I can put a JComboBox into the cells of a column by setting the column
[quoted text clipped - 7 lines]
>
> Tony
The JTable class provides two methods, getCellEditor and prepareEditor,
which may be helpful in your case. Create your own subclass of JTable
and override either or both methods.
The prepareEditor method in particular could be used to fill the
combobox with values specifically for each row (and column).

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#getCellEditor(in
t,%20int
)>
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#prepareEditor(ja
vax.swing.table.TableCellEditor,%20int,%20int
)>

Regards,

Roland


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.