简体   繁体   中英

JFace error when setting JFrame visible: Cocoa AWT: Running on AppKit thread 0 when not expected

I'm trying to code my first JFrame for a simple app. The problem is that as soon as I uncomment the setVisible(true); I obtain the following error message:

Cocoa AWT: Running on AppKit thread 0 when not expected.

Config: Running eclipse on a mac OS 10.10 (Yosemite) and Java is up to date.

Here is the code:

package gui;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

    private JLabel appTitle;

    public MainFrame(){
        super("Tabum by Team Alpha");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,500);
        setLayout(new BorderLayout());
        setVisible(true);
    }
}

I've never used your tools, but I guessing that the problem is that all GUI code should execute on the Event Dispatch Thread (EDT) and your code is not doing this.

You do this by wrapping your code in a SwingUtilities.invokeLater(...):

EventQueue.invokeLater(new Runnable()
{
    public void run()
    {
        // add your code here
    }
});

Read the section from the Swing tutorial on Concurrency for more information.

I couldn't get the solution working with Eclipse on my Mac.

However, when I loaded the exact same solution in IntelliJ IDEA, it worked!

I would classify this as a Eclipse bug. For reference:

  • I'm using the current JDK 1.8.0_31
  • I was using Eclipse Kepler Service Release 2 (20140224-0627)
  • I'm on a Mac OS X Yosemite

Please let me know if it helps you or if you have a better solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM