Search code examples
javaswinglayoutlayout-managerspringlayout

Java Swing: manage multiple SpringLayout


I would like to manage a complex structure of containers as a combination of SpringLayouts (a series of stand-alone SpringLayout one inside other)

In particular, a JFrame managed by a main SpringLayout that contains JPanels: every JPanel should be managed by its own SpringLayout.

Unfortunalely, if i try to apply two different SpringLayout, nothing appears on the frame

I made this simple code to show you the problem. The code contains:

  • A JFrame and its SprilgLayout
  • A JPanel and its SpringLayout
  • a simple JButton inside the JPanel

Here the code:

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class SpringLayout_cobination {

    // THE OBJECTS
    static JFrame frame;// The frame
    static JPanel panel;        // The container
    static JButton Button;      // Object

    // THE LayOuts
    static SpringLayout LayOut_Frame;
    static SpringLayout LayOut_panel;
    static Container contentPane;

    public static void main(String[] args) {

        // Start frame and componets
        frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setSize(830, 420); 

        panel=new JPanel();
        Button=new JButton("ok");

        panel.add(Button);
        panel.setSize(new Dimension(100,100));
        frame.add(panel);

        // Set the layouts
        LayOut_Frame=new SpringLayout(); // Frame LayOut
        frame.setLayout(LayOut_Frame);
        contentPane=frame.getContentPane(); // Main Container of the frame

        LayOut_panel=new SpringLayout();
        panel.setLayout(LayOut_panel);

        LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
        LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);

        LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
        LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);

        frame.setVisible(true);
    }

}

Solution

  • The part of code i forgot was:

    SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
    c.setWidth(Spring.constant(200));
    c.setHeight(Spring.constant(100));
    

    It seems that you must impose a Constraints dimension every time you import a component in the main frame

    in my example, when I import the The final code is:

    import java.awt.Container;
    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Spring;
    import javax.swing.SpringLayout;
    
    public class SpringLayout_cobination {
    
        // THE OBJECTS
        static JFrame frame;// The frame
        static JPanel panel;        // The container
        static JButton Button;      // Object
    
        // THE LayOuts
        static SpringLayout LayOut_Frame;
        static SpringLayout LayOut_panel;
        static Container contentPane;
    
        public static void main(String[] args) {
    
            // Start frame and componets
            frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setSize(830, 420); 
    
            panel=new JPanel();
            Button=new JButton("ok");
    
            panel.add(Button);
            panel.setSize(new Dimension(100,100));
            frame.add(panel);
    
            // Set the layouts
            LayOut_Frame=new SpringLayout(); // Frame LayOut
            frame.setLayout(LayOut_Frame);
            contentPane=frame.getContentPane(); // Main Container of the frame
    
            LayOut_panel=new SpringLayout();
            panel.setLayout(LayOut_panel);
            LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
            LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);
    
            SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
            c.setWidth(Spring.constant(200));
            c.setHeight(Spring.constant(100));
    
            LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
            LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);
    
    
    
            frame.setVisible(true);
        }
    
    }