Revalidate JPanel's parent frame

安稳与你 提交于 2021-02-05 07:09:25

问题


I want my panel to fit the frame when I show/hide fields. How could I notify to the parent frame to revalidate?

I thought about pass the frame to my panel's constructor, but I think may have a way this is already done. I remember that there was a protected attribute in JPanel, but there isn't.. maybe i remembering the wrong component.


回答1:


I need show/hide some fields of my panel, and I want the panel to fit the frame.

You mean like a summary/details view and you want the frame size to change? Then try:

SwingUtilities.windowForComponent(...).pack();




回答2:


you could override the addNotify in the panel into something:

public void addNotify() {
    getParent().revalidate();
    repaint();
    super.addNotify();
}

Not sure if that's what you mean though.




回答3:


SwingUtilities.getRoot(this).invalidate();


来源:https://stackoverflow.com/questions/2806069/revalidate-jpanels-parent-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!