Wrong JCheckBox painting in pressed state with custom icons

I’m trying to properly render and edit boolean values in my JTable

Generally, it works fine

However, there are a few things that bother me

One of them is this. To check my check box, you need to press the left mouse button. To uncheck it, you need to release it

Since it doesn’t really have to do with renderers or editors, my MRE is a simple frame with a check box containing panel

// snippet 1

package demos.button;

import di.Icons;

import javax.swing.*;
import java.awt.*;

public class CheckBoxDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Check Box demo");
        JPanel mainPanel = createMainPanel();
        frame.setContentPane(mainPanel);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    private static JPanel createMainPanel() {
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.CENTER);
        JPanel mainPanel = new JPanel(layout);
        mainPanel.add(createCheckBox());
        return mainPanel;
    }

    private static JCheckBox createCheckBox() {
        JCheckBox checkBox = new JCheckBox();
        checkBox.setIcon(Icons.unchecked());
        checkBox.setSelectedIcon(Icons.checked());
        return checkBox;
    }
}
// snippet 2

package di;

import util.IconUtil;

import javax.swing.*;

public class Icons {
    private Icons() {
    }
    public static Icon checked() {
        return IconUtil.findIcon("/checked.gif").orElse(null);
    }
    public static Icon unchecked() {
        return IconUtil.findIcon("/unchecked.gif").orElse(null);
    }
}
// snippet 3

package util;

import javax.swing.*;
import java.net.URL;
import java.util.Optional;

public class IconUtil {
    private IconUtil() {
    }

    public static Optional<Icon> findIcon(String path) {
        URL iconUrl = IconUtil.class.getResource(path);
        return Optional.ofNullable(iconUrl).map(ImageIcon::new);
    }
}

checked.gif
checked icon
unchecked.gif
unchecked icon

It’s not reproducible if I don’t set custom icons

// snippet 4

    private static JCheckBox createCheckBox() {
        JCheckBox checkBox = new JCheckBox();
        return checkBox;
    }

It’s all the more surprising as the documentation for setIcon() clearly says

Sets the button’s default icon. This icon is also used as the “pressed” and “disabled” icon if there is no explicitly set pressed icon.

Since I set the unchecked icon as the default icon, it should be displayed whenever the checkbox is in the pressed state

However, the selected icon is

You may set the borderPainted property to true to see it even more clearly. It’s always visually selected when it’s pressed

// snippet 5

    private static JCheckBox createCheckBox() {
        JCheckBox checkBox = new JCheckBox();
        checkBox.setIcon(Icons.unchecked());
        checkBox.setSelectedIcon(Icons.checked());
        checkBox.setBorderPainted(true);
        return checkBox;
    }

It’s as if I invoked

// snippet 6

        checkBox.setIcon(Icons.checked());

instead of

// snippet 7

        checkBox.setIcon(Icons.unchecked());

check box demo: pressed icon is selected

It’s interesting and I encourage you to share your take on this weird bug, but at the end of the day what I really want is this:

  1. Once pressed, keep my custom icon (checked or unchecked)
  2. But make the check box look “pressed” (paint the inner shadow border)

Basically, the same thing that happens in a default JCheckBox (see snippet 4), but with a custom icon

New contributor

Heneh 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