Example usage for javax.swing JToolBar add

List of usage examples for javax.swing JToolBar add

Introduction

In this page you can find the example usage for javax.swing JToolBar add.

Prototype

public JButton add(Action a) 

Source Link

Document

Adds a new JButton which dispatches the action.

Usage

From source file:PrintHelloAction.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);//from   w  w w  .  ja  v  a2  s  .c o  m
    menu.add(new JMenuItem(printAction));
    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));
    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(true);
        }
    };
    enableButton.addActionListener(enableActionListener);
    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(false);
        }
    };
    disableButton.addActionListener(disableActionListener);
    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.putValue(Action.NAME, "Changed Action Value");
        }
    };
    relabelButton.addActionListener(relabelActionListener);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);
    frame.setJMenuBar(menuBar);
    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);//w ww . j  a  v a  2  s.c o  m
    menu.add(new JMenuItem(printAction));

    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));

    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(true);
        }
    };
    enableButton.addActionListener(enableActionListener);

    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(false);
        }
    };
    disableButton.addActionListener(disableActionListener);

    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.putValue(Action.NAME, "Hello, World");
        }
    };
    relabelButton.addActionListener(relabelActionListener);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);

    frame.setJMenuBar(menuBar);

    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToolBar toolBar = new JToolBar("Still draggable");

    toolBar.setFloatable(false);//from w w w  . ja v  a 2  s. co m
    toolBar.setRollover(true);

    toolBar.add(new JButton("New"));
    toolBar.addSeparator();
    toolBar.add(new JButton("Open"));

    frame.add(toolBar, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    Container pane = f.getContentPane();
    Icon icon = new RedOvalIcon();
    final Action action = new MyAction("Hello", icon);
    // Add tooltip
    action.putValue(Action.SHORT_DESCRIPTION, "World");
    JToolBar jt1 = new JToolBar();
    JButton b1 = new JButton(action);
    jt1.add(b1);
    pane.add(jt1, BorderLayout.NORTH);
    JToolBar jt2 = new JToolBar();
    JButton b2 = new JButton(action);
    jt2.add(b2);/*  w  w  w  .j  a va  2 s.c o  m*/
    pane.add(jt2, BorderLayout.SOUTH);
    JButton jb = new JButton("Toggle Action");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            action.setEnabled(!action.isEnabled());
        }
    });
    pane.add(jb, BorderLayout.CENTER);
    f.setSize(200, 200);
    f.show();
}

From source file:ToolBarSample.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JToolBar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToolBar toolbar = new JToolBar();
    toolbar.setRollover(true);/*from  ww w  .  j  av a  2s . com*/

    JButton button = new JButton("button");
    toolbar.add(button);
    toolbar.addSeparator();

    toolbar.add(new JButton("button 2"));

    toolbar.add(new JComboBox(new String[] { "A", "B", "C" }));

    Container contentPane = frame.getContentPane();
    contentPane.add(toolbar, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

From source file:SimpleToolbar.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    menubar.add(file);//  ww  w. j a  v a 2s  .c  o m
    f.setJMenuBar(menubar);

    JToolBar toolbar = new JToolBar();
    ImageIcon icon = new ImageIcon("exit.png");
    JButton exit = new JButton(icon);
    toolbar.add(exit);
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    f.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:ToolBarTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JToolBar bar;
    bar = new JToolBar();
    JToggleButton jb;// w  ww .j av a2 s.c o  m
    for (int i = 0; i < 8; i++) {
        jb = new JToggleButton("" + i);
        bar.add(jb);
        if (i == 5) {
            bar.addSeparator();
        }
    }
    contentPane.add(bar, BorderLayout.NORTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JToolBar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolbar = new JToolBar();
    Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
    JToggleButton button = new JToggleButton(icon);
    toolbar.add(button);
    icon = MetalIconFactory.getFileChooserHomeFolderIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);//from ww w.jav a  2  s  .  c  o  m
    icon = MetalIconFactory.getFileChooserListViewIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);
    f.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JToolbar Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JToolBar toolbar = new JToolBar();
    Icon icon = MetalIconFactory.getFileChooserDetailViewIcon();
    JToggleButton button = new JToggleButton(icon);
    toolbar.add(button);
    icon = MetalIconFactory.getFileChooserHomeFolderIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);/*  w ww .  ja  v a 2  s .c o  m*/
    icon = MetalIconFactory.getFileChooserListViewIcon();
    button = new JToggleButton(icon);
    toolbar.add(button);
    content.add(toolbar, BorderLayout.NORTH);
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:UndoableDrawingPanel2.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drawing Sample2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    UndoableDrawingPanel2 drawingPanel = new UndoableDrawingPanel2();

    UndoManager manager = new UndoManager();
    drawingPanel.addUndoableEditListener(manager);

    JToolBar toolbar = new JToolBar();
    toolbar.add(UndoManagerHelper.getUndoAction(manager));
    toolbar.add(UndoManagerHelper.getRedoAction(manager));

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    content.add(drawingPanel, BorderLayout.CENTER);
    frame.setSize(300, 150);//from  w ww. ja  v  a 2s.co m
    frame.setVisible(true);
}