Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2008, 04:25 AM
Member
 
Join Date: Jan 2008
Posts: 16
newtojava7 is on a distinguished road
radiobutton selection
im using an action listener on a button, to say when it clicks the button, IF a radio button is selected then it should do something.

what is the proper way to tell it to do something only if the radiobutton is selected.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-17-2008, 04:58 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Depends on what you're doing and how you are putting things together.
1 — One way is to declare the JRadioButton(s) as member variable (in class scope) and, in the ActionListener, check to see if the radioButton is selected.
2 — Another is to declare a member variable boolean which is set in an ActionListener which has been added to the radio button and in which the boolean is set. Then check the state of this boolean in the button ActionListener.
Code:
class Pseudo implements ActionListener { JRadioButton radioButton; boolean isItTrue = false; Pseudo() { radioButton = new JRadioButton("..."); // 1 // No listener is needed for radioButton. // Its selection state is checked in the // ActionListener of the JButton. JRadioButton anotherRadio = new JRadioButton("..."); // 2 // Add an anonymous listener in which we set the // member variable boolean for each user action // on the "anotherRadio" radio button. anotherRadio.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JRadioButton rb = (JRadioButton)e.getSource(); isItTrue = rb.isSelected(); } }); JButton button = new JButton("..."); button.addActionListener(this); // mount and show components... } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if(radioButton.isSelected()) // one way to know... if(isItTrue) // another way to know... } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection sort in Java Java Tip Algorithms 0 04-15-2008 09:41 PM
validating selection list in struts! rameshraj Web Frameworks 2 02-13-2008 03:21 PM
Code for selection kneekow Eclipse 0 02-01-2008 05:10 PM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 08:36 AM


All times are GMT +3. The time now is 08:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org