using getContentPane() in JFrame class

Hello,

I'm reading Bruce Eckel's book.

In chaapter 14, I read TextArea.java.

My question is:

What is the purpose of getContentPane() in the constructor TextArea() in the code below ?

Isn't JFrame IS a Container ? why bother to get another Container ?

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.util.*;

import com.bruceeckel.swing.*;

import com.bruceeckel.util.*;

public class TextArea extends JFrame {

private JButton

b = new JButton("Add Data"),

c = new JButton("Clear Data");

private JTextArea t = new JTextArea(20, 40);

private Map m = new HashMap();

public TextArea() {

// Use up all the data:

Collections2.fill(m, Collections2.geography,

CountryCapitals.pairs.length);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

Iterator it = m.entrySet().iterator();

while(it.hasNext()) {

Map.Entry me = (Map.Entry)(it.next());

t.append(me.getKey() + ": "+ me.getValue()+"\n");

}

}

});

c.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

t.setText("");

}

});

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

cp.add(new JScrollPane(t));

cp.add(b);

cp.add(c);

}

public static void main(String[] args) {

Consoleb.run(new TextArea(), 475, 425);

}

}

[1555 byte] By [hsiaoka] at [2007-11-27 8:35:20]
# 1
JFrame is a RootPaneContainer: http://java.sun.com/javase/6/docs/api/javax/swing/JRootPane.html
Hippolytea at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 2
I'm afraid I didn't get your point.JFrame is a RootPaneContainerandRootPaneContainer is a Containerwhich means JFrame is a Container.
hsiaoka at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 3

Say you have a JTextArea that you want your JFrame to display.

It's logically(*) wrong to say "JFrame is a Container, so I will add() the JTextArea to JFrame".

What you should say is: "JFrame is a RootPaneContainer, so it has a JRootPane. The JRootPane has a glass pane and a JLayeredPane. The JLayeredPane has a JMenuBar and a content pane. I will add the JTextArea to the content pane".

(*) Since 1.5, JFrame's add() method was altered to behave like getContentPane().add(), but note that it is not adding directly to JFrame. JFrame has only one child, the root pane.

Hippolytea at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 4
> RootPaneContainer is a ContainerActually, that's wrong. RootPaneContainer is an interface.
Hippolytea at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 5

I appreciate the reply.

There are endless API to read.

I found this:

For conveniance JFrame, JDialog, JWindow, JApplet and JInternalFrame, by default, forward, by default, all calls to the add, remove and setLayout methods, to the contentPane. This means you can call:

rootPaneContainer.add(component);

Another question on the side:

How can I remove a topic that I created ?

hsiaoka at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 6

> I appreciate the reply.

> There are endless API to read.

> I found this:

> For conveniance JFrame, JDialog, JWindow, JApplet and

> JInternalFrame, by default, forward, by default, all

> calls to the add, remove and setLayout methods, to

> the contentPane. This means you can call:

> rootPaneContainer.add(component);

That was introduced in 1.5. May people still use older versions of Java, so use that add method with caution. I myself see no reason to use it, since it is a bit of a hack.

> Another question on the side:

> How can I remove a topic that I created ?

Like karma, no thread ever goes away :-)

Hippolytea at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 7
> Like karma, no thread ever goes away :-)I never knew that. The things you learn in this forum...I guess then that there is no such thing as "instant karma"?
petes1234a at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...
# 8
> > Like karma, no thread ever goes away :-)> > I never knew that. The things you learn in this> forum...Well, if a thread gets too wacky for Sun, they smote it down, but there arecopies out there in the internet, right?
Hippolytea at 2007-7-12 20:31:52 > top of Java-index,Java Essentials,Java Programming...