Reputation: 281
Can anyone suggest me how can I divide my JTabbedPane
in two equal horizontal sections? My Pane has three tabs in it. I want to divide the second tab pane (tab 2) into two equal halves?
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class Monitor{
public static void main(String[] args){
JFrame frame = new JFrame("WELCOME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("tab1", button);
button = new JButton("2");
tab.add("tab2", button);
button = new JButton("3");
tab.add("tab3", button);
frame.setSize(400,400);
frame.setVisible(true);
}
}
Upvotes: 1
Views: 1582
Reputation: 168835
Use a single row GridLayout
for the JPanel
that is placed in that tab. With two components in it, they will each have half the space. E.G.
import javax.swing.*;
import java.awt.*;
public class Monitor {
public static void main(String[] args){
Runnable r = new Runnable() {
public void run() {
JFrame frame = new JFrame("WELCOME");
// A better close operation..
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("tab1", button);
// this GridLayout will create a single row of components,
// with equal space for each component
JPanel tab2Panel = new JPanel(new GridLayout(1,0));
button = new JButton("2");
tab2Panel.add(button);
tab2Panel.add(new JButton("long name to stretch frame"));
// add the panel containing two buttons to the tab
tab.add("tab2", tab2Panel);
button = new JButton("3");
tab.add("tab3", button);
// a better sizing method..
//frame.setSize(400,400);
frame.pack();
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 5