I want to remove the background of my JRadioButton but still keeping the same look & feel. Images will speak by themselves:
When I do this code:
JRadioButton myJRadioButton = new JRadioButton("My JRadioButton");
add(myJRadioButton);
I get this:
And when I run it with this code:
JRadioButton myJRadioButton = new JRadioButton("My JRadioButton");
myJRadioButton.setForeground(Color.white); //To see it on the black background.
myJRadioButton.setOpaque(false);
add(myJRadioButton);
I get this:
I've something like a "star" instead of a great and beautiful circle. And what I want is to keep the great and beautiful circle of the first image but without the default background according to it.
You can create a class which extends the JRadioButton and add all the properties inside the class:
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
setForeground(Color.white);
setBackground(Color.BLACK);
public class Sample extends JPanel {
public Sample() {
super(new BorderLayout());
setBackground(Color.BLACK);
TransparentButton testButton = new TransparentButton("hello");
testButton.setSelected(true);
add(testButton, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Hello Word demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new Sample();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
class TransparentButton extends JRadioButton {
public TransparentButton(String string) {
super(string);
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
setForeground(Color.white);
setBackground(Color.BLACK);
}
}
}