javacodex.com
Java Examples
Java Examples
memu home questions

JToolBar

JToolBar provides a component that is useful for displaying commonly used Actions or controls.

This is an example of an application that uses a JToolBar. The JToolBar has a JButton, a JLabel and a JTextField attached to it.

Source: (JToolBarExample.java)

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class JToolBarExample extends JFrame {
 
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JToolBar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    frame.setSize(300,250); 
    frame.setLayout(new BorderLayout());
 
    // Create a horizontal JToolBar
    JToolBar toolbar = new JToolBar();
 
    // the button
    JButton button = new JButton("B1");
    button.setMargin(new Insets(0, 0, 0, 0));
 
    // the label
    JLabel label = new JLabel("text:");
 
    // the text field
    JTextField field = new JTextField(3);
 
    // add them to the toolbar.
    toolbar.add(button);
    toolbar.addSeparator();
    toolbar.add(label);
    toolbar.add(field);
 
    JTextArea body = new JTextArea();
 
    frame.add(toolbar,BorderLayout.NORTH);
    frame.add(body,BorderLayout.CENTER);
 
    frame.setVisible(true);
  }
 
}
 

Output:

$ java JToolBarExample
Output



Contact: javacodex@yahoo.com