Java JButton Create createButtonRow(List buttons)

Here you can find the source of createButtonRow(List buttons)

Description

Takes the given list of buttons and creates a horizontal panel containing them, with standard spacing and size.

License

BSD License

Parameter

Parameter Description
buttons the list of buttons in the row

Return

the button row

Declaration

public static JComponent createButtonRow(List buttons) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.awt.Dimension;

import java.util.Iterator;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;

public class Main {
    /** default border around components */
    public static final int DEFAULT_BORDER = 6;
    /** default spacing between buttons */
    public static final int DEFAULT_STRUT = 6;

    /**//from  ww w .  j a va  2 s .  c  o  m
     * Takes the given list of buttons and creates a horizontal
     * panel containing them, with standard spacing and size.
     * @param buttons the list of buttons in the row
     * @return the button row
     */
    public static JComponent createButtonRow(List buttons) {
        equalizeSizes(buttons);
        Box row = new Box(BoxLayout.X_AXIS);
        row.setBorder(BorderFactory.createEmptyBorder(DEFAULT_BORDER, 0, DEFAULT_BORDER, 0));

        Iterator buttonIt = buttons.iterator();
        while (buttonIt.hasNext()) {
            row.add((JComponent) buttonIt.next());
            if (buttonIt.hasNext())
                row.add(Box.createRigidArea(new Dimension(DEFAULT_STRUT, 0))); // leave space between buttons
        }
        return row;
    }

    /**
     * Takes a list of JComponents and sets them all to the
     * size of the largest component.
     * @param items the list of items to equalize
     */
    public static void equalizeSizes(List items) {
        Dimension maxDim = new Dimension(0, 0);
        Iterator itemIt = items.iterator();
        while (itemIt.hasNext()) {
            Dimension dim = ((JComponent) itemIt.next()).getPreferredSize();
            double height = Math.max(maxDim.getHeight(), dim.getHeight());
            double width = Math.max(maxDim.getWidth(), dim.getWidth());
            maxDim.setSize(width, height);
        }

        itemIt = items.iterator();
        while (itemIt.hasNext()) {
            JComponent c = (JComponent) itemIt.next();
            c.setPreferredSize(maxDim);
            c.setMaximumSize(maxDim);
        }
    }
}

Related

  1. createButton(String text, String icon)
  2. createButton16(final ImageIcon icon)
  3. createButtonFooter(JButton ok, JButton cancel)
  4. createButtonGroup(javax.swing.AbstractButton... buttons)
  5. createButtonPanel()
  6. createButtonsPanel(JComponent... components)
  7. createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)
  8. createCustomButton(Action a)
  9. CreateFlatButton()