Java JButton Create createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)

Here you can find the source of createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)

Description

Create a panel containing a checkbox.

License

Open Source License

Parameter

Parameter Description
boxlabel the string to use for the box title
buttons the list of button names
checked the initial state of each checkbox item
al the actionlistener to invoke for a button. the actioncommand is set to a string containing the integer index of the button.

Return

the JPanel the the buttons are placed in

Declaration

public static JPanel createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al) 

Method Source Code

//package com.java2s;

import java.awt.GridLayout;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JCheckBox;

import javax.swing.JPanel;

public class Main {
    /**// w  w  w  .  j a va2  s.c om
     * Create a panel containing a checkbox.
     * 
     * @param boxlabel the string to use for the box title
     * @param buttons the list of button names
     * @param checked the initial state of each checkbox item
     * @param al the actionlistener to invoke for a button. the
     *        actioncommand is set to a string containing the integer
     *        index of the button.
     * @return the JPanel the the buttons are placed in
     * @see javax.swing.AbstractButton#setActionCommand(String)
     * @see javax.swing.JCheckBox
     */
    public static JPanel createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al) {

        JPanel jp = createPaletteJPanel(boxlabel);
        for (int j = 0; j < buttons.length; j++) {
            JCheckBox jcb = new JCheckBox(buttons[j]);
            jcb.setActionCommand(Integer.toString(j));//index of
                                                      // checked
            if (al != null)
                jcb.addActionListener(al);
            jcb.setSelected(checked[j]);
            jp.add(jcb);
        }
        return jp;
    }

    /**
     * Create a panel with a border and title
     * 
     * @param title the title of the panel (null allowed)
     * @return the panel that got created
     */
    public static JPanel createPaletteJPanel(String title) {
        JPanel panel = new JPanel();

        if (title != null) {
            panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
        } else {
            panel.setBorder(BorderFactory.createEtchedBorder());
        }

        panel.setLayout(new GridLayout(0, 1));
        return panel;
    }
}

Related

  1. createButtonFooter(JButton ok, JButton cancel)
  2. createButtonGroup(javax.swing.AbstractButton... buttons)
  3. createButtonPanel()
  4. createButtonRow(List buttons)
  5. createButtonsPanel(JComponent... components)
  6. createCustomButton(Action a)
  7. CreateFlatButton()
  8. createGameRadioButton(String answer, int fontSize)
  9. createHyperlinkButton(String text, String tip, ActionListener action)