1

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).

Panel on left has only 1 row, panel on right has extra padding

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);
        }   
    }
}
Guillaume F.
  • 1,010
  • 7
  • 21

1 Answers1

1

By setting a size to the JDialog you define an area that need to be filled by the components added to it.
It seems that you need the area to be defined by the content of the JDialog, so the first think is to remove setPreferredSize(new Dimension(400, 200)); and let the layout manager define the appropriate size.

Next you need to set the content (MyPanel) to the desired size and layout, by using its layout manager. In the following MRE GridBagLayout is used for this purpose:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
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);
            tabs.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

            for (int i = 1; i <= 12; i++) {  //12 as an arbitrary test value
                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);

            setBackground(Color.RED);
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{400, 0}; //affects parent width 
            gridBagLayout.rowHeights = new int[]{100, 0};   //affects parent height
            gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE};
            gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
            setLayout(gridBagLayout);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(label, gbc);
        }
    }
}

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65