Example usage for javax.swing Box createHorizontalStrut

List of usage examples for javax.swing Box createHorizontalStrut

Introduction

In this page you can find the example usage for javax.swing Box createHorizontalStrut.

Prototype

public static Component createHorizontalStrut(int width) 

Source Link

Document

Creates an invisible, fixed-width component.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();

    Box box = new Box(BoxLayout.X_AXIS);
    int width = 10;
    box.add(Box.createHorizontalStrut(width));
    box.add(component1);/*from  ww w.j a  va  2s  .  com*/

}

From source file:Main.java

public static void main(String[] args) {
    JTextField ipField = new JTextField(10);
    JTextField portField = new JTextField(10);
    JPanel panel = new JPanel();
    panel.add(new JLabel("IP:"));
    panel.add(ipField);//from   w w w .  jav a 2s. c  o  m

    panel.add(Box.createHorizontalStrut(15));
    panel.add(new JLabel("Port:"));
    panel.add(portField);

    int result = JOptionPane.showConfirmDialog(null, panel, "Enter Information", JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) {
        System.out.println("IP: " + ipField.getText());
        System.out.println("Port: " + portField.getText());
    }
}

From source file:Main.java

public static void main(String[] args) {
    JTextField xField = new JTextField(5);
    JTextField yField = new JTextField(5);

    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("x:"));
    myPanel.add(xField);//from  w ww .  ja va2  s  .c o  m
    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
    myPanel.add(new JLabel("y:"));
    myPanel.add(yField);

    int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
            JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        System.out.println("x value: " + xField.getText());
        System.out.println("y value: " + yField.getText());
    }

}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Component rowTwo = Box.createHorizontalStrut(2);

    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);/*from  w w w .  jav  a  2  s.  c om*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Show Input Dialog Box");
    button.addActionListener(e -> {//  w w  w  .ja va 2  s  .c  o  m
        JTextField xField = new JTextField(5);
        JTextField yField = new JTextField(5);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("x:"));
        myPanel.add(xField);
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("y:"));
        myPanel.add(yField);

        int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
                JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println("x value: " + xField.getText());
            System.out.println("y value: " + yField.getText());
        }
    });
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:StrutSample.java

public static void main(String args[]) {
    Box horizontalBox;// ww  w.  j ava2s.  co m
    JPanel panel;
    JFrame frame = new JFrame("Horizontal Strut");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalStrut(10));
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("Beginning Strut"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(Box.createHorizontalStrut(10));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(Box.createHorizontalStrut(25));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("2 Middle Struts"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalStrut(25));
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    horizontalBox.add(Box.createHorizontalStrut(10));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("Beginning/End Struts"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    horizontalBox.add(Box.createHorizontalStrut(10));
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("End Strut"));
    contentPane.add(panel);

    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(e -> valueLabel.setText(String.valueOf(--value)));

    JButton incButton = new JButton("+");
    incButton.addActionListener(e -> valueLabel.setText(String.valueOf(++value)));

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;/*from   w ww  .  j ava2 s .  c o  m*/
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    c.gridy = 1;
    int w = 32;
    for (c.gridx = 0; c.gridx < 3; c.gridx++) {
        panel.add(Box.createHorizontalStrut(w), c);
    }

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box left = Box.createVerticalBox();
    left.add(Box.createVerticalStrut(30));
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;/*w ww  .  j  a v  a2 s. c  o  m*/
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);

    left.add(Box.createGlue());

    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.add(left, BorderLayout.CENTER);

    Box right = Box.createVerticalBox();
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Dashed"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Thick"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Rounded"));

    right.add(Box.createGlue());

    JPanel rightPanel = new JPanel(new BorderLayout());
    rightPanel.add(right, BorderLayout.CENTER);

    Box top = Box.createHorizontalBox();
    top.add(leftPanel);
    top.add(Box.createHorizontalStrut(5));
    top.add(rightPanel);

    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);

    BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);

    content.setLayout(box);
    content.add(top);

    aWindow.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box left = Box.createVerticalBox();
    left.add(Box.createVerticalStrut(30));
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;//from   ww  w .j  a v  a  2 s.c  o  m
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);

    left.add(Box.createGlue());

    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Color"));
    leftPanel.add(left, BorderLayout.CENTER);

    Box right = Box.createVerticalBox();
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Dashed"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Thick"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Rounded"));

    right.add(Box.createGlue());

    JPanel rightPanel = new JPanel(new BorderLayout());
    rightPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Properties"));
    rightPanel.add(right, BorderLayout.CENTER);

    Box top = Box.createHorizontalBox();
    top.add(leftPanel);
    top.add(Box.createHorizontalStrut(5));
    top.add(rightPanel);

    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);

    BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);

    content.setLayout(box);
    content.add(top);

    aWindow.setVisible(true);
}

From source file:Main.java

public static JPanel createFlow(final int align, final int gap, final Object... components) {
    final JPanel jp = new JPanel(new FlowLayout(align, gap, gap));
    jp.setBorder(BorderFactory.createEmptyBorder());
    for (final Object component : components) {
        if (component instanceof Component) {
            initComponentHeight((Component) component);
            jp.add((Component) component);
        } else if (component instanceof Number) {
            jp.add(Box.createHorizontalStrut(((Number) component).intValue()));
        }/* ww  w  . j  a  v a2s.  c  om*/
    }
    return jp;
}