views:

275

answers:

3

I am trying to create my first GUI application using (Java + Eclipse + Swing). This is my code:

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


public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);

        //====================================================== constructor
        public HelloWorldSwing() {
            //... Set initial text, scrolling, and border.
            m_resultArea.setText("Enter more text to see scrollbars");
            JScrollPane scrollingArea = new JScrollPane(m_resultArea);
            scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));

            // Get the content pane, set layout, add to center
            Container content = this.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(scrollingArea, BorderLayout.CENTER);
            this.pack();
        }

        //============================================================= main
        public static void main(String[] args) {
            JFrame win = new HelloWorldSwing();
            win.setTitle("TextAreaDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.setVisible(true);
        }


}

The code was taken from here.

When I run the application from Eclipse the expected window appears (So, it's good. I see what I want to see). However, when I try to close the window or try to write something in the text area the program freezes. The OS writes me that program is not responding (I try it on Ubuntu).

Can anybody help me to find the reason of the problem?

Thank you in advance for any help.

A: 

You need to catch the exit event and respond with a System.exit( 0 );

You should be able to find that in most swing examples online.

wrong stuff... sorry... coffee... argh....

walnutmon
Shouldn't `JFrame.EXIT_ON_CLOSE` take care of that?
Peter Lang
thats bad advice, given you have some logic in a destroy method of a JFrame it would not be executed when calling System.exit()
smeg4brains
A: 

did you try using the eventdispatcherthread to view the JFrame?

something like:

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndViewJFrame();
        }
    });
}

public void createAndViewJFrame(){
    JFrame win = new HelloWorldSwing();
    win.setTitle("TextAreaDemo");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setVisible(true);
}

then your frame would be shown by the swing dispatcher thread.

hope it helped, although im just guessing...

Update: as commenters pointed you i f**ed up the invokeLater() call. I just edited this post to correct that. Thanx go to yishai & willcodejavaforfood for pointing it out!

frank

smeg4brains
That is not how you call the invokeLater method.
Yishai
well it's close :)
willcodejavaforfood
SwingUtilities.invokeLater(new Runnable(){public void run(){createAndViewJFrame()});
willcodejavaforfood
I made the suggested changes. However, I introduced small changes. First, I declared createAndViewJFrame as "static" (otherwise main complains: cannot make a static reference to non-static method). Second, I exchanged "createAndViewJFrame" and "main" (I hope if does not matter). Unfortunately it did not solve the problem. My application still does not respond.
Roman
+1  A: 

I'm sure this doesn't have to do with the code, as others have found the code runs just fine on their machines - which points to a machine specific issue. From within Eclipse, make sure it is setup to use the expected JDK/JRE. However, before worrying about how Eclipse is handling your situation, I'd run things by hand first - especially since you've got a very simple class.

I would check to ensure that you're using the expected compiler and runtime. On Linux:

which javac
which java

If they're both what you expect, do the following:

javac HelloWorldSwing.java
java HelloWorldSwing 

If you get a similar problem, then you know it's not the Eclipse configuration and it's something else. If you're not using the latest JDK, upgrade to the latest. If you're already at the latest, it could be a display driver. Do other JAVA swing programs work on this computer? I'm sure you could find some on the net, download an app already packaged as a jar and try running it.

JamesG