Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: JComboBox with IDentifiers Date: Mon, 13 Aug 2012 18:49:20 +0200 Organization: A noiseless patient Spider Lines: 38 Message-ID: References: <08a4e9e5-be46-4a15-94c4-e6bfab71d781@googlegroups.com> <2eae09a5-6d01-401a-acb0-cc54ba8b516e@googlegroups.com> <5a6762d4-5fb9-451d-86eb-98c26f3ef95a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 13 Aug 2012 16:49:32 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="f5413cdf228456e5cdb66b1fa4498c4d"; logging-data="20672"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19q9wgvzGuvbv2sH8X4+SB8" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: <5a6762d4-5fb9-451d-86eb-98c26f3ef95a@googlegroups.com> Cancel-Lock: sha1:0fWTa+e84pNlXdrIj6e3Q9/uWDA= Xref: csiph.com comp.lang.java.programmer:17791 On 13/08/2012 18:22, Sax@DIST allegedly wrote: > Hi markspace, > thanks a lot for the two links you suggested me about the Java Programming Language! 0;-) > > The first one, about the "correct" usage of JComboBox itself, is very detailed and I think I can find what I need, reading the lots of examples. > > The second one, about the database connection, will be very useful for me, as I need to get the data for the items of my JComboBox from a DataBase Table. > > After all, my needs are very simple: > (o) to read some records (fields required: 'IDentifier' and 'Description') from a DB-Table > (o) to show the 'Description' of the records as Items of the JComboBox > (o) to do something else with the 'IDentifier' of the selected Item within the JComboBox > > Thanks again for your precious help, > Greetings. > Roberto > 08-) Hi Roberto, You should create a custom data structure containing the data you need (identifier and representation, from the looks of it), add them to the JComboBox's contents, and use a custom renderer (). That way, the _items_ in the combo box will always be your custom data structure instances (which you can use in #setSelectedItem(Object), for instance; or returned by #getSelectedItem()), and the renderer controls how they're displayed. In addition -- I believe I've already seen something like this coded somewhere -- you can write your own custom model () and have that hook directly to the database, possibly with on-demand, lightweight loading of data. HTH, -- DF.