Results 1 to 4 of 4
  1. #1
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default Setting selected JRadioButton in ButtonGroup

    I'm unfamiliar with working with GUIs and have found myself stuck on a small detail, but I have a Button Group that will have selected by default one of two JRadioButtons dependent on certain conditions. The following is the gist of what I have been able to dig up on my own, but it always seems to have the top button selected by default.

    Java Code:
    if(bool) {
    button1.setSelected(true);
    group.setSelected(button1.getModel(), true);
    } else {
    button2.setSelected(true);
    group.setSelected(button2.getModel(), true);
    }
    Thanks for any help.
    Thanks for any help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not clear on what you're doing and perhaps more importantly, when. When are you checking this boolean variable and trying to set the state of your radiobuttons? In an ActionListener?

    I recommend you create a small compilable program that has nothing but to JRadioButtons added to a JPanel and a ButtonGroup and see if you can reproduce this problem so that we can see it precisely for ourselves.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's what I mean about a simple example:
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RadioInfo2
    {
      private static boolean bool = true;
      private static JRadioButton button1 = new JRadioButton("Button 1");
      private static JRadioButton button2 = new JRadioButton("Button 2");
      
      private static void setRadioButton()
      {
        if (bool)
        {
          button1.setSelected(true);
        }
        else
        {
          button2.setSelected(true);
        }
      }
    
      private static void createAndShowUI()
      {
        ButtonGroup btnGrp = new ButtonGroup();
        
        JPanel mainPanel = new JPanel();
        btnGrp.add(button1);
        btnGrp.add(button2);
        
        JButton swapSelection = new JButton("Change Button");
        swapSelection.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            bool = !bool;
            setRadioButton();
          }
        });
    
        mainPanel.add(button1);
        mainPanel.add(button2);
        mainPanel.add(swapSelection);
        setRadioButton();
        
        JFrame frame = new JFrame("RadioInfo2");
        frame.getContentPane().add(mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    thanks. sorry for not responding earlier, but i found out the problem when i got in to work this morning. turns out that some older code uses the setSelected(boolean) method after the actual code i'm modifying. brain fart, yes. thanks again for the attention anyways.

    edit: sorry, meant that older code uses 'setSelected' to select the first button regardless of what i had selected before. but yeah, the point was to have a dialog where either button is already selected when it opens up. i was frustrated for quite some time yesterday but was stupid enough to not look forward in the code.
    Last edited by emceenugget; 01-29-2009 at 06:16 PM. Reason: clarification

Similar Threads

  1. JTable Gridline in selected row
    By arubin in forum AWT / Swing
    Replies: 11
    Last Post: 11-24-2009, 07:05 PM
  2. JTable problem with getting ROW selected
    By nadia in forum Advanced Java
    Replies: 2
    Last Post: 01-13-2009, 05:57 PM
  3. Regarding JTree with JCheckBox and JRadioButton
    By partha_adt in forum AWT / Swing
    Replies: 0
    Last Post: 11-11-2008, 10:03 AM
  4. How to remove selected items in a SWT table
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:29 PM
  5. JRadioButton set object color
    By King8654 in forum AWT / Swing
    Replies: 4
    Last Post: 04-24-2008, 09:52 AM

Posting Permissions

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