[Java swing] JTabbedPane in Java

You have to use the notepad or you are using eclipse, NetBean to java code and you can see we have many different tab. This article we will learn JTabbedPane, a component that allows you to create multiple tabs so.

JTabedbPane in Java

Create simple JTabbedPane

Consider the following code to do simple demo shown above, in your code has relatively clear explanation. Below the code I will say a little more about JTabbedPane.

package nguyenvanquan7826.JTabbedPane;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

/**
 * ----------------- @author nguyenvanquan7826 -----------------
 * ---------------nguyenvanquan7826.wordpress.com --------------
 */
public class DemoJTabbedPane extends JFrame {

	public DemoJTabbedPane() {
		createGUI();
		setDisplay();
	}

	/**
	 * set display for JFrame
	 */
	private void setDisplay() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * add JTabbedPane into JFrame
	 */
	private void createGUI() {
		add(createTabbedPane());
	}

	/**
	 * create a JTabbedPane contain three tab
	 */
	private JTabbedPane createTabbedPane() {
		JTabbedPane tabbedPane = new JTabbedPane();

		/* create three JPanel, which is content of tabs */
		JPanel panel1 = createPane1();
		JPanel panel2 = createJPanel("content of panel 2");
		JPanel panel3 = createJPanel("content of panel 3");

		/* add three tab with three JPanel */
		tabbedPane.addTab("Tab 1", null, panel1, "click to show panel 1");
		tabbedPane.addTab("Tab 2", null, panel2, "click to show panel 2");
		tabbedPane.addTab("Tab 3", null, panel3, "click to show panel 3");

		return tabbedPane;
	}

	/**
	 * create JPanel 1 contain a JTextArea
	 */
	private JPanel createPane1() {
		JPanel panel = new JPanel();
		panel.add(new JScrollPane(createTextArea(10, 40)));
		return panel;
	}

	/**
	 * create a JPanel contain a JLabel
	 */
	private JPanel createJPanel(String text) {
		JPanel panel = new JPanel(new GridLayout(1, 1));
		JLabel lb = new JLabel(text);
		lb.setHorizontalAlignment(JLabel.CENTER);
		panel.add(lb);
		return panel;
	}

	/**
	 * create a JTextArea with rows and columns, two method setWrapStyleWord and
	 * setLineWrap make text can down line when text too long
	 */
	private JTextArea createTextArea(int row, int col) {
		JTextArea ta = new JTextArea(row, col);
		ta.setWrapStyleWord(true);
		ta.setLineWrap(true);
		ta.setForeground(Color.BLUE);
		return ta;
	}

	public static void main(String[] args) {
		new DemoJTabbedPane();
	}
}

In most of the code in the method of JPanel, JLabel, JTextArea,… alone are mentioned in the previous post. Point of note here is the initialization method JTabbedPane. We have some initialization method after:

JTabbedPane(): Create a blank for default JTabbedPane tab at the top
JTabbedPane(int tabPlacement): Create a blank where JTabbedPane tab above (JTabbedPane.TOP), below(JTabbedPane.BOTTOM), left (JTabbedPane.LEFT), right (JTabbedPane.RIGHT).
JTabbedPane(int tabPlacement, int tabLayoutPolicy):Create JTabbenPane drums and regulations placement tab, the layout tab: the roll tab (SCROLL_TAB_LAYOUT) or imbricate (WRAP_TAB_LAYOUT) While size is not enough.

After creating JTabbedPane, we performed additional components in order to create the tab. On each tab containing 1 JPanel with certain content. Method addTab with parameters respectively tab name, icon tab, display components (on the JPanel) and finally the tool tip text (displayed when you hover on the tab).

Read more: class JTabbedPane, use JTabbedPane, Java swing
Read Version English here.