Adjusting size of JPanels in JSplitPane

Arun picture Arun · Aug 2, 2013 · Viewed 13k times · Source

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

Answer

Lee Meador picture Lee Meador · Aug 2, 2013

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