-4

I tried to execute the following code within Eclipse (OSX):

public static void main(String[] args) {
            JFrame frame = new JFrame("Test");
            frame.setSize(new Dimension(400, 30));
            frame.add(new JButton("hello"));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }

The frame does not show up but I get the following console messages:

2014-05-16 14:45:35.230 java[8685:903] [Java CocoaComponent compatibility mode]: Enabled
2014-05-16 14:45:35.232 java[8685:903] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000
2014-05-16 14:45:35.546 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100612800 of class NSConcreteMapTableValueEnumerator autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.547 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100613f40 of class __NSCFDate autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.547 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x100616e60 of class NSCFTimer autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.550 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x10061d7c0 of class __NSCFDate autoreleased with no pool in place - just leaking
2014-05-16 14:45:35.550 java[8685:903] *** __NSAutoreleaseNoPool(): Object 0x10061e610 of class NSCFTimer autoreleased with no pool in place - just leaking

While, if I put the code into a java file outside of my Eclipse project and compile and run it via command line, everything is fine and the frame shows up. Can anybody help me troubleshooting?

UPDATE

the code now looks like this:

import javax.swing.*;

public class TestFrame {

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }

            private void createAndShowGUI() {
                JFrame frame = new JFrame("Test");
                System.out.println(SwingUtilities.isEventDispatchThread());
                frame.getContentPane().add(new JButton("hello"));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });

    }
}

This does not solve the problem (as already mentioned by user DSquare). I figured out that it seems to be a problem with that specific Eclipse project. If I create a new Eclipse project (same Eclipse), the code runs without an error message and the frame shows up. I still don't have a clue which project cnfigurations may cause it. I don't have swt.jar in my classpath (though org.eclipse.swt and org.eclipse.swt.cocoa.macosx.x86_64 in my plugin-dependencies).

Antje Janosch
  • 1,154
  • 5
  • 19
  • 37
  • It'd be useful If you explained why the multiple solutions that you can find with a quick search don't work. And how did you get to that conclusion so fast (<5 minutes), so that the core of your pproblem is more clear and we can help solving it intead of losing time with unsuccessful solutions. – DSquare May 16 '14 at 13:08
  • I cannot find "multiple solutions" (and I spent much more than 5 minutes looking for it). You mentioned that my OSX is misconfigured. It would be nice if you would help me to figure out what's wrong with my OSX. – Antje Janosch May 19 '14 at 06:24
  • It appears to be an error specific with threading in OSX. That piece of code in itself is fine, other solutions either mention the necessity to add certain code necessary for threading management or changing certain values in the Java configuration. Some related answers are [this](http://stackoverflow.com/questions/4768817/what-does-autoreleased-with-no-pool-in-place-mean), [this](http://stackoverflow.com/questions/10871489/no-autorelease-pool-with-jogl) or [this](http://stackoverflow.com/questions/10003962/breakpoint-pointing-out-objc-autoreleasenopool), among others. – DSquare May 19 '14 at 08:09
  • Also since this is threading related make sure to get right what I mentioned about the EDT. You should put `invokeLater` in your main and work from there. – DSquare May 19 '14 at 08:11
  • I've changed the code (see update). Beside this, I could not yet figure out which project settings are wrong and cause this error. – Antje Janosch May 19 '14 at 11:50

5 Answers5

1

I'm not sure whether this is the final solution but I learned a few things and want to share this here:

Any comment/correction is very welcome (as I'm still a newbie to all this...)

Antje Janosch
  • 1,154
  • 5
  • 19
  • 37
0

Your code is right and working in my own eclipse.

May be your eclipse is not working perfectly. in my opinion change your eclipse version and try again.

By the way your make sure to jdk version is same in eclipse and your console.

SiMuRg
  • 1
  • His code is not right, but it's not the cause of his problem. See [Swing's Hello World](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java). There's a fundamental flaw in his example code. – DSquare May 16 '14 at 13:06
  • this is Swing's example and very good hello world application but it is not mean his code is not right. – SiMuRg May 16 '14 at 13:14
  • 1
    He is breaking swing's single threading rule, please read about the [initial threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). Nothing goes wrong with that simple application, however multiple inconsistencies and non-reproducible bugs will appear once the application gets bigger and more complex. That's why the documentation of Swing classes is full of "Warning: Swing is not thread safe. For more information see [Swing's Threading Policy](http://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html#threading)." messages. – DSquare May 16 '14 at 13:20
  • i see i now Swing is a not thread safe but this is just hello world, he is do not need to think about complex. – SiMuRg May 16 '14 at 13:41
  • 1
    Again, look at the official [Hello World](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java), they do call `invokeLater` in the main. "It's just a simple program" is just an excuse for the lazy or the ignorant. If you don't do your simple programs well, with what confidence do you go into the bigger more important ones. – DSquare May 16 '14 at 14:11
  • Unfortunately I cannot change my Eclipse as I do some plugin development for an application build upon Eclipse. The console jdk is the same as the one used in eclipse – Antje Janosch May 19 '14 at 06:27
0

I don't think you can directly add component to the JFrame. You must use the syntax:

frame.getContentPane().add(new JButton("hello"));

as mentioned by @DSquare.

You can add directly to other swing component though but not JFrame.

I did a little more research and find out that since java 1.5 you can call add() directly to the JFrame object and it will implicitly call the correct pane. That include only the addition step. All other calls to content pane should be done explicitly.

Eypros
  • 5,370
  • 6
  • 42
  • 75
  • I have said nothing of the sorts, I was referring to [EDT](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) problems as I specified later. You can add directly to the JFrame because `JFrame.add(...)` is just a comodity method for `JFrame.getContentPane().add(...)` as specified in the [documentation](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html). He has a configuration problem, not a code problem. – DSquare May 16 '14 at 13:25
0

You need to add frame.pack() before frame.setVisible(true).

Also check DSquare comment: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java

adriannieto
  • 352
  • 2
  • 7
  • He does not "need" to. He probably should, when his GUI is more complex, since setSize is [not recommended](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). However that has nothing to do with his OSX misconfiguration problems. – DSquare May 16 '14 at 14:08
0

I had a similar problem when I was using swing and java 1.8. There is some kind of threading problem as mentioned by @DSquare.

I finally solved the problem by unchecking the "Use the -XstartOnFirstThread argument when launching with SWT" in Run --> Run Configurations --> Arguments

Hope this helps.