Java tutorial
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JToolBar; public class ToolBarwithCheckBox extends JFrame { public static void main(String[] args) { ToolBarwithCheckBox that = new ToolBarwithCheckBox(); that.setVisible(true); } public ToolBarwithCheckBox() { setSize(450, 350); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(new ToolbarPanel(), BorderLayout.SOUTH); } } class ToolbarPanel extends JPanel { public ToolbarPanel() { setLayout(new BorderLayout()); JToolBar toolbar = new JToolBar(); for (int i = 1; i < 4; i++) { JCheckBox cbox = new JCheckBox("Checkbox #" + i); toolbar.add(cbox); cbox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBox source = (JCheckBox) (e.getSource()); System.out.println("Toolbar " + source.getText()); } }); } add(toolbar, BorderLayout.NORTH); } }