help needed @ Listener to combo-box

V

vikas talwar

Hi Everyone,

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

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

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

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

// 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 here ?
// ? how could i get the value of "i" or

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

Duane Evenson

Hi Everyone,

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

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

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

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

// 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 here ?
// ? how could i get the value of "i" or

// _combo_box_counter in this block?????????
// ???????????????????????????????<?????
}
});
_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.
 
V

vikas talwar

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

Piet71

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
(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.
....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
 
V

vikas talwar

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

vikas talwar

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

Duane Evenson

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top