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:Toolbars.java

public static void main(String[] args) {
    JToolBar toolbar1 = new JToolBar();
    JToolBar toolbar2 = new JToolBar();

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JButton newb = new JButton(new ImageIcon("new.png"));
    JButton openb = new JButton(new ImageIcon("open.png"));
    JButton saveb = new JButton(new ImageIcon("save.png"));

    toolbar1.add(newb);
    toolbar1.add(openb);//  w ww .  j  a v a 2 s  . co  m
    toolbar1.add(saveb);
    toolbar1.setAlignmentX(0);

    JButton exitb = new JButton(new ImageIcon("exit.png"));
    toolbar2.add(exitb);
    toolbar2.setAlignmentX(0);

    exitb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    panel.add(toolbar1);
    panel.add(toolbar2);

    JFrame f = new JFrame();
    f.add(panel, BorderLayout.NORTH);

    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    final int STRING_POSITION = 1;
    Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" },
            { Color.BLACK, "BLACK" }, null, // separator
            { Color.CYAN, "CYAN" } };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new TheActionListener();

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

    for (Object[] color : buttonColors) {
        if (color == null) {
            toolbar.addSeparator();
        } else {
            Icon icon = MetalIconFactory.getTreeComputerIcon();
            JButton button = new JButton(icon);
            button.setActionCommand((String) color[STRING_POSITION]);
            button.addActionListener(actionListener);
            toolbar.add(button);
        }
    }

    Action action = new ShowAction(toolbar);
    JButton button = new JButton(action);
    toolbar.add(button);

    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:ListProperties.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("List Properties");

    final CustomTableModel model = new CustomTableModel();
    model.uiDefaultsUpdate(UIManager.getDefaults());
    TableSorter sorter = new TableSorter(model);

    JTable table = new JTable(sorter);
    TableHeaderSorter.install(sorter, table);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

    UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            final String lafClassName = actionEvent.getActionCommand();
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(lafClassName);
                        SwingUtilities.updateComponentTreeUI(frame);
                        // Added
                        model.uiDefaultsUpdate(UIManager.getDefaults());
                    } catch (Exception exception) {
                        JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF",
                                JOptionPane.ERROR_MESSAGE);
                    }//from w w  w  . j av  a  2 s . co  m
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    };

    JToolBar toolbar = new JToolBar();
    for (int i = 0, n = looks.length; i < n; i++) {
        JButton button = new JButton(looks[i].getName());
        button.setActionCommand(looks[i].getClassName());
        button.addActionListener(actionListener);
        toolbar.add(button);
    }

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    JScrollPane scrollPane = new JScrollPane(table);
    content.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 400);
    frame.setVisible(true);
}

From source file:SwingToolBarSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand());
        }// w ww  . j a va 2s.co m
    };

    JFrame frame = new JFrame("JToolBar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToolBar toolbar = new JToolBar();
    toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);

    for (int i = 0, n = buttonColors.length; i < n; i++) {
        Object color[] = buttonColors[i];
        if (color == null) {
            toolbar.addSeparator();
        } else {
            Icon icon = new DiamondIcon((Color) color[COLOR_POSITION], true, 20, 20);
            JButton button = new JButton(icon);
            button.setActionCommand((String) color[STRING_POSITION]);
            button.addActionListener(actionListener);
            toolbar.add(button);
        }
    }

    Action action = new ActionMenuSample.ShowAction(frame);
    toolbar.add(action);

    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:ScribbleDragAndDrop.java

/**
 * The main method. Creates a simple application using this class. Note the
 * buttons for switching between draw mode and drag mode.
 *///from w w  w.  j  ava 2  s  .  c  om
