Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #17791

Re: JComboBox with IDentifiers

From Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: JComboBox with IDentifiers
Date 2012-08-13 18:49 +0200
Organization A noiseless patient Spider
Message-ID <k0bb6s$k60$1@dont-email.me> (permalink)
References <08a4e9e5-be46-4a15-94c4-e6bfab71d781@googlegroups.com> <2eae09a5-6d01-401a-acb0-cc54ba8b516e@googlegroups.com> <k097nl$1is$1@dont-email.me> <5a6762d4-5fb9-451d-86eb-98c26f3ef95a@googlegroups.com>

Show all headers | View raw


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
(<http://docs.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html#setRenderer(javax.swing.ListCellRenderer)>).
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
(<http://docs.oracle.com/javase/6/docs/api/javax/swing/ComboBoxModel.html>)
and have that hook directly to the database, possibly with on-demand,
lightweight loading of data.

HTH,
-- 
DF.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

JComboBox with IDentifiers Roberto Sagoleo <roberto.sagoleo@gmail.com> - 2012-08-10 14:41 -0700
  Re: JComboBox with IDentifiers markspace <-@.> - 2012-08-10 15:14 -0700
  Re: JComboBox with IDentifiers Lew <lewbloch@gmail.com> - 2012-08-10 15:26 -0700
  Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-12 14:03 -0700
    Re: JComboBox with IDentifiers markspace <-@.> - 2012-08-12 14:37 -0700
      Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-13 09:22 -0700
        Re: JComboBox with IDentifiers Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-08-13 18:49 +0200
  Re: JComboBox with IDentifiers Roedy Green <see_website@mindprod.com.invalid> - 2012-08-14 18:02 -0700
  Re: JComboBox with IDentifiers "Sax@DIST" <roberto.sagoleo@gmail.com> - 2012-08-22 03:30 -0700

csiph-web