• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binding a JComboBox to a JTable

 
Jacky Luk
Ranch Hand
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacky Luk
Ranch Hand
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Whoops! Eariler I had some logic errors. Now some runtime errors. Something wrong with the actionListener
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic