Adjusting size of JPanels in JSplitPane
Asked Answered
P

2

7

Trying to create a JSplitPane with two JPanels. With the following code:

 JTable table = new JTable(qualifierModel);
  table.setDefaultEditor(String.class, new QualifierCellEditor());


  JPanel qualiferPanel = new JPanel();
  JScrollPane jp = new JScrollPane(table);
  qualiferPanel.add(new JLabel(Translator.getText("Select one qualifier for each section # from the table.")));
  qualiferPanel.add(jp);
  qualiferPanel.setVisible(true);

  JToolBar btnBar = new JToolBar();
  btnBar.setFloatable(false);
  btnBar.add(Box.createHorizontalGlue());
  btnBar.add(addItemButton);
  btnBar.add(removeItemButton);
  setLayout(new BorderLayout());


  profilePanel.add(new JScrollPane(profileTable), BorderLayout.NORTH);
  profilePanel.add(btnBar, BorderLayout.SOUTH);

  JSplitPane spane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  spane.setTopComponent(profilePanel);
  spane.setBottomComponent(qualiferPanel);

  setLayout(new BorderLayout());
  add(spane,BorderLayout.CENTER);

enter image description here

I have added two add buttons in the first JPanel which is not visible. How should I adjust the size of the first JPanel.

enter image description here

Plummy answered 2/8, 2013 at 16:13 Comment(0)
K
11

You can adjust the position of the split pane divider by calling:

spane.setDividerLocation(0.5);

Or you can rearrange the way it splits the space between the two parts with:

spane.setResizeWeight(1.0); // equal weights to top and bottom

You might want to figure out how to remove the blank space from the profile panel and that would help, too.

But I think the real problem might be the size of the frame itself. You didn't show that part of the code.

You might try enlarging the starting size of the JFrame and see how the layouts rearrange things.

(On a side note: I usually fix up the buttons to be on the right side of their own little panel by using a flow layout with right justification.)

Kephart answered 2/8, 2013 at 16:24 Comment(0)
U
7

Another way is to set the JPanel's dimensions with setPreferredSizes(new Dimension(width, height)) and invoke resetToPreferredSizes() on the JSplitPane.

From the JSplitPane's javadoc

To resize the Components to their preferred sizes invoke resetToPreferredSizes.

When the user is resizing the Components the minimum size of the Components is used to determine the maximum/minimum position the Components can be set to. If the minimum size of the two components is greater than the size of the split pane the divider will not allow you to resize it. To alter the minimum size of a JComponent, see JComponent.setMinimumSize(java.awt.Dimension).

When the user resizes the split pane the new space is distributed between the two components based on the resizeWeight property. A value of 0, the default, indicates the right/bottom component gets all the space, where as a value of 1 indicates the left/top component gets all the space.

Maybe you can use JSplitPane#setDividerLocation(aDouble) too.

Ultrared answered 2/8, 2013 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.