Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.

This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.

Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".

A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.

Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.

like image 625
Nick Fortescue Avatar asked Jun 01 '11 17:06

Nick Fortescue


People also ask

What is radio button in Java Swing?

Radio button is use to select one option from multiple options. It is used in filling forms, online objective papers and quiz. We add radio buttons in a ButtonGroup so that we can select only one radio button at a time. We use “ButtonGroup” class to create a ButtonGroup and add radio button in a group.

What is the purpose of JRadioButton class?

Class JRadioButton. An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected.

How do you add a radio button in JOptionPane?

the methods in JOptionPane take a parameter "message" of type Object. You can provide a JPanel containing a message string and radio buttons and whatever else. It will be displayed above the buttons, next to the icon (if there is one).


1 Answers

What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.

like image 157
Rom1 Avatar answered Sep 21 '22 09:09

Rom1