How to give JCheckBox a feel of Button?

Screenshot of JCheckBox having JButton feel
JCheckBox doesn't look like a JButton. Yes, of course, I do agree. But did you remember that JCheckBox is after all a sub-class of the AbstractButton and that this beautiful class has the setBorderPainted() with which we can play and do the thing like a mad kid?


import javax.swing.*;
import java.awt.*;
class JCheckBoxButton extends JFrame
{
JCheckBox jc;

    public JCheckBoxButton()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JCheckBox Content Area Filled");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
       
        jc=new JCheckBox("Check/Uncheck me");
       
        // Quite big
        jc.setFont(new Font("Arial",Font.PLAIN,15));
       
        // Make it quite broad
        jc.setMargin(new Insets(5,5,5,5));
       
        // Set border painted!!
        jc.setBorderPainted(true);
       
        // Focus shouldn't be painted
        jc.setFocusPainted(false);
       
        // Set foreground, background
        jc.setForeground(Color.WHITE);
        jc.setBackground(Color.GRAY);
       
        // Add JCheckBox
        add(jc);
       
        setSize(400,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        new JCheckBoxButton();
    }
}

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

“Let there be no purpose in friendship save the deepening of the spirit.” - Kahlil Gibran


Posted by at on

Tags: Swing Hacks,