/    Sign up×
Community /Pin to ProfileBookmark

JComboBox used to update a JLabel

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

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@WaylanderJan 09.2006 — 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.


[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);
}
}

[/CODE]



[CODE]
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);
}
[/CODE]
×

Success!

Help @Tehee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...