By default, the tabs in a JTabbedPane
component is placed on the top. But you can place the tabs on every side of the JTabbedPane
component, for example it can be on the top, on the right, on the left or on the bottom of the JTabbedPane
.
To change the tab placement position you need to set the tab placement when creating an instance of JTabbedPane
. The tab placement can be set using the following constant values: JTabbedPane.TOP
, JTabbedPane.RIGHT
, JTabbedPane.LEFT
and JTabbedPane.BOTTOM
.
Let’s see the following code snippet to demonstrate it.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class TabbedPaneTabPlacement extends JPanel {
public TabbedPaneTabPlacement() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new TabbedPaneTabPlacement();
panel.setOpaque(true);
JFrame frame = new JFrame("Tabbed Pane Tab Placement Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TabbedPaneTabPlacement::showFrame);
}
private void initializeUI() {
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 200));
// Creates a JTabbedPane with tabs at the bottom.
JTabbedPane pane = new JTabbedPane(JTabbedPane.BOTTOM);
pane.addTab("Tab 1", createPanel("Panel 1"));
pane.addTab("Tab 1", createPanel("Panel 2"));
pane.addTab("Tab 3", createPanel("Panel 3"));
this.add(pane, BorderLayout.CENTER);
}
private JPanel createPanel(String title) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JLabel(title), BorderLayout.NORTH);
return panel;
}
}
And here is the result of the code snippet above.
Latest posts by Wayan (see all)
- How do I create a servlet filter to make secure cookies? - April 18, 2024
- How do I configure secure cookies using web.xml? - April 18, 2024
- How do I create a table with multiple header in iText 8? - March 16, 2024