public static void main(String[] args) {
    // Create a frame and put a scribble pane in it
    JFrame frame = new JFrame("ScribbleDragAndDrop");
    final ScribbleDragAndDrop scribblePane = new ScribbleDragAndDrop();
    frame.getContentPane().add(scribblePane, BorderLayout.CENTER);

    // Create two buttons for switching modes
    JToolBar toolbar = new JToolBar();
    ButtonGroup group = new ButtonGroup();
    JToggleButton draw = new JToggleButton("Draw");
    JToggleButton drag = new JToggleButton("Drag");
    draw.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scribblePane.setDragMode(false);
        }
    });
    drag.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scribblePane.setDragMode(true);
        }
    });
    group.add(draw);
    group.add(drag);
    toolbar.add(draw);
    toolbar.add(drag);
    frame.getContentPane().add(toolbar, BorderLayout.NORTH);

    // Start off in drawing mode
    draw.setSelected(true);
    scribblePane.setDragMode(false);

    // Pop up the window
    frame.setSize(400, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static void showFrameWithToolBar(String toolBarPosition) {

    JPanel gui = new JPanel(new BorderLayout());

    JToolBar tb = new JToolBar();

    gui.add(tb, toolBarPosition);/*from  w  w  w  .  j  a va  2 s  . com*/
    tb.add(new JButton("Button 1"));
    tb.add(new JButton("Button 2"));
    tb.addSeparator();
    tb.add(new JButton("Button 3"));
    tb.add(new JCheckBox("Check 1", true));

    JFrame f = new JFrame(toolBarPosition);
    f.setContentPane(gui);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationByPlatform(true);
    f.pack();

    f.setSize(400, 120);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Populates the toolbar with actions./*from www  .j  a  v a 2s . c o m*/
 * 
 * @param toolBar
 * @param actionMap
 * @param list
 */
public static void populateToolbar(JToolBar toolBar, ActionMap actionMap, List<Object> list) {
    for (Object object : list) {
        if (object instanceof Action) {
            toolBar.add((Action) object);
        } else {
            toolBar.add(actionMap.get(object));
        }
    }
}

From source file:Main.java

/**
 * Populates a {@link JToolBar} with the list items and {@link ActionMap}.
 * //from  w w w.  j  a  v  a  2s  .  co  m
 * @param toolBar
 * @param actionMap
 * @param items
 */
public static void populate(JToolBar toolBar, ActionMap actionMap, List<?> items) {
    toolBar.setOpaque(false);
    for (Object item : items) {
        if (item instanceof Component) {
            toolBar.add((Component) item);
        } else if (item == null) {
            toolBar.addSeparator();
        } else {
            final Action action = item instanceof Action ? (Action) item : actionMap.get(item);
            final AbstractButton button = createToolbarItem(action);
            toolBar.add(button);
        }
    }
}

From source file:Main.java

public Main() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/HTML");
    editorPane.setEditorKit(new HTMLEditorKit());
    editorPane.setText("<hr>Welcome to <b>java2s.com!</b><hr>");
    JToolBar bar = new JToolBar();
    bar.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
    bar.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
    bar.add(new StyledEditorKit.FontSizeAction("12", 12));
    bar.add(new StyledEditorKit.FontSizeAction("14", 14));
    bar.add(new StyledEditorKit.FontSizeAction("16", 16));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.add(bar, BorderLayout.NORTH);
    this.add(editorPane, BorderLayout.CENTER);
    this.pack();//  w  w w .ja v  a  2  s  .c o m
    this.setVisible(true);
}

From source file:Main.java

public void build() {
    setSize(600, 600);/*from ww w .j av  a2s.c o m*/
    JTextPane textPane = new JTextPane();

    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));

    JTextArea changeLog = new JTextArea(5, 30);
    changeLog.setEditable(false);
    JScrollPane scrollPaneForLog = new JScrollPane(changeLog);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, scrollPaneForLog);
    splitPane.setOneTouchExpandable(true);

    JPanel statusPane = new JPanel(new GridLayout(1, 1));
    JLabel caretListenerLabel = new JLabel("Caret Status");
    statusPane.add(caretListenerLabel);

    JToolBar toolBar = new JToolBar();
    toolBar.add(new JButton("Btn1"));
    toolBar.add(new JButton("Btn2"));
    toolBar.add(new JButton("Btn3"));
    toolBar.add(new JButton("Btn4"));

    getContentPane().add(toolBar, BorderLayout.PAGE_START);
    getContentPane().add(splitPane, BorderLayout.CENTER);
    getContentPane().add(statusPane, BorderLayout.PAGE_END);

    JMenu editMenu = new JMenu("test");
    JMenu styleMenu = new JMenu("test");
    JMenuBar mb = new JMenuBar();
    mb.add(editMenu);
    mb.add(styleMenu);
    setJMenuBar(mb);

}