0

Ok, say I have a database table with two columns - one "Name", the other "Age", and there are over 40 names and their respective ages in the table. I want these names to be listed out in a jList/jComboBox, and also I want to be able to click on a name in the jList/jComboBox and have its respective age appear in - say - a text box. Do I have to go about this by simply writing a code that selects all the names from the table and populates the jList/jComboBox and then another code that takes the selected name, puts it in an sql statement, finds the matching age and sends it to a text box, OR is there some kind of a VB-esque column-to-comboBox/List-binding that I can utilise to go about this?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Akinwale Agbaje
  • 315
  • 3
  • 6
  • 17

3 Answers3

0

For only 40 name-age combinations, I would just query the database once, and store this information in a Map. Then you can just query the map when a name is selected, and update the age textfield. This will go a lot faster then running SQL queries each time the selection has been changed.

Robin
  • 36,233
  • 5
  • 47
  • 99
0

You have to set Model for your swing elements and for updating data based on changes at one place to other implement Listeners. Have a look at this Binding comboboxes in swing

Community
  • 1
  • 1
manocha_ak
  • 904
  • 7
  • 19
0

Create a custom object that stores both the name and age values and add this object to the combobox. Then when you select an item you have access to both values.

For example: How to use Map element as text of a JComboBox

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • cant resist: overriding toString for view reasons is sooo dirty that you should _never_ do it (as we both know :-) A custom renderer is _the_ way to go - except the even better alternative of using SwingX which supports pluggable string representation on the renderer level :-) – kleopatra Dec 12 '11 at 15:57
  • @kleopatra, AFAIK you need to override toString() of the Object in the model in order to be able to select the items in the list by typing the first character of the items string representation. – camickr Dec 12 '11 at 20:46
  • You're missing the point. The point isn't to get data out and simply display them in a combo box or list; the point is to get the data, display them in a combo box or list so that when each of them is clicked, a corresponding or matching data is displayed in - say - a textbox. There has to be some link between what's in the combo box and what will appear in the text box. – Akinwale Agbaje Dec 13 '11 at 08:51
  • @Akinwale Agbaje - missed the point, too. Actually I didn't quite understand the question, probably because my VB times are decades ago, thanks Goddess :-) No, there is nothing pre-fabricated in Swing - you have to code it, possibly with the help of a binding framework like JGoodies Binding or (Better)BeansBinding – kleopatra Dec 13 '11 at 09:29
  • good point, Rob - tend to forget how crude core Swing is at places. Or the other way round: how much more work SwingX takes over :-) All XCollectionComponents have a getString(...) method, which is ultimately used internally always (JXList in getNextMatch via its Searchable, JXComboBox in a custom KeySelectionManager) – kleopatra Dec 13 '11 at 09:34
  • @AkinwaleAgbaje I understand the question you don't understand the solution. The item in the combo box contains the value displayed in the combo box and it contains the additional data to be displayed in your other Swing component. So your ActionListener just needs to get the selected item, and then it can access the other data from that item. The code I gave you simple displays both values on the screen. It is a single line of code to display the other value in a Swing component of your choosing. – camickr Dec 13 '11 at 16:09
  • Thanks all. Camickr, cheers. Adapted your answer and now everything works perfectly :D – Akinwale Agbaje Dec 16 '11 at 10:17