A JToolBar mainly comprises of JButtons components which can also be separated by a JSeperators.A JToolBar can be inserted in any four...
A JToolBar mainly comprises of JButtons components which can also be separated by a JSeperators.A JToolBar can be inserted in any four corners of a JPanel or can also be made to float.
Generally, JToolBar uses BoxLayout which assumes objects height or width and arranges them vertically or horizontally.
To align a JToolBar call class setAlignmentX() or setAligmentY().
Generally, JToolBar uses BoxLayout which assumes objects height or width and arranges them vertically or horizontally.
To align a JToolBar call class setAlignmentX() or setAligmentY().
JToolBar Constructors.
- JToolBar()-Creates a new JToolBar with default orientation Horizontal.
- JToolBar(int orientation)-Creates a new JToolBar with specified Orientation.
- JToolBar(String name)-creates a JToolBar with the specified name.
- JToolBar(int orientation,String name)-creates aJToolBar with orientation and name.
JToolBar Code Example
import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; public class JToolBarExample extends JFrame{ public JToolBarExample() { super("JToolBar Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //code to create a JToolBar JToolBar toolbar = new JToolBar(); toolbar.setRollover(true); JButton button = new JButton("New File"); toolbar.add(button); toolbar.addSeparator(); toolbar.add(new JButton("Save File")); toolbar.add(new JComboBox(new String[]{"Sales Report","Items Report","Customers Report"})); Container pane = this.getContentPane(); pane.add(toolbar, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); JScrollPane Jpane = new JScrollPane(textArea); pane.add(Jpane, BorderLayout.CENTER); setSize(350, 150); this.setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { // TODO code application logic here new JToolBarExample(); } }