Why can't I validate a JComponent?

后端 未结 3 915
执念已碎
执念已碎 2021-01-21 20:37

From JavaDoc:

public void validate()

Validates this container and all of its subcomponents. Validating a container means laying out its s

相关标签:
3条回答
  • 2021-01-21 20:48

    To add up to what has been said in the answers, don't forget to override getPreferedSize() method to return your component preferred size. Otherwise the layout manager won't position your JComponent hence will not be displayed.

    0 讨论(0)
  • 2021-01-21 21:06

    The Javadoc for java.awt.Component.isValid() states:

    A component is valid when it is correctly sized and positioned within its parent container and all its children are also valid.

    In short, you can't validate the component until it has been added to a container.

    0 讨论(0)
  • 2021-01-21 21:07

    In the docs for isValid() it says:

    A component is valid when it is correctly sized and positioned within its parent container and all its children are also valid.

    This is the case until you reach a Top-Level Container (JFrame, JInternalFrame or JApplet). In the example you have in your question, your JComponent doesn't have a parent so it can never be valid.

    0 讨论(0)
提交回复
热议问题