Click to See Complete Forum and Search --> : JComboBox used to update a JLabel


Tehee
01-07-2006, 03:48 PM
Hi Folks,

I have a Combo box, with a list of contact names... When I select a name from the combo box it currently prints the selected name into my JAva Console. So I know the actionPerfomed and SetActionCommand etc... are all wired up correctly.

What I would like it to do is rather than print the name to the console. I'd like it to update a JLabel. (that is currently, intentionally blank, for this purpose)

Heres some code snippets that I hope help you understand what I have done so far.


**THIS IS FROM MY CONTROLLER CLASS**

public synchronized void actionPerformed (ActionEvent event) {

String command = event.getActionCommand();

JLabel nameFillLabel = new JLabel(); // not sure why put this here

if(command.equals("recipientName")){
JComboBox cb = (JComboBox)event.getSource();
String combName = (String)cb.getSelectedItem();
System.out.println(combName); // prints selected item to console
nameFillLabel.setText(combName); // doesnt actually do anything
}

}



***THIS IS FROM MessagerUI class***

private JComboBox recipientCombo = null;
private JLabel nameFillLabel = null;
...
recipientCombo = new JComboBox(names);
nameFillLabel = new JLabel();
...
recipientCombo = new JComboBox(names);
recipientCombo.setActionCommand("recipientName");
...

public void setActionListener(ActionListener sendActionEventsHere){
recipientCombo.addActionListener(sendActionEventsH ere);

}


I'm new to this and I think to problem may have something to do with me trying to update a JLabel thats contained within another class, if so how do I resolve this Issue, Any advice would be greatly appreciated.

Thanks in Advance

Regards

Terry

Waylander
01-09-2006, 08:39 AM
The most straight forward solution i see for your problem is to just add a public setter method to your User interface class and call it instead of the system print line.

I dont know which of your objects create what so im just going to call the setter with an obvious naming convention so you get what i mean, if you want more help communicating between classes then you will probably have to paste a little more code.



public synchronized void actionPerformed (ActionEvent event)
{

String command = event.getActionCommand();

if(command.equals("recipientName"))
{
JComboBox cb = (JComboBox)event.getSource();
String combName = (String)cb.getSelectedItem();

yourInstantiatedMessagerUIObject.setLabelText(combName);
}
}





private JComboBox recipientCombo = null;
private JLabel nameFillLabel = null;
...
recipientCombo = new JComboBox(names);
nameFillLabel = new JLabel();
...
recipientCombo = new JComboBox(names);
recipientCombo.setActionCommand("recipientName");
...

public void setActionListener(ActionListener sendActionEventsHere)
{
recipientCombo.addActionListener(sendActionEventsH ere);
}

public void setLabelText(String value)
{
nameFillLabel.setText(value);
}