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) .
Printable View
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) .
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:
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();
}
});
}
}
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();
}
}
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
> 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);
That only works if you need the button to be "seleced" all the time. for not-selected I don't think it works.Quote:
the simple way is to add the radioButton to a ButtonGroup, so it is the only member
Nevertheless, very cool idea