views:

260

answers:

3

I have a working code which creates a window with a text area. The code is here. I try to figure out how this code works. A lot of things are clear:

  1. The main-method creates an instance of the TextAreaDeom class (which is subclass of the JFrame). In other words, the main-method creates a window.
  2. In the main-method we set some "parameters" of the window and make it visible.

It is not clear to me, in which place we put the text area in the window. I see that the text area is created right before the constructor. I also see that in the constructor we set some "parameters" of the text area (setText). I also see that in the constructor we create a a scrolling area and set some parameters for it. I see that the scrolling area is "connected" to the text area (since we use the instance of the text area to create the scrolling area). I also see that we create an object called "content" (using the current window) and we "add" the scrolling area to the "content".

But at which place the text area is added to the window? May be I can say that the text area is added to the scrolling area and the scrolling area is added to the "content" and the content is a part of the window-object?

+2  A: 

in line 16 you create a JScrollPane which wraps around your JTextArea object. On line 21 you add this JScrollPane, which contains your TextArea to the ContentPane of the JFrame.As you call getContentPane() instead of creating a new one the ContentPane is already part of the JFrame.
All elements of the ContentPane will be displayed as part of the JFrame. The add method of JFrame is only for convenience and forwards the call to the JFrames ContentPane.

josefx
A: 

Hi,

The scroll pane scrollingArea is created with the text area inside. scrollPane, was constructed with text area m_resultArea (see the documentation for JScrollPane's constructor). is then added to the frame's content pane.

Klarth
A: 

The GUI elements should be constructed on the EDT. Here is a more reliable main() method for the program cited above.

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