Example usage for javax.swing Box createVerticalBox

List of usage examples for javax.swing Box createVerticalBox

Introduction

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

Prototype

public static Box createVerticalBox() 

Source Link

Document

Creates a Box that displays its components from top to bottom.

Usage

From source file:Main.java

public Main() {
    JFormattedTextField formattedField = null;
    try {/*  www  .  jav a  2  s . c  o  m*/
        MaskFormatter dateMask = new MaskFormatter("##/##/####");
        formattedField = new JFormattedTextField(dateMask);
    } catch (ParseException ex) {
        System.out.println(ex);
    }
    formattedField.setColumns(10);
    formattedField.setInputVerifier(getInputVerifier());

    JTextField field = new JTextField(10);
    format.setLenient(false);

    Box box = Box.createVerticalBox();
    box.add(formattedField);
    box.add(Box.createVerticalStrut(10));
    box.add(field);
    box.setBorder(new EmptyBorder(10, 10, 10, 10));

    JFrame frame = new JFrame();
    frame.add(box);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public Main(int axis) {
    super(BoxLayout.Y_AXIS);
    container = new Box(axis);
    container.setAlignmentX(Box.LEFT_ALIGNMENT);
    add(container);//  w  w  w. jav  a 2  s  . c  o m

    JTextArea text = new JTextArea();
    container.add(new JScrollPane(text));

    JButton split = new JButton("Split");
    split.setAlignmentX(Box.LEFT_ALIGNMENT);
    split.addActionListener(e -> {
        JTextArea t = new JTextArea();
        container.add(new JScrollPane(t));
        revalidate();
    });
    add(split);

    JButton axisChanger = new JButton("Change Axis");
    axisChanger.setAlignmentX(Box.LEFT_ALIGNMENT);
    axisChanger.addActionListener(e -> {
        Box newContainer;
        if (((BoxLayout) container.getLayout()).getAxis() == BoxLayout.X_AXIS) {
            newContainer = Box.createVerticalBox();
        } else {
            newContainer = Box.createHorizontalBox();
        }
        for (Component c : container.getComponents()) {
            container.remove(c);
            newContainer.add(c);
        }
        remove(container);
        add(newContainer, 0);
        container = newContainer;
        container.setAlignmentX(Box.LEFT_ALIGNMENT);
        revalidate();
    });
    add(axisChanger);

}

From source file:Main.java

public Main() {

    for (int i = 0; i < 15; i++) {
        ((DefaultListModel<String>) sourceList.getModel()).add(i, "A " + i);
        ((DefaultListModel<String>) destList.getModel()).add(i, "B " + i);
    }//from w w  w .  j a v a2 s  . com
    Box nameBox = Box.createHorizontalBox();
    nameBox.add(new JLabel("New:"));
    nameBox.add(newTextField);

    Box sourceBox = Box.createVerticalBox();
    sourceBox.add(new JLabel("Source"));
    sourceBox.add(new JScrollPane(sourceList));

    Box destBox = Box.createVerticalBox();
    destBox.add(new JLabel("Destination"));
    destBox.add(new JScrollPane(destList));

    Box listBox = Box.createHorizontalBox();
    listBox.add(sourceBox);
    listBox.add(destBox);

    Box allBox = Box.createVerticalBox();
    allBox.add(nameBox);
    allBox.add(listBox);

    this.getContentPane().add(allBox, BorderLayout.CENTER);

    sourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    destList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    newTextField.setDragEnabled(true);
    sourceList.setDragEnabled(true);
    destList.setDragEnabled(true);

    sourceList.setDropMode(DropMode.INSERT);
    destList.setDropMode(DropMode.INSERT);

    sourceList.setTransferHandler(new ListTransferHandler());
    destList.setTransferHandler(new ListTransferHandler());
}

From source file:FieldValidator.java

private static JComponent createContent() {
    Dimension labelSize = new Dimension(80, 20);

    Box box = Box.createVerticalBox();

    // A single LayerUI for all the fields.
    LayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();

    // Number field.
    JLabel numberLabel = new JLabel("Number:");
    numberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    numberLabel.setPreferredSize(labelSize);

    NumberFormat numberFormat = NumberFormat.getInstance();
    JFormattedTextField numberField = new JFormattedTextField(numberFormat);
    numberField.setColumns(16);//from   w ww  .  j a va 2  s . c o m
    numberField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    numberField.setValue(42);

    JPanel numberPanel = new JPanel();
    numberPanel.add(numberLabel);
    numberPanel.add(new JLayer<JFormattedTextField>(numberField, layerUI));

    // Date field.
    JLabel dateLabel = new JLabel("Date:");
    dateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    dateLabel.setPreferredSize(labelSize);

    DateFormat dateFormat = DateFormat.getDateInstance();
    JFormattedTextField dateField = new JFormattedTextField(dateFormat);
    dateField.setColumns(16);
    dateField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    dateField.setValue(new java.util.Date());

    JPanel datePanel = new JPanel();
    datePanel.add(dateLabel);
    datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));

    // Time field.
    JLabel timeLabel = new JLabel("Time:");
    timeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    timeLabel.setPreferredSize(labelSize);

    DateFormat timeFormat = DateFormat.getTimeInstance();
    JFormattedTextField timeField = new JFormattedTextField(timeFormat);
    timeField.setColumns(16);
    timeField.setFocusLostBehavior(JFormattedTextField.PERSIST);
    timeField.setValue(new java.util.Date());

    JPanel timePanel = new JPanel();
    timePanel.add(timeLabel);
    timePanel.add(new JLayer<JFormattedTextField>(timeField, layerUI));

    // Put them all in the box.
    box.add(Box.createGlue());
    box.add(numberPanel);
    box.add(Box.createGlue());
    box.add(datePanel);
    box.add(Box.createGlue());
    box.add(timePanel);

    return box;
}

