Toolbar with CheckBox : JToolBar « Swing « Java Tutorial






Toolbar with CheckBox
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);
  }
}








14.57.JToolBar
14.57.1.JToolbar: Toolbars provide a quick access to the most frequently used commands.
14.57.2.A Complete JToolBar Usage ExampleA Complete JToolBar Usage Example
14.57.3.Create a vertical toolbar
14.57.4.Adding separator for JToolBarAdding separator for JToolBar
14.57.5.Add various buttons to the toolbar
14.57.6.Customizing Tool BarsCustomizing Tool Bars
14.57.7.Toolbar with CheckBoxToolbar with CheckBox
14.57.8.Swing ToolBar with Image button
14.57.9.Preventing a JToolbar Container from Floating
14.57.10.Highlighting Buttons in a JToolbar Container While Under the Cursor
14.57.11.Determining When a Floatable JToolBar Container Changes Orientation
14.57.12.Customizing JToolBar Look and Feel
14.57.13.This class represents a separator for the toolbar buttons.
14.57.14.ToolBar button