From COMBO to LABEL

Hi,

Yet another question from myself (swt), i am stuck once again!

I have my program half working.

i have entered deatils through a form into an arraylist so the players details are stored.

when i click a players name i want there age to be automatically be put into the next text box called age (retrieved from arraylist), but my problem is i can only get the last inputted age for which ever player i choose.

Here is my code:

//Player Inputted To Combo

for (int i = 0; i < playerList.size(); i++){

final Player result = (Player) playerList.get(i);

String[] data ={ result.getName()};

for (int j = 0; j < data.length; j++){

nameCombo.add(data[j]);

}

}

//Action Performed

nameCombo.addSelectionListener(new SelectionListener(){

publicvoid widgetSelected(SelectionEvent event){

for (int k = 0; k < playerList.size(); k++){

Player pAge = (Player) playerList.get(k);

String data2 = pAge.getAge();

label.setText(data2);

}

}

[1783 byte] By [Symb0la] at [2007-11-26 15:24:03]
# 1
Let me see if I understand what you're after.You have an ArrayList of players the user selects a player from a drop down and you want to populate one or more fields in a form with information relating to the selected player.Have I got that right?PS.
puckstopper31a at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 2

> Let me see if I understand what you're after.

>

> You have an ArrayList of players the user selects a

> player from a drop down and you want to populate one

> or more fields in a form with information relating to

> the selected player.

>

> Have I got that right?

>

> PS.

Thanks for your reply.

I first of all have no players, they get inputted into the system (arraylist) with their credentials, its after that in a new shell when i want to input a result, i want to be able to select a player from the drop down list (previously created), then from their previous data entry a text box to be populated with his/her age.

so yes you are correct.

Thank, hope you can help! (Please)

Symb0la at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 3

So what is nameCombo? if is a JComboBox, add a ActionListener

nameCombo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

JComboBox cb = (JComboBox)ae.getSource();

String p = (String)cb.getSelectedItem();

label.setText(p);

} });

SomeoneElsea at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 4

> So what is nameCombo? if is a JComboBox, add a

> ActionListener

>

> > nameCombo.addActionListener(new ActionListener() {

> public void actionPerformed(ActionEvent

> ae) {

> JComboBox cb =

> (JComboBox)ae.getSource();

> String p =

> (String)cb.getSelectedItem();

> label.setText(p);

> e]

it is done in swt in the action performed method.

i think the same principiles apply in action performed and action listener.

nameCombo is a Combo.

How does [code]String p = (String)cb.getSelectedItem();"

get the associated age with each player from the arraylist?

Thanks once again for your input. :)

Symb0la at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 5

I did not make the Player class on my system, so This was a example that you need to modify. yours would be like this

nameCombo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

JComboBox cb = (JComboBox)ae.getSource();

Player p = (Player)cb.getSelectedItem();

label.setText(p.getAge());

} });

~Tim

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 6

Regardless of how they've gotten there you have a list of people, and you've now gotten them into a combo box and someone presumably you has picked one of them from the combo.

1) add an action listener to the combo.

2) use the information from the combo (selected item) to find the right one in the list

3) once you've found the right one in the list pluck up their information from the one found in the list and display it on the screen.

This is what you're after right?

PS.

puckstopper31a at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 7

> Regardless of how they've gotten there you have a

> list of people, and you've now gotten them into a

> combo box and someone presumably you has picked one

> of them from the combo.

>

> 1) add an action listener to the combo.

> 2) use the information from the combo (selected item)

> to find the right one in the list

> 3) once you've found the right one in the list pluck

> up their information from the one found in the list

> and display it on the screen.

>

> This is what you're after right?

>

> PS.

Yes.

1) Understand.

2) How do i find the right one from the selection?

3) How do i pluck out the information?

Thank you.

Hope you can help.

Symb0la at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 8

> 1) Understand.

>

> 2) How do i find the right one from the selection?

The ActionEvent has a getSource() method that return the Object that fired the event, in this case it is the ComboBox. Since it is returned as an Object, you have to cast it to ComboBox (or JComboBox, depending on awt or swing) . The combobox has a getSelectedItem() method, that will return the object selected. Cast this to the type of object you stored (Player, I believe, in your case)

>

> 3) How do i pluck out the information?

Now that you have a Player object, call it's getAge() method, and set the text of the label to that value.

>

> Thank you.

>

You're Welcome

> Hope you can help.

~Tim

SomeoneElsea at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 9

> > 1) Understand.

> >

> > 2) How do i find the right one from the

> selection?

>

>

> The ActionEvent has a getSource() method that return

> the Object that fired the event, in this case it is

> the ComboBox. Since it is returned as an Object, you

> have to cast it to ComboBox (or JComboBox, depending

> on awt or swing) . The combobox has a

> getSelectedItem() method, that will return the object

> selected. Cast this to the type of object you stored

> (Player, I believe, in your case)

>

>

>

>

> >

> > 3) How do i pluck out the information?

>

> Now that you have a Player object, call it's getAge()

> method, and set the text of the label to that value.

>

> >

> > Thank you.

> >

> You're Welcome

>

> > Hope you can help.

>

>

> ~Tim

Could this translate to SWT? :(

Please say it can, please please please!

Thanks for you wisdom.

Symb0la at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...
# 10
I've never used swt, but I am sure it is possible. The problem in your original code is that you loop the the entire player list, changing the label each time through. You need to get the Selected item's index, and use that to get the player object and get the age.~Tim
SomeoneElsea at 2007-7-8 21:39:25 > top of Java-index,Java Essentials,Java Programming...