Roman
Roman

Reputation: 131038

How does the Swing Hello World application works?

I am trying to figure out how the Swing based Hello World application works. This is the code I have:

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 (I slightly modified it). I have two main questions about the example but if you answer at leas one of them would appreciate it a lot. Here are my questions:

1. As far as I understand the "main" method is run automatically. In this method we instantiate a "win" object and, as a consequence the constructor of the class will be executed. But when the first line of the class is executed (JTextArea m_resultArea = new JTextArea(6, 30));.

2. Is there a good reason to instantiate the text area (m_resultArea) outside the constructor and then set its parameters (setText) within the constructor. Why we cannot instantiate the text area in the constructor? Why we cannot set the parameters of the text area beyond the constructor? (Just for the sake of consistency).

Upvotes: 0

Views: 980

Answers (5)

JRL
JRL

Reputation: 77995

Side note:

This is a typical example of where using a debugger will greatly help you understand the flow of execution. If you don't know how to use a debugger, I recommend these great free video tutorials for the Eclipse debugger.


Now, to answer your questions:

As far as I understand the "main" method is run automatically. In this method we instantiate a "win" object and, as a consequence the constructor of the class will be executed.

The object being instantiated is of type HelloWorldSwing, not win. win is just the name of the variable.

Is there a good reason to instantiate the text area (m_resultArea) outside the constructor and then set its parameters (setText) within the constructor.

Instantiation can occur outside of the constructor, but regular method calls like setText() must occur within another method.

But when the first line of the class is executed (JTextArea m_resultArea = new JTextArea(6, 30));.

The first line of the class is executed before the constructor.

Why we cannot instantiate the text area in the constructor?

You can as well, up to you.

Why we cannot set the parameters of the text area beyond the constructor? (Just for the sake of consistency).

As I said before, loops, method calls, etc. must occur within a method (constructor, or main, or any other method).

For more info on field initialization, you can refer to the Sun tutorial here.

Upvotes: 3

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17359

The code you posted violates Swing threading rules. The code in main method has to run on Event Dispatch Thread and should look like:

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

Upvotes: 3

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45324

1) No, you do not instantiate a "win" object; you instantiate a HelloWorldSwing object. Instance variables are initialized before the body of the constructor runs.

2) There's no reason why you can't instantiate the text area in the constructor. You can set various properties of the TextArea any time you like.

Upvotes: 1

Péter Török
Péter Török

Reputation: 116246

  1. the line is executed automatically when the HelloWorldSwing object is created. All fields of the class are initialized before the constructor is actually called.

  2. you can do it either way. In this case, as resultArea is modified inside the constructor, I would put the instantiation inside the constructor as well, but it is IMHO a style issue. And you are also free to modify the properties of resultArea anytime after the constructor has finished.

Upvotes: 1

Mike42
Mike42

Reputation: 397

1- The instantiation of the JTextArea is the first thing that happens when you create an object of HelloWorldSwing. The constructor is executed right after that. Since it's not static, you cannot access it without and object (in this case, win).

2- There is a good reason, but it could be instantiated inside the constructor. It's like that because that way you make sure it is not null when you want to use it. If you used it in the constructor before you instantiated, you would get an Exception. It's just a way to make sure it's already created before the class object is contructed.

Upvotes: 1

Related Questions