JToolBar Component in Java

This is a tutorial in which we will going to create a program that will have a JToolBar in Java. A JToolBar provides a bar on the top of the program where it contains a group of components such as buttons, icons, labels, textfields, etc., that provide easy access to functionality of the other components. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jToolBarComponent.java. 2. Import the javax.swing.* package to access the JButton, JFrame, JScrollPane, JTextArea, JList, JLabel, and JToolBar class.
  1. import javax.swing.*; // used to access the JButton, JFrame, JScrollPane, JTextArea, JList, JLabel, and JToolBar class
3. Initialize your variable in your Main, variable frame for JFrame, variable textArea for JTextArea, variable pane for JScrollPane, and variable toolbar for JToolBar component.
  1. JFrame frame = new JFrame("JToolBar Component");
  2. JTextArea textArea = new JTextArea();
  3. JScrollPane pane = new JScrollPane(textArea);
  4. JButton button = new JButton("Button");
  5. JToolBar toolbar = new JToolBar();
4. To add components on the toolbar, we will used the add method.
  1. toolbar.add(button);
To separate another components on the toolbar so that i cannot be connected to other components, we will use the addSeparator method.
  1. toolbar.addSeparator();
We can also instantiate a component into the add method of the toolbar. We will instantiate a label and a list inside the toolbar. Have this code below:
  1. toolbar.add(new JLabel("This is a label"));
  2. toolbar.add(new JList(new String[]{"Blue","Red","Yellow"}));
5. Now, add the toolbar and panel variable to the frame using the getContentPane().add method with the default BorderLayout of North and Center position, respectively. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane().add(toolbar, "North");
  2. frame.getContentPane().add(pane, "Center");
  3. frame.setSize(350, 150);
  4. frame.setVisible(true);
  5. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access the JButton, JFrame, JScrollPane, JTextArea, JList, JLabel, and JToolBar class
  2.  
  3.  
  4. public class jToolBarComponent{
  5. public static void main(String args[]) {
  6. JFrame frame = new JFrame("JToolBar Component");
  7. JTextArea textArea = new JTextArea();
  8. JScrollPane pane = new JScrollPane(textArea);
  9. JButton button = new JButton("Button");
  10. JToolBar toolbar = new JToolBar();
  11.  
  12. toolbar.add(button);
  13. toolbar.addSeparator();
  14.  
  15. toolbar.add(new JLabel("This is a label"));
  16.  
  17. toolbar.addSeparator();
  18.  
  19. toolbar.add(new JList(new String[]{"Blue","Red","Yellow"}));
  20.  
  21.  
  22. frame.getContentPane().add(toolbar, "North");
  23. frame.getContentPane().add(pane, "Center");
  24. frame.setSize(350, 150);
  25. frame.setVisible(true);
  26. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. }
  28. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Submitted byneslihan (not verified)on Thu, 11/27/2014 - 03:58

Can I download it now

Add new comment