Example usage for javax.swing SpringLayout getConstraints

List of usage examples for javax.swing SpringLayout getConstraints

Introduction

In this page you can find the example usage for javax.swing SpringLayout getConstraints.

Prototype

public Constraints getConstraints(Component c) 

Source Link

Document

Returns the constraints for the specified component.

Usage

From source file:Main.java

public static void main(String[] args) {
    SpringLayout layout = new SpringLayout();
    JPanel p = new JPanel(layout);
    p.setBorder(BorderFactory.createLineBorder(Color.GREEN, 10));

    JLabel l1 = new JLabel("label: width=90%", SwingConstants.CENTER);
    l1.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
    JButton l2 = new JButton("button: width=50%");

    Spring panelw = layout.getConstraint(WIDTH, p);

    SpringLayout.Constraints c1 = layout.getConstraints(l1);
    c1.setX(Spring.constant(0));//ww  w. j  av a 2  s .c om
    c1.setY(Spring.constant(20));
    c1.setWidth(Spring.scale(panelw, 0.9f));
    p.add(l1);

    SpringLayout.Constraints c2 = layout.getConstraints(l2);
    c2.setWidth(Spring.scale(panelw, 0.5f));
    layout.putConstraint(SOUTH, l2, -20, SOUTH, p);
    layout.putConstraint(EAST, l2, -20, EAST, p);
    p.add(l2);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(p);
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:SpringDemo4.java

public static void main(String[] args) {
    JFrame frame = new JFrame("SpringDemo4");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);/* www.j a  v  a  2  s . c  o  m*/

    // Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);
    contentPane.add(textField);

    // Adjust constraints for the label so it's at (5,5).
    SpringLayout.Constraints labelCons = layout.getConstraints(label);
    labelCons.setX(Spring.constant(5));
    labelCons.setY(Spring.constant(5));

    // Adjust constraints for the text field so it's at
    // (<label's right edge> + 5, 5).
    SpringLayout.Constraints textFieldCons = layout.getConstraints(textField);
    textFieldCons.setX(Spring.sum(Spring.constant(5), labelCons.getConstraint(SpringLayout.EAST)));
    textFieldCons.setY(Spring.constant(5));

    // Adjust constraints for the content pane.
    setContainerSize(contentPane, 5);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:TrySpringLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout layout = new SpringLayout();
    Container content = aWindow.getContentPane();
    content.setLayout(layout);/*w  w  w. j  ava  2  s .  c  o  m*/
    JButton[] buttons = new JButton[6];
    SpringLayout.Constraints constr = null;
    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new JButton("Press " + (i + 1));
        content.add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring);
    constr.setY(ySpring);
    // Hook buttons together with springs
    for (int i = 1; i < buttons.length; i++) {
        constr = layout.getConstraints(buttons[i]);
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring, SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring, SpringLayout.SOUTH, buttons[i - 1]);
    }
    aWindow.setVisible(true); // Display the window
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500, 500);//  w ww  .  j av  a 2 s. co  m
    SpringLayout layout = new SpringLayout();
    Container content = aWindow.getContentPane();
    content.setLayout(layout);

    JButton[] buttons = new JButton[6];
    SpringLayout.Constraints constr = null;
    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new JButton("Press " + (i + 1));
        content.add(buttons[i]);
    }

    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);

    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring);
    constr.setY(ySpring);

    for (int i = 1; i < buttons.length; i++) {
        constr = layout.getConstraints(buttons[i]);
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring, SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring, SpringLayout.SOUTH, buttons[i - 1]);
    }
    aWindow.setVisible(true); // Display the window
}

From source file:Main.java

protected static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    // Set the container's right edge to the right edge
    // of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    // Set the container's bottom edge to the bottom edge
    // of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }//from w w w.j a  v a 2 s.c  o m
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:SpringDemo4.java

public static void setContainerSize(Container parent, int pad) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component[] components = parent.getComponents();
    Spring maxHeightSpring = Spring.constant(0);
    SpringLayout.Constraints pCons = layout.getConstraints(parent);

    //Set the container's right edge to the right edge
    //of its rightmost component + padding.
    Component rightmost = components[components.length - 1];
    SpringLayout.Constraints rCons = layout.getConstraints(rightmost);
    pCons.setConstraint(SpringLayout.EAST,
            Spring.sum(Spring.constant(pad), rCons.getConstraint(SpringLayout.EAST)));

    //Set the container's bottom edge to the bottom edge
    //of its tallest component + padding.
    for (int i = 0; i < components.length; i++) {
        SpringLayout.Constraints cons = layout.getConstraints(components[i]);
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getConstraint(SpringLayout.SOUTH));
    }/*from w  w  w . j av a 2  s.  c o m*/
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(pad), maxHeightSpring));
}

From source file:SpringBox.java

private static SpringLayout.Constraints getConstraintsForCell(int row, int col, Container parent, int cols) {
    SpringLayout layout = (SpringLayout) parent.getLayout();
    Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}

From source file:layout.SpringDemo4.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from w  w w .  java  2s .  co m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SpringDemo4");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);

    //Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);
    contentPane.add(textField);

    //Adjust constraints for the label so it's at (5,5).
    SpringLayout.Constraints labelCons = layout.getConstraints(label);
    labelCons.setX(Spring.constant(5));
    labelCons.setY(Spring.constant(5));

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    SpringLayout.Constraints textFieldCons = layout.getConstraints(textField);
    textFieldCons.setX(Spring.sum(Spring.constant(5), labelCons.getConstraint(SpringLayout.EAST)));
    textFieldCons.setY(Spring.constant(5));

    //Adjust constraints for the content pane.
    setContainerSize(contentPane, 5);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:SpringDemo4.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *///from   w  w w  .j  a  va 2 s .  c  o m
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SpringDemo4");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);

    //Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);
    contentPane.add(textField);

    //Adjust constraints for the label so it's at (5,5).
    SpringLayout.Constraints labelCons = layout.getConstraints(label);
    labelCons.setX(Spring.constant(5));
    labelCons.setY(Spring.constant(5));

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    SpringLayout.Constraints textFieldCons = layout.getConstraints(textField);
    textFieldCons.setX(Spring.sum(Spring.constant(5), labelCons.getConstraint(SpringLayout.EAST)));
    textFieldCons.setY(Spring.constant(5));

    //Adjust constraints for the content pane.
    setContainerSize(contentPane, 5);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}