Swing Components with custom UI are not painted correctly

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.

New contributor

gocode_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật