JTabbedPane Component in Java

This is a tutorial in which we will going to create a program that has the JTabbedPane Component using Java. The JTabbedPane is used to let the user switch on other menus or components by clicking on a tab with a given title or icon. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jTabbedPaneComponent.java. 2. Import the javax.swing package to access the JFrame,JButton, JLabel,JTextField, and the JTabbedPane class.
  1. import javax.swing.*; // this is used to access the JFrame,JButton, JLabel,JTextField, and the JTabbedPane class
3. Initialize your variable in your Main, variable frame for creating JFrame and variable tabbedPane for the JTabbedPane component.
  1. JFrame frame = new JFrame("JTabbedPane Component");
  2. JTabbedPane tabbedPane = new JTabbedPane();
4. To create a tab in the TabbedPane we will use the addTabmethod. The first tab that we will create will be equal to 0 and the last tab will be equal to the tab index minus 1. Have this code below:
  1. tabbedPane.addTab("Transaction", new JButton("This is the first tab."));
  2. tabbedPane.addTab("Maintenance", new JLabel("This is the second tab."));
  3. tabbedPane.addTab("Inquiry", new JTextField("This is the third tab."));
We have use the addTab Method to create tabbed pane in the frame. This addTab method has this parameters of String and the Component. We have created three tabbed pane above, the Transaction, Maintenance, and Inquiry as the string text and the second parameter has the parameters of JButton, JLabel, and JTextField component. 5. Now, add the tabbed pane to the frame using the default BorderLayout of the North orientation of the getContentPane. Then set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane().add(tabbedPane, "North");
  2. frame.setSize(400, 150);
  3. frame.setVisible(true);
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // this is used to access the JFrame,JButton, JLabel,JTextField, and the JTabbedPane class
  2.  
  3. public class jTabbedPaneComponent {
  4.  
  5.  
  6. public static void main(String args[]) {
  7. JFrame frame = new JFrame("JTabbedPane Component");
  8. JTabbedPane tabbedPane = new JTabbedPane();
  9.  
  10. tabbedPane.addTab("Transaction", new JButton("This is the first tab."));
  11. tabbedPane.addTab("Maintenance", new JLabel("This is the second tab."));
  12. tabbedPane.addTab("Inquiry", new JTextField("This is the third tab."));
  13.  
  14.  
  15. frame.getContentPane().add(tabbedPane, "North");
  16. frame.setSize(400, 150);
  17. frame.setVisible(true);
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. }
  20. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number of Facebook Page 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

Add new comment