TabbedPane1.java Source code

Java tutorial

Introduction

Here is the source code for TabbedPane1.java

Source

// : c14:TabbedPane1.java
// Demonstrates the Tabbed Pane.
// <applet code=TabbedPane1 width=350 height=200></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabbedPane1 extends JApplet {
    private String[] flavors = { "Chocolate", "Strawberry", "Vanilla Fudge Swirl", "Mint Chip",
            "Mocha Almond Fudge", "Rum Raisin", "Praline Cream", "Mud Pie" };

    private JTabbedPane tabs = new JTabbedPane();

    private JTextField txt = new JTextField(20);

    public void init() {
        for (int i = 0; i < flavors.length; i++)
            tabs.addTab(flavors[i], new JButton("Tabbed pane " + i));
        tabs.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                txt.setText("Tab selected: " + tabs.getSelectedIndex());
            }
        });
        Container cp = getContentPane();
        cp.add(BorderLayout.SOUTH, txt);
        cp.add(tabs);
    }

    public static void main(String[] args) {
        run(new TabbedPane1(), 350, 200);
    }

    public static void run(JApplet applet, int width, int height) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet);
        frame.setSize(width, height);
        applet.init();
        applet.start();
        frame.setVisible(true);
    }
} ///:~