by Dinesh Thakur Category: Swing

The SpringLayout layout manager is for a GUI's components to maintain their position relative to container edges or the edges of the other components after the GUI is resized. The SpringLayout layout manager lays out its container components according to a set of constraints specified by the user. Each constraint represented by a Spring object, controls the vertical or horizontal distance between two component edges. The edges can belong to the container itself or to any child of the container.

In order to create SpringLayout layout manager, use the constructor


Just like CardLayout, you need to keep the reference of the SpringLayout layout manager in order to use the various methods available with the SpringLayout class. For example: The following statements sets the layout manager, for the content pane of a JFrame object to be SpringLayout.

SpringLayout layout = new SpringLayout();


Every component of a SpringLayout controlled container as well as the container itself has exactly one set of constraints associated with it. These constraints are represented by SpringLayout. Constraints object. By default, SpringLayout creates constraints that make their associated component have the minimum, preferred and maximum sizes according to the values returned from the Component's getMinimumSize (), getPreferredSize () and getMaximumSize () respectively. But the components (x,y) positions are not constrained. So each component will put its top left corner at (0,0) point of the container. This overlapping arrangement of components will look messy. In order to properly position the components, you must put constraints explicitly on each component.

You usually don't need to add the component with the constraints. Instead, you can add the component and then typically attach the constraints separately. To add constraints for a component, we first create a SpringLayout. Constraints object. The SpringLayout.Constraints is not a simple class but actually a collection of Spring objects, each of which represents a different constraint on the component. You need to add each spring constraint separately to SpringLayout.Constraints. You do this by setting specific constraints on an edge of the component.

The top, bottom, left and right edges of a component are referred to by their compass point north, south, west and east. When you need to refer to a particular edge in you code for setting a constraint, you need to use one of the constants NORTH, SOUTH, WEST and EAST defined in SpringLayout class

import javax.swing.*;

import java.awt.*;
class SpringLayoutFrameExample extends JFrame
         JButton Btnfirst = new JButton("first");
         JButton BtnSecond = new JButton("Second");
         Container c = getContentPane();
         SpringLayout layout = new SpringLayout();
   class SpringLayoutJavaExample
      public static void main(String args[])
         SpringLayoutFrameExample frame = new SpringLayoutFrameExample();
         frame.setTitle("SpringLayout Java Example");

SpringLayout in Java Swing Example

About Dinesh Thakur


Related Articles