简体   繁体   中英

System.out.print(JPanel);

core.EditArea[,119,96,556x931,invalid,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

I have printed out a JPanel, and above is an output. Can anyone tell me what that "invalid" part means?

Better yet, if anyone knows where to find a list of explaned System.out.print(thingy) outputs for as much thingys as possible?

System.out.print(thingy) is just printing method thingy.toString()

toString() for JPanel comes from java.awt.Component class:

public String toString() {
    return getClass().getName() + "[" + paramString() + "]";
}



There is invokation of protected String paramString() that leads us back to JPanel where this method is overridden:

/**
 * Returns a string representation of this JPanel. This method 
 * is intended to be used only for debugging purposes, and the 
 * content and format of the returned string may vary between      
 * implementations. The returned string may be empty but may not 
 * be <code>null</code>.
 * 
 * @return  a string representation of this JPanel.
 */
protected String paramString() {
    return super.paramString();
}

and this leads us to JCompomnent that is first level parent class of JPanel (but it still does not have explicitly declared toString() so this one from java.awt.Component is called for every JComponent, including JPanel).
So, paramString() that gets invoked:

protected String paramString() {
    String preferredSizeString = (isPreferredSizeSet() ?
                  getPreferredSize().toString() : "");
    String minimumSizeString = (isMinimumSizeSet() ?
                getMinimumSize().toString() : "");
    String maximumSizeString = (isMaximumSizeSet() ?
                getMaximumSize().toString() : "");
    String borderString = (border != null ?
               border.toString() : "");

    return super.paramString() +
    ",alignmentX=" + alignmentX +
    ",alignmentY=" + alignmentY +
    ",border=" + borderString +
",flags=" + flags +             // should beef this up a bit
    ",maximumSize=" + maximumSizeString +
    ",minimumSize=" + minimumSizeString +
    ",preferredSize=" + preferredSizeString;
}

... and again - super.paramString() - from java.awt.Container

protected String paramString() {
String str = super.paramString();
LayoutManager layoutMgr = this.layoutMgr;
if (layoutMgr != null) {
    str += ",layout=" + layoutMgr.getClass().getName();
}
return str;
}

... and again - super.paramString() - that finally leads us back to java.awt.Component

protected String paramString() {
    String thisName = getName();
    String str = (thisName != null? thisName : "") + "," + x + "," + y + "," + width + "x" + height;
    if (!isValid()) {
        str += ",invalid";
    }
    if (!visible) {
        str += ",hidden";
    }
    if (!enabled) {
        str += ",disabled";
    }
    return str;
}

And here is the root cause of invalid that was printed inside your string describing JPanel

/**
 * Determines whether this component is valid. A component is valid
 * when it is correctly sized and positioned within its parent
 * container and all its children are also valid. 
 * In order to account for peers' size requirements, components are invalidated
 * before they are first shown on the screen. By the time the parent container 
 * is fully realized, all its components will be valid.
 * @return <code>true</code> if the component is valid, <code>false</code>
 * otherwise
 * @see #validate
 * @see #invalidate
 * @since JDK1.0
 */
public boolean isValid() {
    return (peer != null) && valid;
}

How to achieve valid state was nicely explained by @Reimeus

It's the result of isValid() of java.awt.Component , which is extended by JPanel :

public boolean isValid()

Determines whether this component is valid. A component is valid when it is correctly sized and positioned within its parent container and all its children are also valid. In order to account for peers' size requirements, components are invalidated before they are first shown on the screen. By the time the parent container is fully realized, all its components will be valid.

The 'invalid' field simply means that the components need to be laid out. To make 'valid' can be acheived by 'packing' the parent container, eg:

JPanel p = new JPanel();
JFrame frame = new JFrame();
frame.add(p);
frame.pack();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM