Results 1 to 6 of 6
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default freeze radioButton without disable them

    Hi

    I want to use Radio buttons for display , view only. I can I set that the radio button cannot be set or unset by user, beside setEnabled(false) .

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

    Default

    I don't know if there is a "proper" way to do this, but it can be done by,
    1) removing all mouse listeners from the radio button so it can't respond to clicks, and
    2) make the radio button not allow focus so it can't respond to space bar presses.

    For instance:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class FuSwing
    {
      private static void createAndShowGUI()
      {
        final JRadioButton radioButton = new JRadioButton("Radio Button");
    
        // so it can't respond to clicks:
        MouseListener[] listeners = radioButton.getMouseListeners();
        for (MouseListener listener : listeners)
        {
          radioButton.removeMouseListener(listener);
        }
    
        // so it can't respond to space-bar
        radioButton.setFocusable(false);
        
        // but thte radiobutton can still respond to our commands
        JButton toggleRadioBtn = new JButton("Toggle Radio Button");
        toggleRadioBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent arg0)
          {
            radioButton.setSelected(!radioButton.isSelected());
          }
        });
        
        JPanel panel = new JPanel();
        panel.add(radioButton);
        panel.add(toggleRadioBtn);
        
        JFrame frame = new JFrame("FuSwing Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
    
      }
    }

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

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test {
        private JPanel getContent() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            for(int i = 0; i < 4; i++) {
                JRadioButton rb = new JRadioButton(String.valueOf(i));
                rb.setFocusable(false);
                panel.add(rb, gbc);
            }
            JPanel parent = new JPanel();
            OverlayLayout overlay = new OverlayLayout(parent);
            parent.setLayout(overlay);
            parent.add(new ClearShield());
            parent.add(panel);
            return parent;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Test().getContent());
            f.setSize(300,100);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class ClearShield extends JComponent {
        public ClearShield() {
            long eventsToEnable = AWTEvent.MOUSE_EVENT_MASK |
                                  AWTEvent.MOUSE_MOTION_EVENT_MASK;
            enableEvents(eventsToEnable);
        }
    
        protected void processMouseEvent(MouseEvent e) {
            e.consume();        
        }
    
        protected void processMouseMotionEvent(MouseEvent e) {
            e.consume();
        }
    }

  4. #4
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Thanks

    By the way , I found another way.

    Save the status in a boolean variable and add AcrionListener : when ever ActioPerformed - set the radio button - to the old status

  5. #5
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > By the way , I found another way.

    the simple way is to add the radioButton to a ButtonGroup, so it is the only member

    JRadioButton rb = new JRadioButton("RB",true);
    ButtonGroup bg = new ButtonGroup();
    bg.add(rb);

  6. #6
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    the simple way is to add the radioButton to a ButtonGroup, so it is the only member
    That only works if you need the button to be "seleced" all the time. for not-selected I don't think it works.

    Nevertheless, very cool idea

Similar Threads

  1. Disable the WebBrowser ToolBar
    By Deepa in forum New To Java
    Replies: 0
    Last Post: 03-04-2009, 01:03 PM
  2. RadioButton
    By tiger100plus in forum New To Java
    Replies: 1
    Last Post: 01-05-2009, 08:04 PM
  3. How to disable the form
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-05-2008, 02:00 PM
  4. radiobutton selection
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:58 AM
  5. Replies: 3
    Last Post: 06-07-2007, 05:52 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
  •