Skip to main content

Jcombobox Listeners in swing

3 replies [Last post]
sarithau
Offline
Joined: 2010-02-22
Points: 0

Hi,

I want some info regarding the listeners for Jcombobox component in swing..

In my application i have a combo box with 3 values.
EX:Budgetary,Formal,Other..If user selects Budgetary option i need to show some popup message..

I have done with using adding actionlistener to the Combobox..Its working..But iam facing some problem..

After selecting this Budgetary opation user will get some popup message and saves the application.This data is saving in Xml..

After saving i will go to another page and do some work.. after that if i will come back to same page(which i had worked previously) as soon as i select that page ,previous saved data will be displayed...As soon as i select the page that popup message is coming..I don't want that..if user change the combobox value(budgetary) then only i want the popup message..

How to do this ..Please help onthis..here iam attaching my code:::

i_objProposalCombo.addActionListener(

new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
OSASession.setIsSavedStatus(false);
String str_proposalValue=i_objProposalCombo.getSelectedItem().toString();
if(str_proposalValue=="Budgetary")
{
JOptionPane.showMessageDialog(null,"Service Information and Core Services sections", "Information",JOptionPane.INFORMATION_MESSAGE);
}

}
}
);

Please help me out on this problem..

Thanks in advance..

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anthony_p
Offline
Joined: 2006-07-23
Points: 0

Read this:

http://java.sun.com/javase/6/docs/api/java/awt/ItemSelectable.html#addItemListener(java.awt.event.ItemListener)

Especially the part stating that: "Item events are not sent when an item's state is set programmatically." You need to use this listener instead of the ActionListener.

PS. Please use the [ code ] and [ /code ] tags (w/o spaces, lowercase) when posting code snippets.

sarithau
Offline
Joined: 2010-02-22
Points: 0

Hi
Thanks you so much for you reply..
I had used ItemListener also..This doesn't help me in my situation...

As soon as i enter into the the the popup message is coming..After that the page is displaying if the combobox vlaue is "Badgetary"..I don't want this popup message...

The page just display the information..If i change the combobox value to" badgetary" then only the popup message should come...

Please tell me which listener and and how to do this........

Thanks

anthony_p
Offline
Joined: 2006-07-23
Points: 0

Here's a problem, by the way:

[code]
if(str_proposalValue=="Budgetary")
[/code]

See [1] for an explanation.

Well, regarding your issue, how do you initialize your combobox and its model? Have you followed the Swing Combobox tutorial at [2] to write your code?

[1] http://leepoint.net/notes-java/data/strings/12stringcomparison.html

[2] http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html