I am trying to create a side bar for a simple program that I am working on. I decided to use JRadioButtons in a group, and customize their appearance, to make the side bar appear the way I want. I customize the radio button’s appearances by creating a custom UI.
The customized painting works fine for the first instance of a JRadioButton that has this UI applied to it. The rest of the buttons that has the same UI are not painted, even though the custom paintComponent() method is called for each and every one of them. Since the first JRadioButton actually is painted the way I want to, I don’t understand why the rest of them wouldn’t be.
public class Side_Bar_Button_UI extends BasicRadioButtonUI {
private static final Side_Bar_Button_UI INSTANCE = new Side_Bar_Button_UI();
private static final int padding = 6;
private static final int padding_d = 12;
private static final int arc = 8;
public static ComponentUI createUI(JComponent c) {
return INSTANCE;
}
@Override
protected void installDefaults(AbstractButton b) {
super.installDefaults(b);
b.setBorder(new EmptyBorder(padding, padding, padding, padding));
}
@Override
public synchronized void paint(Graphics g, JComponent c) {
super.paint(g, c);
paintComponent(g, c);
}
private void paintComponent(Graphics g, JComponent c) {
System.out.println("paintComponent called on" + c);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = c.getX();
int y = c.getY();
int w = c.getWidth();
int h = c.getHeight();
// Paint background
g2.setColor(Colors.SIDE_PANEL);
g2.fillRect(x, y, w, h);
JRadioButton b = (JRadioButton) c;
// Paint focus if selected
if (b.isSelected()) {
g2.setColor(Colors.SIDE_PANEL_SELECTED);
g2.fillRoundRect(x+padding, y+padding, w-padding_d, h-padding_d, arc, arc);
}
// Paint icon
Icon icon = b.getIcon();
if (icon != null) {
icon.paintIcon(c, g2, x+padding_d, y+padding_d);
}
// Paint text
String text = b.getText();
if (text != null && !text.isEmpty()) {
FontMetrics fm = g2.getFontMetrics();
int text_height = fm.getHeight();
g2.setColor(b.getForeground());
g2.drawString(text, x + padding_d + icon.getIconWidth() + padding, y + h / 2 + text_height / 4);
}
g2.dispose();
}
@Override
protected void paintFocus(Graphics g, Rectangle textRect, Dimension size) {
// Removing focus paint
}
}
It’s worth mentioning that I am using the IntelliJ Swing GUI designer to create the panel in which these radio buttons reside, but it’s a certainty that this isn’t the problem as it is replicated in a normal Swing JPanel class.
public class Admin_Page {
private JPanel page;
private JPanel top_panel;
private JPanel side_panel;
private JRadioButton side_bar_button_1;
private JRadioButton side_bar_button_2;
private JRadioButton side_bar_button_3;
private JRadioButton side_bar_button_4;
private JButton button1;
private JPanel side_bar_button_panel;
private ButtonGroup side_bar_button_group;
public Admin_Page() {
// createUIComponents() automatically called
}
public JPanel get_page() {
return page;
}
private void createUIComponents() {
side_bar_button_1 = new JRadioButton();
side_bar_button_1.setUI(new Side_Bar_Button_UI());
side_bar_button_1.setIcon(new ImageIcon("resources/icons/dashboard_icon_24x24.png"));
side_bar_button_2 = new JRadioButton();
side_bar_button_2.setUI(new Side_Bar_Button_UI());
side_bar_button_2.setIcon(new ImageIcon("resources/icons/category_icon_24x24.png"));
side_bar_button_3 = new JRadioButton();
side_bar_button_3.setUI(new Side_Bar_Button_UI());
side_bar_button_3.setIcon(new ImageIcon("resources/icons/trolley_icon_24x24.png"));
side_bar_button_4 = new JRadioButton();
side_bar_button_4.setUI(new Side_Bar_Button_UI());
side_bar_button_4.setIcon(new ImageIcon("resources/icons/profiles_icon_24x24.png"));
}
}
When I didn’t get this to work, I tried creating a custom JRadioButton by extending it and overriding it’s paintComponent() method, but this approach resulted in the very same problem that I am having by setting the UI to the JRadioButton instances.
I have read a lot online to try and figure out what is going wrong here, but haven’t been able to get anywhere with this for countless hours. Any help would be much appreciated.
gocode_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.