Results 1 to 2 of 2
  1. #1
    newtojava7 is offline Member
    Join Date
    Jan 2008
    Posts
    16
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    Java 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...
        }
    }

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 07:23 AM
  2. Selection sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM
  3. validating selection list in struts!
    By rameshraj in forum Web Frameworks
    Replies: 2
    Last Post: 02-13-2008, 01:21 PM
  4. Code for selection
    By kneekow in forum Eclipse
    Replies: 0
    Last Post: 02-01-2008, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •