Add various buttons to the toolbar : JToolBar « Swing « Java Tutorial






import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class Main {
  public static void main(String[] argv) throws Exception {
    JToolBar toolbar = new JToolBar();
    ImageIcon icon = new ImageIcon("icon.gif");
    Action action = new AbstractAction("Button Label", icon) {
      public void actionPerformed(ActionEvent evt) {
      }
    };

    JButton c1 = new JButton(action);
    c1.setText(null);
    c1.setMargin(new Insets(0, 0, 0, 0));
    toolbar.add(c1);

    JToggleButton c2 = new JToggleButton(action);
    c2.setText(null);
    c2.setMargin(new Insets(0, 0, 0, 0));
    toolbar.add(c2);

    JComboBox c3 = new JComboBox(new String[] { "A", "B", "C" });
    c3.setPrototypeDisplayValue("XXXXXXXX"); // Set a desired width
    c3.setMaximumSize(c3.getMinimumSize());
    toolbar.add(c3);
  }
}








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