Hi guys

Continuing with this forum, I fix what i Want like this:

1. I created my clase called clsComboItem with two properties: valueMember
and displayMember
2. With the Data of the result set and using a Collection, I load my combo,
my object of the class clsComboItem and my Collection like this:

    Previously at the global level of the GUI, i declared a Collection of
type clsComboItem, in this case is a vector:

   Vector<clsComboItem> Coleccion = new Vector<clsComboItem>();

   while (objConexion.ResultSetSQL.next())
            {
                objC = new clsComboItem();

objC.setIntIndice(objConexion.ResultSetSQL.getInt("Codigo"));

objC.setStrValorMostrar(objConexion.ResultSetSQL.getString("Nombre"));
                Coleccion.add(objC);
                jComboBox1.addItem(objC.getStrValorMostrar());
            }
when i want get the code of any data for example that in the DB is like
this:

1 Sandra
2 Freddy
3 Ibo

.
.
.
.
Etc,

3. i made like this:

private void capturarCampoSeleccionado()
    {
        String strClave;
        int theIndexNumber = 0;
        strClave = String.valueOf(jComboBox1.getSelectedItem());
        for (int i = 0 ; i < Coleccion.size(); i++)
        {
            if(Coleccion.get(i).getStrValorMostrar().equals(strClave))
            {
                theIndexNumber = Coleccion.get(i).getIntIndice();
                break;
            }
        }
        javax.swing.JOptionPane.showMessageDialog(null, "Datos seleccionado:
" + String.valueOf(theIndexNumber));
    }
So this was my solution.

I come from .Net, and this is the most similar way for capture data from the
database like i want,

Any suggestion?? any way for do this more efficient??

2010/11/7 Mihai DINCA <mihai.di...@free.fr>

> 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> 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
>> To unsubscribe from this group, send email to
>> javaprogrammingwithpassion+unsubscr...@googlegroups.com<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
>
>


-- 
Cordiamente,

Freddy Castelblanco Macías

-- 
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