Example usage for javax.swing Box createHorizontalBox

List of usage examples for javax.swing Box createHorizontalBox

Introduction

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

Prototype

public static Box createHorizontalBox() 

Source Link

Document

Creates a Box that displays its components from left to right.

Usage

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 ww  .  j  a  v  a  2  s.c om*/

    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: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   ww w. jav  a2  s  .  c  o  m
    }
    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: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  www.  j  a v  a2 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;/*  www.j  a v  a2 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:edu.stanford.smi.protegex.owl.ui.widget.HTMLEditorPanel.java

public HTMLEditorPanel(OWLModel owlModel, String text, String language) {
    setLayout(new BorderLayout());
    ekitCore = new EkitCore(null, null, null, null, false, true, true, null, null, false, false);
    ekitCore.setDocumentText(text);/*from  www  .  ja v a2 s. c o m*/
    JPanel ekitTopPanel = new JPanel();
    ekitTopPanel.setLayout(new GridLayout(2, 1));
    ekitTopPanel.add(ekitCore.getMenuBar());
    ekitTopPanel.add(ekitCore.getToolBar(true));
    if (owlModel != null) {
        languageComboBox = ComponentUtil.createLanguageComboBox(owlModel, language);
        Box languagePanel = Box.createHorizontalBox();
        languagePanel.add(new JLabel("Language: "));
        languagePanel.add(languageComboBox);
        add(BorderLayout.SOUTH, languagePanel);
    }
    add(BorderLayout.NORTH, ekitTopPanel);
    add(BorderLayout.CENTER, ekitCore);
}

From source file:sample.fa.ScriptRunnerApplication.java

void createGUI() {
    Box buttonBox = Box.createHorizontalBox();

    JButton loadButton = new JButton("Load");
    loadButton.addActionListener(this::loadScript);
    buttonBox.add(loadButton);/*  w  ww. j  a va2 s .  com*/

    JButton saveButton = new JButton("Save");
    saveButton.addActionListener(this::saveScript);
    buttonBox.add(saveButton);

    JButton executeButton = new JButton("Execute");
    executeButton.addActionListener(this::executeScript);
    buttonBox.add(executeButton);

    languagesModel = new DefaultComboBoxModel();

    ScriptEngineManager sem = new ScriptEngineManager();
    for (ScriptEngineFactory sef : sem.getEngineFactories()) {
        languagesModel.addElement(sef.getScriptEngine());
    }

    JComboBox<ScriptEngine> languagesCombo = new JComboBox<>(languagesModel);
    JLabel languageLabel = new JLabel();
    languagesCombo.setRenderer((JList<? extends ScriptEngine> list, ScriptEngine se, int index,
            boolean isSelected, boolean cellHasFocus) -> {
        ScriptEngineFactory sef = se.getFactory();
        languageLabel.setText(sef.getEngineName() + " - " + sef.getLanguageName() + " (*."
                + String.join(", *.", sef.getExtensions()) + ")");
        return languageLabel;
    });
    buttonBox.add(Box.createHorizontalGlue());
    buttonBox.add(languagesCombo);

    scriptContents = new JTextArea();
    scriptContents.setRows(8);
    scriptContents.setColumns(40);

    scriptResults = new JTextArea();
    scriptResults.setEditable(false);
    scriptResults.setRows(8);
    scriptResults.setColumns(40);

    JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scriptContents, scriptResults);

    JFrame frame = new JFrame("Script Runner");
    frame.add(buttonBox, BorderLayout.NORTH);
    frame.add(jsp, BorderLayout.CENTER);

    frame.pack();
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    frame.setVisible(true);
}

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);/*  www  . j  a va2  s  .  c  om*/
    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:com.diversityarrays.wizard.WizardRunner.java

