Here you can find the source of createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)
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. |
public static JPanel createCheckbox(String boxlabel, String[] buttons, boolean[] checked, ActionListener al)
//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; } }