Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

help needed @ Listener to combo-box

Thread view: 
vikas talwar - 27 May 2006 14:28 GMT
Hi Everyone,

below is my code.  Please find my question between '?'ed lines.

//------------------------------------------------------------
// CREATE COMBO BOX
//------------------------------------------------------------
if(obj[i].equals(CSV_COMBO_BOX))
    {
    _combobox[_combobox_count] = new JComboBox();

    //get possible value for combo box
    String [] possible_values = get_possible_values(val[i]);
    for(int i=0; i < possible_values.length; ++i)
        _combobox[_combobox_count].addItem(possible_values[i]);

    //set the default value to usr_val[i]
    _combobox[_combobox_count].setSelectedItem(usr_val[i]);

    // add to panel
    panel.add("tab",_combobox[_combobox_count]);

    // listner
    _combobox[_combobox_count].addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e)
        {
                   // ?????????????????????????????????????

                   // ? I WANT TO UPDATE usr_val[i] here           ?
                   // ? how could i get the value of "i" or

                   //  _combo_box_counter in this block?????????
                   //  ???????????????????????????????<?????
        }
        });
    _combobox_count++;
    }

--
need your help
Vikas Talwar
Duane Evenson - 27 May 2006 15:34 GMT
> Hi Everyone,
>
[quoted text clipped - 33 lines]
>     _combobox_count++;
>      }

You can reference the selected value with
(JComboBox)e.getSource()).getSelectedItem() or 'i' with
(JComboBox)e.getSource()).getSelectedIndex()

and reference the _combo_box_counter with
(JComboBox)e.getSource()).getItemCount()

The key is to use e.getSource() and cast it to the event object's class -- in
this case, JComboBox.
Then you can use whatever methods available to that class you want.
vikas talwar - 27 May 2006 16:52 GMT
going to implement, i guess it'll work.

Thanks a lot.
vikas talwar - 27 May 2006 17:04 GMT
hi Duane,

I think you didnt' get what I wanted to ask ( maybe I was not clear)

(JComboBox)e.getSource()).getSelectedItem()       returns new selected
value in combo box
(JComboBox)e.getSource()).getSelectedIndex()     returns index of new
selected value in combo box
(JComboBox)e.getSource()).getItemCount()           don't what its
returning.

Please read my code again.
Piet71 - 28 May 2006 08:54 GMT
> hi Duane,
>
> I think you didnt' get what I wanted to ask ( maybe I was not clear)
The "maybe I was not clear" is a good start....

> (JComboBox)e.getSource()).getSelectedItem()       returns new selected
> value in combo box
[quoted text clipped - 4 lines]
>
> Please read my code again.
...but thats no a really good continuation. Do you think the question
is much clearer now?
Anyways, you have shown us a big "if" block where lots of things are
happening. If I understand your scenario correctly, you have two
arrays: one called "val" which is an array of string arrays that you
use to populate a JComboBox, and the other one is called "usr_val" that
holds strings and is used to set the JComboBox default values. You want
to change the default value when the selected item in the corresponding
JComboBox has changed. To manage your arrays and JComboBoxes, you have
one counter "i" which seems to count the arrays of string arrays and
the array with the default strings and one counter "_combobox_count"
which counts the JComboBoxes that have been genererated with the help
of these arrays. Obviously, the arrays could be used to generate a
different JComponent as well, in which case "i" and "_combobox_count"
will have different values.
Both "i" and "_combobox_count" are clearly available within the
if-block, so you don´t need any extra tricks to access them. What
happens if you directly access "i" or "_combobox_count" from within the
"itemStateChanged"-method?
HTH, PIet
vikas talwar - 28 May 2006 09:58 GMT
Hello Piet,

you hava understood my code( ur perfectly clear).

but value of 'i' and '_combobox_counter' is always chaning while
generating GUI interface.
and at the time of event genreration  the value of 'i' will be last
element of   JComponentArray, and '_combobox_counter' will be last
element comboBoxArray.
vikas talwar - 28 May 2006 10:00 GMT
.Hello Piet,

you understood my code well (gr8).

but value of 'i' and '_combobox_counter' is always chaning while
generating GUI interface.
and at the time of event genreration  the value of 'i' will be last
element of   JComponentArray, and '_combobox_counter' will be last
element comboBoxArray
Duane Evenson - 28 May 2006 12:39 GMT
> hi Duane,
>
[quoted text clipped - 8 lines]
>
> Please read my code again.

Oh, you just want to refer to variable in the outer class from within the
inner class, right?
Declare these variables as class variables, then they will be visible to
inner classes. You can refer to them inside the inner class as just "i"
and "_combobox_count".


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.