Question

This is a continuation of the question "Keeping preferred sizes of components in center of BorderLayout", which I don't know how to link from here. If someone wants to edit in a link and send me a message somehow saying how it's done, I'd appreciate it.

In that question, I asked and was answered how to keep my components at their preferred sizes, and as an adjunct how to put the panel that is in the center of a BorderLayout in the upper left corner of the center of that layout (instead of having it float out to the middle.

I simplified my real problem in order to fit it into a question, and now I'm having trouble generalizing it. The following code is similar, except it puts a tabbed pane in the center of the border layout.

I've cut some things out: in my app, the tabbed pane is in one half of a split pane, and I have a toolbar above (separate borderlayout panel), and navigation buttons below. I cut all that out of here because I can demo the problem without it, but it does mean I have a reason for using border layout, mostly because the panels in the center of all this are ones that I want to take advantage of any extra space the user has to give them.

In this code, we get the tabbed pane, but the two panels within it float to the middle of their extra space if the window gives it to them. In one version (see the alternate lines near the bottom of the code) we get scroll bars for them, and in the other we don't.

I would like the components within the tabbed panes to stay their preferred sizes. This code is doing that now by plopping each pane into a gridbaglayout panel, but that has other effects that I don't want.

I would like the contents of each tabbed pane to stay in the upper-left corner of the space if it is larger than the panel needs; I would like scroll bars to appear when there isn't enough space.

So are there other opinions on how I might get this done?

package example;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class BorderAndBox extends JFrame
{
    public static void main(String args[])
    {
        BorderAndBox bnb = new BorderAndBox();
        bnb.createUI();
        bnb.setLocationByPlatform(true);
        bnb.setVisible(true);
    }

    public void createUI()
    {
        JPanel borderPanel = new JPanel(new BorderLayout());

        JPanel innerPanelOne = new JPanel();
        innerPanelOne.setLayout(new BoxLayout(innerPanelOne, BoxLayout.LINE_AXIS));
        String[] firstChoices = { "first", "uno", "UN" };
        String[] secondChoices = { "second", "dos", "zwei" };
        String[] thirdChoices = { "third", "tres", "drei" };
        JComboBox firstCombo = new JComboBox(firstChoices);
        JComboBox secondCombo = new JComboBox(secondChoices);
        JComboBox thirdCombo = new JComboBox(thirdChoices);
        innerPanelOne.add(firstCombo);
        innerPanelOne.add(secondCombo);
        innerPanelOne.add(thirdCombo);
        JPanel innerPanelTwo = new JPanel();
        innerPanelTwo.setLayout(new BoxLayout(innerPanelTwo, BoxLayout.PAGE_AXIS));
        innerPanelTwo.add(new JLabel("additionalLabel"));

        JPanel panelA = new JPanel(new GridBagLayout());
        panelA.add(innerPanelOne);

        JPanel innerPanelThree = new JPanel();
        innerPanelThree.setLayout(new BoxLayout(innerPanelThree, BoxLayout.PAGE_AXIS));
        JLabel lblFirst = new JLabel("first in line");
        JLabel lblSecond = new JLabel("second to none");
        JLabel lblThird = new JLabel("third the bird");
        JLabel lblFourth = new JLabel("fourth");
        JLabel lblFifth = new JLabel("fifth");
        JLabel lblSixth = new JLabel("sixth");
        innerPanelThree.add(lblFirst);
        innerPanelThree.add(lblSecond);
        innerPanelThree.add(lblThird);
        innerPanelThree.add(lblFourth);
        innerPanelThree.add(lblFifth);
        innerPanelThree.add(lblSixth);
        JPanel panelB = new JPanel(new GridBagLayout());
        panelB.add(innerPanelThree);

        JScrollPane scrollPane1 = new JScrollPane(panelA);
        JScrollPane scrollPane2 = new JScrollPane(panelB);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("one", scrollPane1);
        tabbedPane.add("two", scrollPane2);


        JPanel westPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(tabbedPane, BorderLayout.NORTH);
        westPanel.add(northPanel, BorderLayout.WEST);
        // the following lines are mutually exclusive; 
        // use only the first, you have no scroll bars,
        // use only the second, you do have them
        borderPanel.add(westPanel, BorderLayout.CENTER);
//          borderPanel.add(tabbedPane, BorderLayout.CENTER);

        getContentPane().add(tabbedPane);
        pack();
    }



}
Was it helpful?

Solution

Maybe I'm missing what you're trying to do, but what you seem to be missing is codE that sets the GridBagConstraints for adding a component to your GridBagLayout-using container. For e.g.,

  GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
        GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
        new Insets(0, 0, 0, 0), 0, 0);
  panelA.add(innerPanelOne, gbc);

  //...

  panelB.add(innerPanelThree, gbc);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top