Raymond Zheng
Raymond Zheng

Reputation: 119

JRadiobutton selection not showing

Java: I have a 3 JRadioButtons added to a JPanel, which is added to a GraphicsProgram. When I Run the program, I can select the radiobuttons (when I click the buttons, they do function). However, the JPanel doesn't show the new selection. In other words, even though the selections WORK, they don't show.

Even when I add the radio buttons to a ButtonGroup, it doesn't work. I tried repaint() for the JPanel toolbar, but it still doesn't work.

A link to a screenshot of the applet while it is running. The Radio buttons are stuck that way. When I click "Pellets" or "Gate", it still shows that "Walls" is selected. However, even though the buttons don't SHOW the correct selection, they still ARE selected.

enter image description here

toolbar = new JPanel();

wallButton = new JRadioButton("Walls");
wallButton.setActionCommand("walls");
wallButton.setSelected(true);

pelletButton = new JRadioButton("Pellets");
pelletButton.setActionCommand("pellets");

gateButton = new JRadioButton("Gate");
gateButton.setActionCommand("gate");

toolbar.add(wallButton);
toolbar.add(pelletButton);
toolbar.add(gateButton);

wallButton.addActionListener(this);
pelletButton.addActionListener(this);
gateButton.addActionListener(this);

add(toolbar, SOUTH);

Upvotes: 0

Views: 1192

Answers (3)

Doga Oruc
Doga Oruc

Reputation: 816

You MUST call repaint() and revalidate() after a component is changed. Try to add these methods on your RadioButtons' action listeners.

Upvotes: 0

Thudani Hettimulla
Thudani Hettimulla

Reputation: 774

I think you have a layouting issue of your JRadioButtons on your toolbar panel. Try setting a LayoutManager (May be a new FlowLayout()) to the toolBar panel.

Upvotes: 0

Mitesh Pathak
Mitesh Pathak

Reputation: 1306

I think you are missing ButtonGroup

Add the following code before add():

ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(wallButton);
radioButtonGroup.add(pelletButton);
radioButtonGroup.add(gateButton);

Upvotes: 1

Related Questions