Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class Main extends JFrame implements ActionListener {
    JTabbedPane tabbedPane;
    JButton focusButton;
    JPanel contentPane, tab1 = new JPanel(), tab2 = new JPanel();
    JTextField textField1 = new JTextField(10), textField2 = new JTextField(10), textField3 = new JTextField(10);

    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textField1.requestFocusInWindow();
        tab1.add(textField1);

        tab2.add(textField2);
        tab2.add(textField3);

        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        tabbedPane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT);
        tabbedPane.addTab("TAB 1", null, tab1, "I am TAB 1");
        tabbedPane.addTab("TAB 2", null, tab2, "I am TAB 2");

        focusButton = new JButton("CHANGE FOCUS");
        getRootPane().setDefaultButton(focusButton);
        focusButton.addActionListener(this);

        contentPane.add(tabbedPane, BorderLayout.CENTER);
        contentPane.add(focusButton, BorderLayout.PAGE_END);
        setContentPane(contentPane);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        if (tabbedPane.getSelectedIndex() == 0) {
            tabbedPane.setSelectedIndex(1);
            textField3.requestFocusInWindow();
        } else if (tabbedPane.getSelectedIndex() == 1) {
            tabbedPane.setSelectedIndex(0);
            textField1.requestFocusInWindow();
        }
    }

    public static void main(String... args) {
        new Main();
    }
}