How to get Option selected from JOption Button

Ive group 2 options in a JOption Button Group named group1 (see attached code).  I want to get and print the text of the option that was selection.  In the code the text name of the options are "Deliver" and "Pickup".  How do I do that?
transport1 = new JRadioButton("Deliver");
		transport1.setMnemonic(KeyEvent.VK_D);
		transport1.setSelected(true);
		transport1.addActionListener(alp);
		transport2 = new JRadioButton("Pick Up");
		transport2.setMnemonic(KeyEvent.VK_P);
		transport2.addActionListener(alp);
		ButtonGroup group1 = new ButtonGroup();
		group1.add(transport1);
		group1.add(transport2);

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
Are you sure your ButtonGroup is accessible at the point of group1.getSeection()...
I see you are declariing it afgaian in the same place as instatiationg - it is not instance, but local:
ButtonGroup group1 = new ButtonGroup();
0
 
for_yanCommented:

http://leepoint.net/notes-java/GUI/components/50radio_buttons/25radiobuttons.html

Common JRadioButton methods
b =       rb.isSelected();       Returns true if that button is selected.
       rb.setSelected(b);       Sets selected status of a radio button to b (true/false).
       rb.addActionListener(an-action-listener);       Adds an action listener to the radio button. The action listener will be called if button is selected.
       rb.addItemListener(an-item-listener);       Add an item listener to a radio button. The item listener will be called if the button is selected or deselected.

Common ButtonGroup methods
       bgroup.add(rb);       Adds a button to a radio button group.
rb =       bgroup.getSelectedJRadioButton();       Returns the radio button which is currently selected.
       bgroup.setSelectedJRadioButton(rb);       Sets the status of a particular radio button (unsetting others).
0
 
for_yanCommented:
if(group1.getSelectedRadioButton().equals(transport1) {
//do something

}
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
for_yanCommented:
forgot one parenthesis:

if(group1.getSelectedRadioButton().equals(transport1) ){
//do something

}
0
 
for_yanCommented:
I'm not sure that this method works (although those guys are writing it)

group1.getSelectedRadioButton().

however this method

if(transport1.isSelected()) {


}
is working fine - as I tested




0
 
Vanavah EdwardsAuthor Commented:
I used your code but changed it to
String rb = group1.getSelectedJRadioButton();
so as to get the option text.  But I am getting a red error on -->> getSelectedJRadioButton();
Which reads -->> The method getSelectedJRadioButton() is undefined for the type ButtonGroup
My interest is not testing for the option, I know how to do this easily but to get the text straight without using condition statements.
0
 
for_yanCommented:
As i mentioned above
this method does not work:
String rb = group1.getSelectedJRadioButton();

YOu anyway have to test, so I don't think it makes mauch sense trying to get the string

   This also works for me:
  ButtonModel bm = bgroup.getSelection();
      if(bm.equals(transport1.getModel())) System.out.println("selected");
    but it is just a little but more complex

 
0
 
for_yanCommented:
Also this will work and will return KeyEvent.VK_D or whatever button is seleceted:
   int in  = bgroup.getSelection().getMnemonic();
0
 
for_yanCommented:

If you setActionCmmand:
   transport1.setActionCommand("Deliver");
then this :
String s =  bgroup.getSelection().getActionCommand();
will retrurn string "Deliver"

So use one of these methods - you can somehow get to Radiobtton object itselfe, but
it is not worth fighting.

Swing is amazingly overcomplicated in this respect
0
 
for_yanCommented:

Basically, I don't tink you may want anything more than this:
    if(bgroup.getSelection().equals(transport1.getModel()))  System.out.println("selected");
and this is working
0
 
Vanavah EdwardsAuthor Commented:
I set as you suggested -->>
transport1.setActionCommand("Deliver");
transport2.setActionCommand("Pickup");
Then I did -->>
String s = group1.getSelection().getActionCommand();
System.out.println(s);
But I got this ERROR --->>
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at insertNewMoccaRecord.<init>(insertNewMoccaRecord.java:58)
0
 
for_yanCommented:
Strange, I am getting back the string
0
 
Vanavah EdwardsAuthor Commented:
This is the full transport option code snippet
transport1 = new JRadioButton("Deliver");
		transport1.setMnemonic(KeyEvent.VK_D);
		transport1.setSelected(true);
		transport1.addActionListener(alp);
		transport2 = new JRadioButton("Pickup");
		transport2.setMnemonic(KeyEvent.VK_P);
		transport2.addActionListener(alp);
		transport1.setActionCommand("Deliver");
		transport2.setActionCommand("Pickup");

		ButtonGroup group1 = new ButtonGroup();
		group1.add(transport1);
		group1.add(transport2);
		transport1.addFocusListener(fe);

Open in new window

0
 
for_yanConnect With a Mentor Commented:
no, - where do you do this

String s = group1.getSelection().getActionCommand();

Thsi is probably in some other methods.
Make sure group1 is instance variable of the calss
0
 
Vanavah EdwardsAuthor Commented:
You were right I was double declaring the buttongroup and it is working.  I am stil checking.
0
 
for_yanConnect With a Mentor Commented:
Declare always all visual elements as instance variable and don't declare them anywhere agin
rather than at the toop of the class  - it is not considered to be a good practice - but who cares?
Otherwise you spend half of your time chasing the null componenets which you declared second time.

0
 
Vanavah EdwardsAuthor Commented:
I learnt a lot from you.  Thanks for your help.  My problem is solved.  Just these two lines of your code --->>
pst.setString(11,    group1.getSelection().getActionCommand());
pst.setString(12,    group2.getSelection().getActionCommand());
Replaced the attached snippet and I removed any unnecessary memory variables that was declared.
Those 2 lines of code was all I needed to insert the selected text into my database.  I will now give you the points
if (transport1.getActionCommand()=="Deliver") {
			    	mtransport = "Deliver";
			    }
				else {
			    	mtransport = "Pickup";
				}
			    if (cashCharge1.getActionCommand()=="Cash") {
			    	mcashCharge = "Cash";
			    }
				else {
			    	mcashCharge = "Charge";
				}

Open in new window

0
 
for_yanCommented:
But don't worry too much about lines of code - it is better to use 15 lines instead of 2 lines than spend another 15 minutes on that.
Unfortunately most programmers do not realize this simple truth.
0
 
Vanavah EdwardsAuthor Commented:
Great teacher.  Very knowledgeable.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.