Example usage for java.awt Container setLayout

List of usage examples for java.awt Container setLayout

Introduction

In this page you can find the example usage for java.awt Container setLayout.

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

From source file:layout.SpringDemo1.java

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

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    contentPane.add(new JLabel("Label: "));
    contentPane.add(new JTextField("Text field", 15));

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

From source file:SpringDemo1.java

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

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

    //Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    contentPane.add(new JLabel("Label: "));
    contentPane.add(new JTextField("Text field", 15));

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

From source file:Main.java

/**
 * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
 * 1.3 {@link javax.swing.JFileChooser#showDialog}.
 *
 * @param chooser the file chooser to display.
 * @param parent the parent window.// w  w w. jav  a 2s  .  c o m
 * @param approveButtonText the text for the approve button.
 *
 * @return the return code of the chooser.
 */
public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) {
    if (approveButtonText != null) {
        chooser.setApproveButtonText(approveButtonText);
        chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
    }

    Frame frame = (parent instanceof Frame) ? (Frame) parent
            : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
    String title = chooser.getDialogTitle();

    if (title == null) {
        title = chooser.getUI().getDialogTitle(chooser);
    }

    final JDialog dialog = new JDialog(frame, title, true);
    dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(chooser, BorderLayout.CENTER);
    dialog.pack();
    dialog.setLocationRelativeTo(parent);
    chooser.rescanCurrentDirectory();

    final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };

    ActionListener l = new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
                retValue[0] = JFileChooser.APPROVE_OPTION;
            }

            dialog.setVisible(false);
            dialog.dispose();
        }
    };

    chooser.addActionListener(l);
    dialog.show();

    return (retValue[0]);
}

From source file:layout.SpringDemo2.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from   w w w .  ja va  2 s.c om*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SpringDemo2");
    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).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

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

From source file:SpringDemo2.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 av a2 s. co 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("SpringDemo2");
    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).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

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

From source file:layout.SpringDemo3.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from   ww w. j a v  a 2s  .  com*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("SpringDemo3");
    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).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the content pane: Its right
    //edge should be 5 pixels beyond the text field's right
    //edge, and its bottom edge should be 5 pixels beyond
    //the bottom edge of the tallest component (which we'll
    //assume is textField).
    layout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, textField);
    layout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, textField);

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

From source file:Main.java

/**
 * Lays out a list of controls in a compact grid-layout, meaning that each
 * cell in the grid only takes up it's preferred size.
 * /*www. j a  va  2  s  .c  om*/
 * @param parent the parent <code>Container</code>
 * @param colCount the number of columns
 * @param padding the number of pixels to pad between cells
 * @param components a <code>List</code> of <code>Component</code>s that
 *        should be added to <code>parent</code>
 */
public static void makeCompactGrid(Container parent, int colCount, int padding,
        List<? extends Component> components) {

    parent.setLayout(new GridBagLayout());

    final int componentCount = components.size();
    final int rowCount = getRowCount(componentCount, colCount);

    final GridBagConstraints c = new GridBagConstraints();
    final int realPadding = (padding / 2);

    c.gridheight = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 1;
    c.insets = new Insets(realPadding, realPadding, realPadding, realPadding);

    for (int i = 0; i < (rowCount * colCount); i++) {
        final int x = (i % colCount);
        final int y = (i / colCount);

        c.gridx = x;
        c.gridy = y;

        parent.add(components.get(i), c);
    }
}

From source file:SpringDemo3.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 av a  2 s .  c  om*/
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("SpringDemo3");
    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).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the text field so it's at
    //(<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

    //Adjust constraints for the content pane: Its right
    //edge should be 5 pixels beyond the text field's right
    //edge, and its bottom edge should be 5 pixels beyond
    //the bottom edge of the tallest component (which we'll
    //assume is textField).
    layout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, textField);
    layout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, textField);

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

From source file:SquareLayout.java

/**
 * Creates a new Container with SquareLayout which will contain the specified
 * component.// ww w.  j a va 2  s  .  c o  m
 */

public static Container createSquareContainer(Component child) {
    Container container = new Container();
    container.setLayout(new SquareLayout());
    container.add(child);

    return container;
}

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 ww.j a  v a2  s. c o 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);
}