Skip to content

UI not visible when using SWT with default option -XstartOnFirstThread on macOS #2252

Description

@elsazac

Describe the bug
UI of the Java application doesn’t show up when -XstartOnFirstThread is enabled (This option is enabled by default for SWT)

To Reproduce
Run the below snippet with :

Use the -XstartOnFirstThread arguement when launching with swtoption enabled (This is enabled by default); The UI doesn't show up; When the option is disabled, the UI shows up

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;
public class HtmlHeaderLafScrollTest {
    private static JFrame frame;
    private static JTable table;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(HtmlHeaderLafScrollTest::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        frame = new JFrame("Swing HTML Header + Scrollbars + L&F Switcher");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 400);
        frame.setLocationRelativeTo(null);
        // Top panel with Look and Feel selector
        JPanel topPanel = new JPanel();
        JComboBox<String> lafSelector = new JComboBox<>(new String[] {
            "System (Aqua on macOS)",
            "Nimbus",
            "Metal"
        });
        JButton applyButton = new JButton("Apply Look & Feel");
        applyButton.addActionListener((ActionEvent e) -> {
            String selected = (String) lafSelector.getSelectedItem();
            try {
                switch (selected) {
                    case "Nimbus":
                        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                        break;
                    case "Metal":
                        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                        break;
                    default:
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        break;
                }
                SwingUtilities.updateComponentTreeUI(frame);
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(frame, "Failed to apply Look and Feel.");
            }
        });
        topPanel.add(new JLabel("Select Look & Feel:"));
        topPanel.add(lafSelector);
        topPanel.add(applyButton);
        // Column headers using HTML
        String[] columnNames = {
            "<html>ID</html>",
            "<html>Name<br>(First Last)</html>",
            "<html>Age</html>",
            "<html>Occupation<br>(Job Title)</html>",
            "<html>Department<br>(Short Name)</html>",
            "<html>Country</html>",
            "<html>Hire Date<br>(YYYY-MM-DD)</html>"
        };
        // Create data with 50 rows
        Object[][] data = new Object[50][columnNames.length];
        for (int i = 0; i < 50; i++) {
            data[i][0] = i + 1;
            data[i][1] = "Employee " + (i + 1);
            data[i][2] = 25 + (i % 10);
            data[i][3] = "Job Title " + (i % 5);
            data[i][4] = "Dept " + (i % 3);
            data[i][5] = "Country " + (i % 4);
            data[i][6] = "2021-01-" + String.format("%02d", (i % 28 + 1));
        }
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model);
        // Enable horizontal scrollbars by forcing wider column widths
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setPreferredWidth(150);
        }
        JScrollPane scrollPane = new JScrollPane(table,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        frame.setLayout(new BorderLayout());
        frame.add(topPanel, BorderLayout.NORTH);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

Expected behavior
The UI should show up even when the default enabled option Use the -XstartOnFirstThread arguement when launching with swt is present.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)

  2. JRE/JDK version

Workaround (or) Additional context
No

Metadata

Metadata

Assignees

No one assigned

    Labels

    macOShappens on macOS

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions