Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Tabbed Pane in a Frame using Java Swing API ?.

A Simple program to demonstrate how to use Tabbed Pane in a Frame using Java Swing API ?.

package com.iplt20.swing.example;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabbedPaneDemo extends JFrame {
  private String[] ipl = {
  "Deccan Chargers", "Kolkata Night Riders", 
  "Mumbai Indians", "Chennai Super Kings", 
  "Rajasthan Royals", "Royal Challengers Bangalore",
  "Kings XI Punjab", "Pune Warriors India",
  "Delhi Daredevils"
  };

  private JTabbedPane tab = new JTabbedPane();
  private JTextField textfield = new JTextField(20);
  
  public TabbedPaneDemo() {
    int i = 0;
    for(String teams : ipl){
      tab.addTab(ipl[i],
      new JTextField("Who will win IPL this year?."));
      i++;
    }

    tab.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
 textfield.setText("You selected: " +
          tab.getTitleAt(tab.getSelectedIndex()));
      }
    });
    add(BorderLayout.SOUTH, textfield);
    add(tab);
  }
  
  public static void main(String[] args) {
  setFrame(new TabbedPaneDemo(), 450, 175);
 }

  public static void 
  setFrame(final JFrame frame, final int width, final int height) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
 frame.setTitle(frame.getClass().getSimpleName());
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(width, height);
 frame.setVisible(true);
      }
    });
  }
} 


Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot