Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting size of JPanels in JSplitPane

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

like image 865
Arun Avatar asked Aug 02 '13 16:08

Arun


2 Answers

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

like image 183
Lee Meador Avatar answered Oct 17 '22 15:10

Lee Meador


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.

like image 39
NiziL Avatar answered Oct 17 '22 17:10

NiziL