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]

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.
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 ?
> 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 :-)