Steve McLeod
Steve McLeod

Reputation: 52458

How do I change the colour of the focus ring in a JCheckBox?

Our Swing GUI has a black panel with white controls. However the JCheckBox instance on the panel always shows the focus ring in black. It seems to ignore the foreground colour when rendering the focus ring. Here's an example, where I've set the content pane background to grey so that the focus ring is visible:

enter image description here

Here's the code I'm using:

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

public class ScratchSpace {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JCheckBox checkBox = new JCheckBox("Hello cruel world");
                checkBox.setForeground(Color.WHITE);
                checkBox.setOpaque(false);

                JPanel contentPane = new JPanel();
                contentPane.setOpaque(true);
                contentPane.setBackground(new Color(0.5f, 0.5f, 0.5f));
                contentPane.add(checkBox);

                JFrame frame = new JFrame();
                frame.setContentPane(contentPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

How can I tell a JCheckBox to render the focus ring in a specific colour? Ideally it would use the control's foreground colour.

Upvotes: 2

Views: 818

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

You could try changing the look and feel property CheckBox.focus, note, doing this will effect ALL JCheckBoxs...

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCheckBox {

    public static void main(String[] args) {
        new TestCheckBox();
    }

    public TestCheckBox() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                UIManager.put("CheckBox.focus", Color.RED);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new JCheckBox("Hello world"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 4

Related Questions