my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Binding a JComboBox to a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Binding a JComboBox to a JTable" Watch "Binding a JComboBox to a JTable" New topic
Author

Binding a JComboBox to a JTable

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
I seem to be lost in the google Jungle.
Could you show me the link which teaches us how to bind a JComboBox to a JTable?



Thanks
Jack
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Jack Luk wrote:
Could you show me the link which teaches us how to bind a JComboBox to a JTable?

I don't understand what you mean by "bind a JComboBox to JTable".

Do you mean having a JComboBox as an editor for one of the columns in a JTable? if so, here's the link


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Ranganathan Kaliyur Mannar wrote:
Jack Luk wrote:
Could you show me the link which teaches us how to bind a JComboBox to a JTable?

I don't understand what you mean by "bind a JComboBox to JTable".

Do you mean having a JComboBox as an editor for one of the columns in a JTable? if so, here's the link


Hi Ranganathan Kaliyur Mannar,
No, I don't mean that. I actually mean to choose an item from the combo box, then corresponding data from the database will be displayed.
Thanks a lot. Cheers!
Jack
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Jack Luk wrote:
Ranganathan Kaliyur Mannar wrote:
Jack Luk wrote:
Could you show me the link which teaches us how to bind a JComboBox to a JTable?

I don't understand what you mean by "bind a JComboBox to JTable".

Do you mean having a JComboBox as an editor for one of the columns in a JTable? if so, here's the link




Hi Ranganathan Kaliyur Mannar,
No, I don't mean that. I actually mean to choose an item from the combo box, then corresponding data from the database will be displayed.
For example, When I choose an employee id, his attendance information will be shown on the JTable.
Thanks a lot. Cheers!
Jack
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

I don't have any readymade links for that. You just need to add an ItemListener or ActionListener to the combobox and when an item is selected,
you hit the database with a query (with a where clause where you would put the selected item) and bring back data.
And then you add the data to the TableModel which will display the data in a JTable.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Ranganathan Kaliyur Mannar wrote:I don't have any readymade links for that. You just need to add an ItemListener or ActionListener to the combobox and when an item is selected,
you hit the database with a query (with a where clause where you would put the selected item) and bring back data.
And then you add the data to the TableModel which will display the data in a JTable.


Does that mean I have to "implements" an ItemListener for the JComboBox. Actually, I am working with Netbeans. The javax.swing.JComboBox field is not editable inside JFrame by default.
How would I implement such as interface?


Now I have this


Thanks
Jack
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

There is no simple add() method in JTable to which you can add an object (data).

Have you worked with JComboBox and JTable before? If not, I would suggest you to start reading the tutorial. Particularly the two pages: combo and table

Read the tutorial, write sample code to understand each of these components/concepts separately and then come back to this program.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Ranganathan Kaliyur Mannar wrote:There is no simple add() method in JTable to which you can add an object (data).

Have you worked with JComboBox and JTable before? If not, I would suggest you to start reading the tutorial. Particularly the two pages: combo and table

Read the tutorial, write sample code to understand each of these components/concepts separately and then come back to this program.



Okay, I get it now. I can get the text from the combo, then query the database and populate the table with the Resultset.
Right?
Thanks
Jack
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Jack Luk wrote:
I can get the text from the combo, then query the database and populate the table with the Resultset.

That is right. But populating the JTable has to be done via the TableModel.
Alternatively, you can create a JTable with an Object[][] - so, if you can build such an array from browsing the ResultSet, you can use this.
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601




Whoops! Eariler I had some logic errors. Now some runtime errors. Something wrong with the actionListener
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Didn't you say earlier that you wanted to listen for combo box's changes? why are then listening for table selection changes?
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
Ranganathan Kaliyur Mannar wrote:Didn't you say earlier that you wanted to listen for combo box's changes? why are then listening for table selection changes?



Yes, you're right. Please see my last post. I've performed an addItem to the comboBox somewhere

OK: I fixed that. Sorry, If I have time to come back, I'll tell you what I did

Thanks
Jack
 
jQuery in Action, 2nd edition
 
subject: Binding a JComboBox to a JTable