2

I can't seem to get simple toggle buttons working inside a JToolbar.

This example program adds three toggle buttons in a button group to a JToolbar. On macOS, currenty toggled button is only visible if the app looses focus.

focused

no focus

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

public class Foo extends JFrame {

    public Foo() {
        setTitle("Foo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JToolBar toolbar = new JToolBar();
        toolbar.setFloatable(false);
        add(toolbar, BorderLayout.NORTH);

        JToggleButton well = new JToggleButton("Well Done"); 
        JToggleButton medium = new JToggleButton("Medium");
        JToggleButton rare = new JToggleButton("Rare");

        toolbar.add(well);
        toolbar.add(medium);        
        toolbar.add(rare);

        ButtonGroup group = new ButtonGroup();
        group.add(well);
        group.add(medium);
        group.add(rare);

        setSize(400, 300);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
        } catch (ClassNotFoundException e) {   
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Foo().setVisible(true);
            }
        });
    }
}

Is there a known issue with such usage of toggle buttons on macOS? Any workarounds?

This is with macOS version 10.14.5, Oracle/OpenJDK JRE/JDK 1.8 and 11.

predi
  • 5,528
  • 32
  • 60

0 Answers0