From source file:PizzaGridBagLayout.java

public PizzaGridBagLayout() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    addItem(panel1, new JLabel("Name:"), 0, 0, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Phone:"), 0, 1, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Address:"), 0, 2, 1, 1, GridBagConstraints.EAST);

    addItem(panel1, name, 1, 0, 2, 1, GridBagConstraints.WEST);
    addItem(panel1, phone, 1, 1, 1, 1, GridBagConstraints.WEST);
    addItem(panel1, address, 1, 2, 2, 1, GridBagConstraints.WEST);

    Box sizeBox = Box.createVerticalBox();
    ButtonGroup sizeGroup = new ButtonGroup();
    sizeGroup.add(small);/*from ww w . j  a v a  2 s.  c o  m*/
    sizeGroup.add(medium);
    sizeGroup.add(large);
    sizeBox.add(small);
    sizeBox.add(medium);
    sizeBox.add(large);
    sizeBox.setBorder(BorderFactory.createTitledBorder("Size"));
    addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);

    Box styleBox = Box.createVerticalBox();

    ButtonGroup styleGroup = new ButtonGroup();
    styleGroup.add(thin);
    styleGroup.add(thick);
    styleBox.add(thin);
    styleBox.add(thick);
    styleBox.setBorder(BorderFactory.

            createTitledBorder("Style"));
    addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);

    Box topBox = Box.createVerticalBox();
    ButtonGroup topGroup = new ButtonGroup();
    topGroup.add(pepperoni);
    topGroup.add(mushrooms);
    topGroup.add(anchovies);
    topBox.add(pepperoni);
    topBox.add(mushrooms);
    topBox.add(anchovies);
    topBox.setBorder(BorderFactory.createTitledBorder("Toppings"));
    addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);

    Box buttonBox = Box.createHorizontalBox();
    buttonBox.add(okButton);
    buttonBox.add(Box.createHorizontalStrut(20));
    buttonBox.add(closeButton);
    addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);

    this.add(panel1);
    this.pack();
    this.setVisible(true);
}

From source file:be.fedict.eid.tsl.Pkcs11CallbackHandler.java

private char[] getPin() {
    Box mainPanel = Box.createVerticalBox();

    Box passwordPanel = Box.createHorizontalBox();
    JLabel promptLabel = new JLabel("eID PIN:");
    passwordPanel.add(promptLabel);/*from  ww  w  . j  av  a  2  s  . c o  m*/
    passwordPanel.add(Box.createHorizontalStrut(5));
    JPasswordField passwordField = new JPasswordField(8);
    passwordPanel.add(passwordField);
    mainPanel.add(passwordPanel);

    Component parentComponent = null;
    int result = JOptionPane.showOptionDialog(parentComponent, mainPanel, "eID PIN?",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (result == JOptionPane.OK_OPTION) {
        char[] pin = passwordField.getPassword();
        return pin;
    }
    throw new RuntimeException("operation canceled.");
}

From source file:BoxLayoutTest.java

public Box createBox(boolean horizontal, boolean strutsAndGlue) {
    Box b;/*w w  w.ja  v  a  2  s  . c  o m*/
    if (horizontal)
        b = Box.createHorizontalBox();
    else
        b = Box.createVerticalBox();

    b.add(new JLabel("Name: "));
    b.add(new JTextField());

    if (strutsAndGlue)
        if (horizontal)
            b.add(Box.createHorizontalStrut(5));
        else
            b.add(Box.createVerticalStrut(5));

    b.add(new JLabel("Password: "));
    b.add(new JTextField());

    if (strutsAndGlue)
        b.add(Box.createGlue());

    b.add(new JButton("Ok"));

    return b;
}

From source file:layout.BoxLayoutDemo2.java

public void populateContentPane(Container contentPane) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    //Create the rectangles.
    int shortSideSize = 15;
    for (int i = 0; i < NUM_COMPONENTS; i++) {
        if (sizeIsRandom) {
            shortSideSize = (int) (30.0 * Math.random()) + 30;
        } else {/*from  w  w w . ja v a2  s .c  om*/
            shortSideSize += 10;
        }
        bldComponent[i] = new BLDComponent(xAlignment[i], hue[i], shortSideSize, restrictSize, sizeIsRandom,
                String.valueOf(i));
        panel.add(bldComponent[i]);
    }

    //Create the instructions.
    JLabel label = new JLabel("Click a rectangle to " + "change its X alignment.");
    JCheckBox cb = new JCheckBox("Restrict maximum rectangle size.");
    cb.setSelected(restrictSize);
    cb.addItemListener(this);

    panel.setBorder(BorderFactory.createLineBorder(Color.red));

    Box box = Box.createVerticalBox();
    box.add(label);
    box.add(cb);

    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(box, BorderLayout.PAGE_END);
}

