JPanel size by inner components

前端 未结 8 2200
夕颜
夕颜 2021-02-18 13:48

Is it possible to tell JPanel to set its size to fit all components that it contains? Something like pack() for JFrame.

edit: The trick with preferredSize d

相关标签:
8条回答
  • 2021-02-18 14:31

    By default Containers have a preferred size that matches the preferred layout size given by the container. So literally all you have to do is:

    panel.setSize(panel.getPreferredSize());
    

    Presumably you are doing something odd with the parent to stop the parent's layout manager doing the equivalent of this.

    0 讨论(0)
  • 2021-02-18 14:33

    I had a similar issue using Netbeans GUI Builder. My inner panels were getting weird sizes; I was trying to adjust the minimum and preferred sizes manually, which was a frustrating exercise.

    The problem was solved when I reset all the minimum and preferred sizes back to default (In Netbeans GUI Builder: right click JPanel component -> Properties -> preferredSize -> Reset to Default). When there is no imposed size, the jpanel takes the size of the inner component.

    Note: GridBaLayout was used in my case

    0 讨论(0)
  • 2021-02-18 14:33

    JSplitPanes are a bit fussy when it comes to its children's sizes, have a look at the Java tutorial. Are you using the GridBagLayout correctly? Looks like it's not setting the right JPanel's minimum size properly.

    0 讨论(0)
  • 2021-02-18 14:39

    Here's an example of a panel which:

    • Resizes with it's parent.
    • Sets the width to the width of the parent.
    • Sets the height according to sum of the height of all of it's children.
    JPanel panel = JPanel(new GridBagLayout())
    
    panel.setMaximumSize(new Dimension(panel.getMaximumSize().width, panel.getPreferredSize().height))
    
    panel.validate()
    panel.repaint()
    
    0 讨论(0)
  • 2021-02-18 14:39

    The javax.swing mysteries reveal themselves only gradually, and only to those who are prepared to offer many libations (particularly torn out clumps of hair, hours burning the midnight oil, etc.) to the gods of Swing.

    However, for this case in point I would suggest the following as a sort of Swiss army knife which usually does what you think the framework should do anyway:

    myJPanel.getTopLevelAncestor().validate()
    

    As the sacred text says, "Validates this container and all of its subcomponents." (Container.validate). NB getTopLevelAncestor() is a JComponent method.

    Can't remember how JSplitPane fits into this: try it and you'll probably find that it validates both components (right and left, top and bottom), but I would be surprised if changing the divider doesn't do this for you anyway.

    0 讨论(0)
  • 2021-02-18 14:40

    After looking at the source code for pack(), I came up with:

        panel.setPreferredSize(panel.getPreferredSize());
    

    This forces the panel to recalculate its preferred size based on the preferred sizes of its subcomponenents.

    You may or may not have to call validate() afterward; in my tiny example, it seemed to make no difference, but the Javadoc says:

    The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

    So I guess it depends on why you're having to repack your JPanel.

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