• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

JComboBox dilemma in JTable

Hi,
I have multiple TableCellEditor for the columns in my JTable. Here is the structure of my JTable:

Column0(single-JComboBox)  
Column1(multi-JComboBox)  
Column2(String)
Column3(User input)
Column4(single-JComboBox)

Initially, the JTable only contains headers and empty cells. The user clicks cell(0, 0) to select from JComboBox's popup menu.

Column1 has 5 lists of choices for the JComboBox. Depending on what the user has selected in Column0's JComboBox, Column1 displays a JComboBox popup menu accordingly. Once the users clicks an item in Column1's JComboBox popup menu, Column2's value in the same row is set accordingly.

I create a TableCellEditor for row and it seems to work. However, when the user changes the selection in Column0 that contains a value, it doesn't affect Column1 until the user clicks the cell in Column1.

The problem is that the user has to click the cell to make the JComboBox visible. I don't want to set default selectedIndex for the JComboBox, so I paint the cell in Column1 to flag the user the change in Column0. Is there any better solution?

Thanks in advance.
0
vvstan
Asked:
vvstan
1 Solution
 
ykaganovCommented:
override setValueAt method of your TableModel:

pubic void setValueAt(Object value, int row, int col) {
  super.setValueAt(value, row, col);

  if(col == 0) {
    Object value2 = someCustomLogic();
    // calculate the new value in Column1
    setValueAt(value2, row, 1);
  }
}

Then, in the JComboBox that renders the editor for Column1, override

getTableCellEditorComponent

to populate the values and update selections based on getValueAt(row, 0) before returning it.

Hope this helps,

- Eugene
0
 
vvstanAuthor Commented:
Hi Eugene,

I am trying to figure out how to apply your solution to my problem.

In your setValueAt method, I don't think that I can calculate the new value for the corresponding cell in Column1. I can provide a corresponding JComboBox list but again, the user has to click on the corresponding cell in Column1 after changing the selection in Column0 to see the list, then the user can make the selection.

Right now I have an ActionListener for Column0 so Column1 will provide an appropriate JComboBox list accordingly but the user has to click the cell to make the JComboBox visible.

Thanks
0
 
vvstanAuthor Commented:
Hi Eugene,

I am trying to figure out how to apply your solution to my problem.

In your setValueAt method, I don't think that I can calculate the new value for the corresponding cell in Column1. I can provide a corresponding JComboBox list but again, the user has to click on the corresponding cell in Column1 after changing the selection in Column0 to see the list, then the user can make the selection.

Right now I have an ActionListener for Column0 so Column1 will provide an appropriate JComboBox list accordingly but the user has to click the cell to make the JComboBox visible.

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ykaganovCommented:
If I understand your application logic correctly, the value selected in column0 drives the set of values selected in column1. So, if a user changes the selection in column0, he has to go and edit column1 next, right?

The only things you can do when value in Column0 changes is repopulate JComboBox list values for Column1, and either clear the Column1 field, or pick a default value from that list. The user still has to pick the right value from the list for Column1 as a separate step.

I think what you're trying to do is to pop up JComboBox for Column1 as soon as the user is done editing column0. I would suggest that you actually don't need to do that, just don't let the user proceed with your application until the table data is configured correctly (for example, if the table is in a dialog, don't enable "ok" button if column1 has any nulls in it). But I suppose you could add a listener to the table model that will invoke startEditing() on the column1 cell whenever the column0 changes, after you've done the model changes I suggested.

- Eugene
0
 
CleanupPingCommented:
vvstan:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept ykaganov's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now