• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get value from combo box and radio button?

 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I have a combobox name "cboYear", radio button "optYes" and a command button "btnExecute". When user clicks the Execute button, I want to collect the user input and store it into a variable. I know how to get value from text box by using getText method, but I don't know how to get value from radio button and combo box. Can someone tell me what method I should use?
I appreciate your help!
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can call e.getSource on the event, and that will return of your button. Radio buttons are each a different button. So in your actionPerformed method of your listener you can put:
JButton buttonPushed = (JButton) e.getSource();
if (buttonPushed.getName().equals( "blah" )) doBlahStuff;
etc.
Instead of using getName, you could also use getText, in case you have two buttons with the same text that do the same thing. You can also use action commands, in case you might want several buttons with different text to do the same thing. (or buttons with the same text to do different things...) This is useful if your program gets translated into a different language. In your container:

button.setActionCommand("getDataX");
button.setText("Push Me");
button.addActionListener(new DataButtonListener());
button2.setActionCommand("getDataX");
button2.setText("Enter");
button.addActionListener(new DataButtonListener());
and in your DataButtonListener:
if (buttonPushed.getActionCommand().equals( "getDataX" )) get(DataX);
Now both buttons will get dataX.
HTH
e

[This message has been edited by eric moon (edited January 25, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!