From source file:BoxLayoutPane.java

public BoxLayoutPane() {
    // Use a BorderLayout layout manager to arrange various Box components
    this.setLayout(new BorderLayout());

    // Give the entire panel a margin by adding an empty border
    // We could also do this by overriding getInsets()
    this.setBorder(new EmptyBorder(10, 10, 10, 10));

    // Add a plain row of buttons along the top of the pane
    Box row = Box.createHorizontalBox();
    for (int i = 0; i < 4; i++) {
        JButton b = new JButton("B" + i);
        b.setFont(new Font("serif", Font.BOLD, 12 + i * 2));
        row.add(b);/*from w w  w. j  av  a2s.c  om*/
    }
    this.add(row, BorderLayout.NORTH);

    // Add a plain column of buttons along the right edge
    // Use BoxLayout with a different kind of Swing container
    // Give the column a border: can't do this with the Box class
    JPanel col = new JPanel();
    col.setLayout(new BoxLayout(col, BoxLayout.Y_AXIS));
    col.setBorder(new TitledBorder(new EtchedBorder(), "Column"));
    for (int i = 0; i < 4; i++) {
        JButton b = new JButton("Button " + i);
        b.setFont(new Font("sanserif", Font.BOLD, 10 + i * 2));
        col.add(b);
    }
    this.add(col, BorderLayout.EAST); // Add column to right of panel

    // Add a button box along the bottom of the panel.
    // Use "Glue" to space the buttons evenly
    Box buttonbox = Box.createHorizontalBox();
    buttonbox.add(Box.createHorizontalGlue()); // stretchy space
    buttonbox.add(new JButton("Okay"));
    buttonbox.add(Box.createHorizontalGlue()); // stretchy space
    buttonbox.add(new JButton("Cancel"));
    buttonbox.add(Box.createHorizontalGlue()); // stretchy space
    buttonbox.add(new JButton("Help"));
    buttonbox.add(Box.createHorizontalGlue()); // stretchy space
    this.add(buttonbox, BorderLayout.SOUTH);

    // Create a component to display in the center of the panel
    JTextArea textarea = new JTextArea();
    textarea.setText("This component has 12-pixel margins on left and top"
            + " and has 72-pixel margins on right and bottom.");
    textarea.setLineWrap(true);
    textarea.setWrapStyleWord(true);

    // Use Box objects to give the JTextArea an unusual spacing
    // First, create a column with 3 kids. The first and last kids
    // are rigid spaces. The middle kid is the text area
    Box fixedcol = Box.createVerticalBox();
    fixedcol.add(Box.createVerticalStrut(12)); // 12 rigid pixels
    fixedcol.add(textarea); // Component fills in the rest
    fixedcol.add(Box.createVerticalStrut(72)); // 72 rigid pixels

    // Now create a row. Give it rigid spaces on the left and right,
    // and put the column from above in the middle.
    Box fixedrow = Box.createHorizontalBox();
    fixedrow.add(Box.createHorizontalStrut(12));
    fixedrow.add(fixedcol);
    fixedrow.add(Box.createHorizontalStrut(72));

    // Now add the JTextArea in the column in the row to the panel
    this.add(fixedrow, BorderLayout.CENTER);
}

From source file:org.jdal.swing.TableEditor.java

/**
 * {@inheritDoc}/*from   w w  w  . j  a  va 2s. c o  m*/
 */
@Override
protected JComponent buildPanel() {
    Box box = Box.createVerticalBox();
    Container tablePanel = createTablePanel();
    box.add(tablePanel);
    box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    return box;
}