Sort of a follow-up to JTabbedPane why is there extra padding only when I have multiple tabs? (code and picture)
I have a lot of tabs, so I would prefer to have multiple rows of tabs to avoid excessive scrolling. This makes the SCROLL_TAB_LAYOUT undesirable, as it creates a single row of tabs (shown in left image).
Using WRAP_TAB_LAYOUT lets me have multiple rows of tabs. However, I get extra space at the bottom of each tab panel (show by the large red area in right image).
Is there a way to get multiple rows of tabs, without the ugly extra spaces of WRAP_TAB_LAYOUT?
Here is some code to create the JTabbedPane. With a large number of tabs (20+), neither layout is satisfactory.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class DialogTest {
public static void main(String[] args) {
new DialogTest();
}
public DialogTest() {
JDialog dialog = new MyDialog();
dialog.pack();
dialog.setVisible(true);
}
class MyDialog extends JDialog {
public MyDialog() {
super(null, ModalityType.APPLICATION_MODAL);
final JTabbedPane tabs = new JTabbedPane();
tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); // change to WRAP_TAB_LAYOUT for other layout
final int numTabs = Integer.parseInt(JOptionPane.showInputDialog("Number of tabs:"));
setPreferredSize(new Dimension(400, 200));
for (int i = 1; i <= numTabs; i++) {
tabs.addTab("Tab"+i, new MyPanel(i));
}
setLayout(new BorderLayout());
add(tabs, BorderLayout.NORTH);
}
}
class MyPanel extends JPanel {
public MyPanel(int text) {
final JLabel label = new JLabel("THIS IS A PANEL" + text);
label.setFont(label.getFont().deriveFont(18f));
label.setBackground(Color.cyan);
label.setOpaque(true);
add(label);
setBackground(Color.red);
}
}
}