• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JTable Cell Editor

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have to use combobox as JTable cell editor.
In the first cell from the combobox when some item is selected based on that the corresponding second column cell will be populated with different data.
Exa : 1st combobox of the first column has the following data : "ABC","DEF"
ABC has following data : "aaa", "bbb", "ccc"
DEF has following data : "ddd", "eee", "fff"

So on selecting ABC from the 1st combobox in the 1st cell of the row, will lead to populate the 2nd combobox in the second cell of the same row with its corresponding data.

I hope i am able to explain the problem.
How can i achieve this ?
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This page from the Java Tutorial has a good section on using combo boxes as editors in tables:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same problem here, and the Sun table tutorial isn't much help. I finally figured it out, but I'm going to have to dig a bit to get some links to helpful articles for you.

Standby please.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to have one column of JComboBoxes within a JTable whose values depend on the selections made in another column of JComboBoxes, follow these instructions:

1. Create a table model class that extends AbstractTableModel and override the methods you'll need to maintain your table. The sun site does have good instructions on this step.

2. Create a custom TableCellRenderer. This renderer will display JComboBoxes on your table even when they are not selected. If you don't return this class for a cell, the value will appear as plain text.
    a. make a new class that extends JComboBox and implements TableCellRenderer
    b. the constructor must take a String[] for the items in the JComboBox that you'll send to the super() constructor

3. Create a custom JComboBox cell editor. This will allow the users to use JComboBox to select values within cells.
    a. make a new class that extends DefaultCellEditor
    b. must take String[] for items

#3 Ex:



4. Create a custom JTable! This is the most important step. You need to do this so you can override the 'getCellRenderer' and 'getCellEditor' methods within JTable to return the renderers and editors you choose.
    a. make new class that extends JTable
    b. override getCellRenderer to return the proper TableCellRenderer (the one you created in the correct columns) for "int row, int col"
    c. override getCellEditor to return the proper TableCellEditor (the one you created in the correct columns) for "int row, int col"

5. Create a data model that drives the table model. This data model must react accordingly when a value is selected within it. If one value is selected within the model and another must change, then the model should take care of this. It is much easier to do is this way! Keep your data separate from the view! This model should drive the display. Look up the MVC design pattern for more details.
[ August 02, 2005: Message edited by: Peter Glass ]
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Re your PM, I figured you would have both problems, so gave you the help for the first part first If you have the combo boxes working OK, I would say the next part depends on how exactly your data works.

Is the data in the second (and third etc) combo boxes static or dynamic? I.e. does 'ABC' always have the subchoices of 'aaa', 'bbb', and 'ccc', or do those subchoices depend on the contents of some other data structure? If the data is static, you would have to make two different celleditors: the initial one - a plain old combo box, and a second one that implements ActionListener and waits for changes from the first combo box, then populates itself with the appropriate values.

If the data is dynamic the approach will be similar but you will need to interface with your TableModel, which makes things more complicated.

Either way, I think you should make a class that implements both CellEditor and ActionListener, and either extends JComboBox or encapsulates an instance of it.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stuart, in my example all the data within the table is dynamic. It gets loaded from other areas of the program and there is little control over it. But you can create a data model to drive the table that will prevent implementing ActionListeners with your CellEditors.

The data within the table model should be able to handle itself, so if there is a change in a value that affects another value, the model should be smart enought to change itself. This keeps it simple within the model as well as making the table simpler from the outside, and no unecessary ActionEvents flying about.
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Peter,
Can you send some sample code for the solution you have provided. It will be a great help for me.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but the code I was referring to is on another computer system that I do not currently have access to.
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic