1

How do you make a variable width toggle button displaying arbitrary width text (known only at runtime), with a border, which indicates state without the default dark blue gradient of a JToggleButton when toggled on on macOS High Sierra through Catalina? I would like something like the look and feel of the roundRect button type before High Sierra where the toggled on state retained its border and the background was a slightly darker grey. In the context of these buttons, several dark blue buttons is undesirable and distracting, as they indicate state, as opposed to indicating something requiring action.

Below is example code and results on macOS Sierra and Mojave. The first three buttons use the roundRect button type. The last two buttons use the default button type. The desired behavior is similar to how the three roundRect buttons appear on Sierra. The Mojave image is similar to the look and feel of High Sierra through Catalina. To me the third button (roundRect type button in the on state) on Mojave looks like isolated text, instead of a toggle button in the on state.

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

public class JTog {
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        final String[] names = new String[]{"rr off", "rr mousedown", "rr on", 
                                            "default off", "default on"};
        JToggleButton[] tb = new JToggleButton[5];

        for (int i = 0; names.length; i++) {
            tb[i] = new JToggleButton(names[i], (i==2)||(i==4));
            if (i < 3)
                tb[i].putClientProperty("JButton.buttonType","roundRect");
            tb[i].setFocusable(false);
            tb[i].setBorderPainted(true);
            frame.add(tb[i]);
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setBounds(200, 200, 300, 80);
                frame.setVisible(true);
            }
        });
    }
}

Result of example code on macOS Sierra. Result of example code on macOS Mojave.

I have tried using lineBorders, but have not been able to produce something nice looking or something which indicates state by a slight change in shade. I am not overly concerned with the look of the button when the mouse is down, but I included that state for completeness, and had the mouse button pressed on that button (second from the left) for the screenshots.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Frank
  • 11
  • 1

0 Answers0