A qustion about the model of JTextArea

First please look at the following test code:

import javax.swing.*;

publicclass TextAreaModel{

private JTextArea area;

public TextAreaModel(){

area =new JTextArea();

if (area.getDocument() ==null){

System.out.println("area's model is null");

}else{

System.out.println(area.getDocument());

}

}

publicstaticvoid main(String[] args){

new TextAreaModel();

}

}

The output is javax.swing.text.PlainDocument@54172f or sth. like this

except the string after '@'. I just thought that it should output "area's

model is null".

I tried to analyse the code, but I got totally confused.

area = new JTextArea() invokesJTextArea(null, null, 0,0),

which causesJTextComponent() to be called.

JTextComponent() 's java doc says that this constructor will set

the document model tonull, but I set breakpoints to trace into the

excution and find that model becomes aPlainDocument instance

after the constructorJTextComponent(), and then the test code outputs the non-null model

object 's information.

Is there anyone that can tell me why the constructor

JTextComponent()'s behavior seems confliting with its java-doc?

[2067 byte] By [huza] at [2007-10-3 10:32:40]
# 1

This is the source of the c'tor:public JTextArea(Document doc, String text, int rows, int columns) {

super();// JTextComponent

this.rows = rows;

this.columns = columns;

if (doc == null) {

doc = createDefaultModel(); // <- new PlainDocument();

}

setDocument(doc);

[snip]

PhHeina at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 2
I mean that model becomes non-null after super() (JTextComponent()[/]).but JTextComponent()'s java-doc says it set model to null
huza at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 3
Good heavens look at the code. The super call sets it to null and a few lines later, back in the JTextArea c'tor it's set to a PlainDocument.
PhHeina at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 4
I don't think you understood what i mean. I said model becomes non-null just after super() was called.On that time. setDocument(doc) isn't excuted.
huza at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 5
How did you test that? Because your code doesn't tell.
PhHeina at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 6

I use eclipse to set some breakpoints in JTextArea(), after super

() was called, model is a PlainDocument instance whose id

is 126, and then it excuted the code setDocument, the model

becomes a PlainDocument instance whose id is 155. model had

been set twice.

huza at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...
# 7
Got it, the JTextComponent c'tor never touches the document. So the API doc seems to be wrong after all.
PhHeina at 2007-7-15 5:55:41 > top of Java-index,Desktop,Core GUI APIs...