protected void handleWizardEnded(WizardEndedReason reason) {

    if (handler != null) {
        handler.execute(reason);//from  w w w.ja  v a  2  s.co  m

    }

    status.setText(reason.toString());
    WizardRunner.this.setEnabled(false);

    RootPaneContainer rpc = ((RootPaneContainer) window);

    rpc.getContentPane().remove(this);

    Container cp = rpc.getContentPane();
    cp.removeAll();

    JLabel label = new JLabel(reason.toString());

    Box n = Box.createHorizontalBox();
    n.add(Box.createHorizontalGlue());
    n.add(label);
    n.add(Box.createHorizontalGlue());
    cp.add(n, BorderLayout.CENTER);

    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());
    box.add(new JButton(closeMe));
    box.add(Box.createHorizontalStrut(20));
    cp.add(box, BorderLayout.SOUTH);

    cp.setBackground(Color.LIGHT_GRAY);

    Dimension d = window.getSize();
    ++d.width;
    window.setSize(d);

}

From source file:com.diversityarrays.kdxplore.field.CollectionPathSetupDialog.java

public CollectionPathSetupDialog(Window owner, String title, boolean wantPlotsPerGroup) {
    super(owner, title, ModalityType.APPLICATION_MODAL);

    Box buttons = Box.createHorizontalBox();
    buttons.add(Box.createHorizontalGlue());
    buttons.add(new JButton(cancelAction));
    buttons.add(new JButton(useAction));

    Container cp = getContentPane();
    if (wantPlotsPerGroup) {
        plotsPerGroupChoice.addItemListener(new ItemListener() {
            @Override/*from  w  ww . j  a  v a2 s.  c  om*/
            public void itemStateChanged(ItemEvent e) {
                plotsPerGroup = (PlotsPerGroup) plotsPerGroupChoice.getSelectedItem();
            }
        });

        JPanel ppgPanel = new JPanel();
        GBH gbh = new GBH(ppgPanel);
        int y = 0;
        gbh.add(0, y, 1, 1, GBH.NONE, 1, 1, GBH.EAST, "Plots Per Group:");
        gbh.add(1, y, 1, 1, GBH.HORZ, 1, 1, GBH.WEST, plotsPerGroupChoice);
        ++y;
        cp.add(ppgPanel, BorderLayout.NORTH);
    }

    cp.add(odtChoicePanel, BorderLayout.CENTER);
    cp.add(buttons, BorderLayout.SOUTH);

    pack();
}

From source file:com.diversityarrays.kdxplore.field.CollectionPathPanel.java

public CollectionPathPanel(boolean wantPlotsPerGroup, BiConsumer<VisitOrder2D, PlotsPerGroup> onChoiceChanged,
        List<? extends OrOrTr> onlyThese) {
    super(new BorderLayout());

    plotsPerGroupChoice.setSelectedItem(plotsPerGroup);
    odtChoicePanel.setOrOrTr(visitOrder);

    this.onChoiceChanged = onChoiceChanged;

    if (wantPlotsPerGroup) {
        plotsPerGroupChoice.addItemListener(new ItemListener() {
            @Override/*from w ww  . j  av a 2 s. c o m*/
            public void itemStateChanged(ItemEvent e) {
                plotsPerGroup = (PlotsPerGroup) plotsPerGroupChoice.getSelectedItem();
                onChoiceChanged.accept(visitOrder, plotsPerGroup);
            }
        });

        Box top = Box.createHorizontalBox();
        top.add(new JLabel("Plots Per Group:"));
        top.add(plotsPerGroupChoice);
        top.add(Box.createHorizontalGlue());

        add(top, BorderLayout.NORTH);
    }

    if (!Check.isEmpty(onlyThese)) {
        OrOrTr first = onlyThese.get(0);

        odtChoicePanel.setOnlyAllow(onlyThese.toArray(new OrOrTr[onlyThese.size()]));

        String msg;
        if (onlyThese.size() == 1) {
            msg = "<HTML>For now, only supporting:<BR>" + onlyThese.get(0).toString();
        } else {
            msg = onlyThese.stream().map(oot -> oot.toString())
                    .collect(Collectors.joining("<BR>", "<HTML>For now, only supporting:<BR>", ""));
        }

        JLabel label = new JLabel(msg, JLabel.CENTER);
        label.setForeground(Color.RED);
        add(label, BorderLayout.SOUTH);
    }

    add(odtChoicePanel, BorderLayout.CENTER);
}