Search code examples
javaswinggridbaglayout

Java Swing - Modify GridBagConstraints


I got two JPanels in a GridBagLayout, a canvas and a toolbox, and if the user clicks over the toolbox panel I want it to change size (essentially opening it). Is there a (smarter) way to modify the weight of the toolbox constraint without having to remove all the components from the parent panel and re-adding the canvas and toolbox but with modified constraints?


Solution

  • You can change the constraints of a Component already contained in a Container with a GridBagLayout by calling

    1. setConstraints on the GridBagLayout with the appropriate GridBagConstraint object
    2. Calling revalidate and repaint on the Container