File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to get selected radioButton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get selected radioButton?" Watch "How to get selected radioButton?" New topic
Author

How to get selected radioButton?

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi all,

I'm trying to base a decision off of which JRadioButton from a buttonGroup (my buttonGroup is called sendReceiveGroup) is selected, and I'm having trouble. When I try to use the isSelected() or getSelection() methods, NetBeans is telling me that it sees no variable sendReceiveGroup, although I clearly have it in my init() statement. I wonder what gives? What I have so far is below, haven't run it yet, just trying to get the GUI straight first before getting deep into the FTP part. Thanks for any help you can provide!



Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Patrick Brooks:
Hi all,

I'm trying to base a decision off of which JRadioButton from a buttonGroup (my buttonGroup is called sendReceiveGroup) is selected, and I'm having trouble. When I try to use the isSelected() or getSelection() methods, NetBeans is telling me that it sees no variable sendReceiveGroup, although I clearly have it in my init() statement. I wonder what gives?


NetBeans is correct, in that sendReceiveGroup is a local variable which is out of scope outside the init() method. Two possible solutions:

1) Call send.isSelected() instead of sendReceiveGroup.isSelected(send) since send a class member variable, and hence in scope.

2) Make sendReceiveGroup a class member variable.

While I'm here, convention calls for class names to start with a capital letter. It's not a big deal, but it usually makes sense to follow convention. So consider renaming your class AppletGUI.

[edit: sendReceiveGroup.isSelected(send) also won't work because that method takes a ButtonModel, not a Button. It would have to be sendReceiveGroup.isSelected(send.getModel()).]
[ February 02, 2008: Message edited by: Brian Cole ]

bitguru blog
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Originally posted by Brian Cole:


NetBeans is correct, in that sendReceiveGroup is a local variable which is out of scope outside the init() method. Two possible solutions:

1) Call send.isSelected() instead of sendReceiveGroup.isSelected(send) since send a class member variable, and hence in scope.

2) Make sendReceiveGroup a class member variable.

While I'm here, convention calls for class names to start with a capital letter. It's not a big deal, but it usually makes sense to follow convention. So consider renaming your class AppletGUI.

[edit: sendReceiveGroup.isSelected(send) also won't work because that method takes a ButtonModel, not a Button. It would have to be sendReceiveGroup.isSelected(send.getModel()).]

[ February 02, 2008: Message edited by: Brian Cole ]


Yep, that did it. I didn't think about it, since I had declared everything else at the top of my program, oops. Thank you, sir!
 
GeeCON Prague 2014
 
subject: How to get selected radioButton?