Hi Freddy

A JComboBox manages objects not only Strings. It displays the value returned by the "toString()" method in each of the objects, but inside you can do whatever you want.

For e.g., define your own "ComboItem" class. For e.g.:

   public class ComboItem {
        private int index;
        private String value;

        public ComboItem( int i, String v ){
            index = i; value = v;
        }

        @Override
        public String toString(){
            return "" + index + " " + value;
        }

        public int getIndex(){ return index; }
   }

When you read the database, create a ComboItem for each record in the database then add the ComboItem to your JComboBox. For e.g.:

   ....
   JComboBox jcb = ...;
   ....
   ResultSet rs = ...;
   while ( rs.next()){
        ComboItem ci = new ComboItem( rs.getInteger("INDEXNO"),
   rs.getString("LABEL");
        jcb.addItem( ci );
   }
   ....

If you do this way, the JComboBox will display what "toString()" returns for each of the items. I.e. index, space, value.

When the user ends entering the data (including choosing a line in the JComboBox), you can get the object chosen by the user and the index number:

   ....
   ComboItem ci = (ComboItem)(jcb.getSelectedItem());
   int theIndexNumber = ci.getIndex();
   ....

Now, this is only one possible way to do it. Another way is to put all the ComboItem objects into a collection when they are read from the database, then fill the JComboBox based on the elements of the collection (meaning separation between the database access level and the user interface). And so on.

Hope it helps
Mihai





Le 07/11/2010 04:28, Shiv Shankar Prajapati a écrit :
Hi,
  Go through following link, it might help you
http://www.javabeginner.com/java-swing/java-jcombobox-class-example

Here JComboBox takes string array which we can build at run time in different ways. So fetch all the records and convert it in to String array and then pass it to the JComboBox.


On Sat, Nov 6, 2010 at 8:58 AM, Freddy Castelblanco Macias <thelords...@gmail.com <mailto:thelords...@gmail.com>> wrote:

    Hi people !!!

    Im working in Java with SQLJDBC and plain Files (secuencial,
    Indexed ... etc). But ... I have to make a GUI and in one control of
    Swing (JComboBox) I want to try to fill this control with information
    that i have in my database or with information of my plain file. But i
    want in one way:.... For example:

    //this is de actionPerformed of the jCombobox
    //so when im evaluating the content on this control i want capture the
    value and display in the GUI the information //of this VALUE..

    1  Coke
    2  Bread
    3  Lents

    The number is the value witch i want work and the words must be
    displayed in the GUI

    SO how can i do that???

    Help please !!!!

    Some told me one day, that this I can do whith a Collection, but
    How ??

    --
    To post to this group, send email to
    javaprogrammingwithpassion@googlegroups.com
    <mailto:javaprogrammingwithpassion@googlegroups.com>
    To unsubscribe from this group, send email to
    javaprogrammingwithpassion+unsubscr...@googlegroups.com
    <mailto:javaprogrammingwithpassion%2bunsubscr...@googlegroups.com>
    For more options, visit this group at
    http://groups.google.com/group/javaprogrammingwithpassion?hl=en




--
With Regards,

Shiv Shankar,
Persistent System Ltd.

--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en

--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to