简体   繁体   中英

Swing Application Window not responding on Mac

I'm trying to get a basic Swing Application to run on my Mac OS X 10.8.2 (Java version 1.6.0_37) machine, and every time I try to run it from Eclipse , the frame appears, but I can't interact with it.

I've tried to start from a basic, clean slate where I create a new Swing Application Window project in Eclipse (WindowBuilder->Swing Designer->Application Window) . This generates the following skeleton code:

import java.awt.EventQueue;

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

public class Test {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test window = new Test();
                window.frame.setVisible(true);
                window.frame.pack();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Test() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnPress = new JButton("Press");
    frame.getContentPane().add(btnPress, BorderLayout.CENTER);
}

}

Everything seems to be fine, but when I run this from Eclipse, the frame doesn't let me interact with any components (in my non-example code, there are buttons and tabs).

Also, in the console, I see things like:

2012-11-09 14:30:27.624 java[8107:707] [Java CocoaComponent compatibility mode]: Enabled

2012-11-09 14:30:27.626 java[8107:707] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000

Is there some Mac-specific setting that I have to change? (I'm using the latest default Mac JRE)

The program runs fine on my machine under OSX, but it could be the missing

window.frame.pack();

Have you tried this?

Test window = new Test();
window.frame.pack();
window.frame.setVisible(true);

I had the same problem when using the DJ Swing library in my application (it uses SWT). Interestingly, the problem occurred even though I didn't initialise DJ Swing explicitly. It now works because I have added DJ Swing initialisation:

public class SwingAppTest {

    public static void main(String[] args) {

        NativeInterface.open();
        UIUtils.setPreferredLookAndFeel();

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("Example");
                frame.getContentPane().setLayout(new BorderLayout());
                frame.setPreferredSize(new Dimension(400, 200));
                frame.setBounds(0,0,200,200);
                frame.setTitle("blah");
                JButton blah = new JButton("blah");
                blah.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        System.out.println("button clicked");
                    }
                });
                frame.getContentPane().add(blah, BorderLayout.CENTER);
                frame.setVisible(true);
            }
        });

        NativeInterface.runEventPump();
    }

}

Apple is no longer supporting Java in is operating systems after 10.6:
Apple not committing to Java support in Mac OS X 10.7

seems like missing or incompatible library files in the JRE.

that is weird. I tried your sample (no linux) and don't see any problem there. So as Wayne mentioned, might be macos issue.

Btw, what is your java version you're using?

On the other hand problem might be in the code you didn't share with us => hard to guess :)

EDIT : OK, so it seems you're playing "guess what I have in my code" game with us :) As my assumption is that code not shown causes problem.

It reminds me of some of these Poiroit/Agatha Christie detective stories, where just a little detail might have significant impact on the reality.

This is the reason for my theory:

  • code shown uses Java swing library ( import javax.swing. ... ) + Awt ( import java.awt. ... ) - this combonation is a common use case, however
  • the error message you shared shows SWT library error ( Setting timeout for SWT to 0.100000 )

So to me it seems like you're mixing things that should never be mixed. As Swing is java UI library that is completely OS independent (originally Sun made) that is built on top of Awt . However SWT is completely different java UI library which is kind of mix of native calls with java on it (originally IBM made).

Therefor I'd suggest to double check your code and make sure that if you use JFrame , the only library components you have there are from Swing ( javax.swing.... ) / Awt ( java.awt. ... ).

I've been knocking my head against a wall for days trying to get this working and finally found the answer:

'It's solved now - it was just a case of removing swt.jar from the project dependencies.'

Hey presto!!!!

In my case, I was trying to code a game where I used jPanel. I needed to override the paint method and added pack(); to the main and it finally showed me what I was trying to draw.

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