
Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)
We used JPanel and JTabbed of Swing this time.
We will use the following source as a template.
Source
import javax.swing.JFrame;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
// installation process
}
}
Table of Contents
- JPanel
- Place three in FlowLayout
- Add JButton to JPanel
- JTabbedPane
- Add tabs
- setTabLayoutPolicy
JPanel
JPanel is a container that can be used for general purposes.
Place three in FlowLayout
We specified FlowLayout as the JFrame layout manager and placed three JPanels. We used the default constructor to create an instance and add it to the JFrame. We drew a boundary because in that state it was transparent and it was unclear where it was located. The border can be specified using the setBorder() method.
Source
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Test2 extends JFrame {
public static void main(String[] args) {
new Test2();
}
public Test2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JPanel panel_01 = new JPanel();
panel_01.setBorder(new LineBorder(Color.RED));
add(panel_01);
JPanel panel_02 = new JPanel();
panel_02.setBorder(new LineBorder(Color.BLUE));
add(panel_02);
JPanel panel_03 = new JPanel();
panel_03.setBorder(new LineBorder(Color.GREEN));
add(panel_03);
}
}

Since FlowLayout was specified, the components were placed in order from left to right. Since each JPanel was not configured with any content, nothing was displayed.
Add JButton to JPanel
JPanel is a child class of the Container class. Since the add() method is defined in the Container class, JPanel can call this method. The argument is a Component object.
In the following sample, some JButtons will be added.
Source
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Test3 extends JFrame {
public static void main(String[] args) {
new Test3();
}
public Test3() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JPanel panel_01 = new JPanel();
panel_01.setBorder(new LineBorder(Color.RED));
panel_01.add(new JButton("Button 1-1"));
panel_01.add(new JButton("Button 1-2"));
add(panel_01);
JPanel panel_02 = new JPanel();
panel_02.setBorder(new LineBorder(Color.BLUE));
panel_02.add(new JButton("Button 2-1"));
panel_02.add(new JButton("Button 2-2"));
panel_02.add(new JButton("Button 2-3"));
add(panel_02);
JPanel panel_03 = new JPanel();
panel_03.setBorder(new LineBorder(Color.GREEN));
panel_03.add(new JButton("Button 3-1"));
add(panel_03);
}
}

In the case of JPanel’s Layout Manager, the default value is FlowLayout. As a result of outputting panel1.getLayout() to the console, the following is displayed.
Source
System.out.println(panel_01.getLayout());
Result
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
Since the value of align is center, the component will be centered. If you add a JPanel with the JFrame layout manager at its default value, you can see how it is centered.
Source
private void init() {
JPanel panel1 = new JPanel();
panel1.setBorder(new LineBorder(Color.RED));
panel1.add(new JButton("Button 1-1"));
panel1.add(new JButton("Button 1-2"));
panel1.add(new JButton("Button 1-3"));
panel1.add(new JButton("Button 1-4"));
panel1.add(new JButton("Button 1-5"));
add(panel1);
}

The default value of a layout manager of JFrame is BorderLayout. We used the add method to place the JPanel in the JFrame. In this case, the JPanel is placed in the center. The height and width of a placed JPanel is the same as a JFrame. Therefore, JButton will be wrapped and placed on the next line.
To change this placement method, for example, set up a new FlowLayout as shown below.
Source
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Test5 extends JFrame {
public static void main(String[] args) {
new Test5();
}
public Test5() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
JPanel panel1 = new JPanel();
panel1.setBorder(new LineBorder(Color.RED));
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel1.add(new JButton("Button 1-1"));
panel1.add(new JButton("Button 1-2"));
panel1.add(new JButton("Button 1-3"));
panel1.add(new JButton("Button 1-4"));
panel1.add(new JButton("Button 1-5"));
add(panel1);
}
}

By setting FlowLayout.LEFT, the JButton will be left-justified. You can see that JButton is left-justified in the second line.
Sponsored Links
JTabbedPane
The JTabbedPane class has tabs that switch between components. There are several add methods, but the following method seems to be easy to use.
- Component add(String title, Component component)
The first argument above is the string to be displayed in the tab.It is considered basic to specify JPanel as the second argument.
Add tabs
In the following example, JLabel and JPanel are set to the contents of the tab.
Source
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Test6 extends JFrame {
public static void main(String[] args) {
new Test6();
}
public Test6() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
init();
setVisible(true);
}
private void init() {
JPanel panel_01 = new JPanel();
panel_01.add(new JLabel("My JLabel 01"));
JPanel panel_02 = new JPanel();
panel_02.add(new JButton("My JButton 01"));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("My Tab 01", panel_01);
tabbedPane.add("My Tab 02", panel_02);
add(tabbedPane);
}
}


In the second tab, the JPanel is set to the contents of the tab. A JButton is added to that JPanel.
setTabLayoutPolicy
What to do when tabs do not fit in the screen can be set with the setTabLayoutPolicy() method.
The following two arguments can be specified.
- JTabbedPane.WRAP_TAB_LAYOUT
- JTabbedPane.SCROLL_TAB_LAYOUT
In JTabbedPane, the private field tabLayoutPolicy is defined. The method to change the value is the setTabLayoutPolicy() method. According to the API specification, the default value of tabLayoutPolicy is JTabbedPane.WRAP_TAB_LAYOUT.
In the following example, 10 tabs are added and the tabLayoutPolicy is not changed.
Source
private void init() {
JTabbedPane tabbedPane = new JTabbedPane();
for (int i = 0; i < 10; i++) {
String s = "tab " + i;
tabbedPane.add(s, new JLabel("the content of " + s));
}
add(tabbedPane);
}

It was wrapped and displayed where it did not fit in the width of the JFrame.
The following example adds one line of SCROLL_TAB_LAYOUT setting to the above Source.
Source
private void init() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
for (int i = 0; i < 10; i++) {
String s = "tab " + i;
tabbedPane.add(s, new JLabel("the content of " + s));
}
add(tabbedPane);
}


The left and right arrow icons are now displayed. Tabs can now be scrolled.
That’s all. I hope this is helpful to you.