Add Toolbars to JFrame - Java Swing

Java examples for Swing:JToolBar

Description

Add Toolbars to JFrame

Demo Code

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class Main extends JFrame {

  public Main() {
    super("FeedBar");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create icons
    ImageIcon loadIcon = new ImageIcon("load.gif");
    ImageIcon saveIcon = new ImageIcon("save.gif");
    ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
    ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
    // create buttons
    JButton load = new JButton("Load", loadIcon);
    JButton save = new JButton("Save", saveIcon);
    JButton subscribe = new JButton("Subscribe", subscribeIcon);
    JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
    // add buttons to toolbar
    JToolBar bar = new JToolBar();
    bar.add(load);/*  w  w w  .  j  a  v a2s.  c  o  m*/
    bar.add(save);
    bar.add(subscribe);
    bar.add(unsubscribe);
    // prepare user interface
    JTextArea edit = new JTextArea(8, 40);
    JScrollPane scroll = new JScrollPane(edit);
    BorderLayout bord = new BorderLayout();
    setLayout(bord);
    add("North", bar);
    add("Center", scroll);
    pack();
    setVisible(true);
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}

Related Tutorials