3

Why does the following code, placed alone in the constructor of a JPanel subclass, produce a proportional empty space to the left of the split_pane but above mQueryField?

mMessageArea = new JTextArea ();
JScrollPane message_pane = new JScrollPane (mMessageArea);

mTableView = new JTable ();
mTableView.setFillsViewportHeight (true);
JScrollPane table_pane = new JScrollPane (mTableView);

JSplitPane split_pane = new JSplitPane (JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);

mQueryField = new JTextField ();
mQueryField.setMaximumSize (new Dimension (Short.MAX_VALUE, 20));

setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (split_pane);
add (mQueryField);

EDIT - SSCCE:

import java.awt.Dimension;    
import javax.swing.*;

public class SSCCE extends JPanel
{

    private JTextArea mMessageArea;
    private JTable mTableView;
    private JTextField mQueryField;

    public SSCCE ()
    {
        mMessageArea = new JTextArea ();
        JScrollPane message_pane = new JScrollPane (mMessageArea);

        mTableView = new JTable ();
        mTableView.setFillsViewportHeight (true);
        JScrollPane table_pane = new JScrollPane (mTableView);

        JSplitPane split_pane = new JSplitPane (JSplitPane.VERTICAL_SPLIT,
                message_pane, table_pane);

        mQueryField = new JTextField ();
        mQueryField.setMaximumSize (new Dimension (Short.MAX_VALUE, 20));

        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
        add (split_pane);
        add (mQueryField);
    }

    public static void main (String[] args)
    {
        JFrame frame = new JFrame ();
        frame.add (new SSCCE ());
        frame.setVisible (true);
        frame.pack ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }

}
Anton
  • 1,422
  • 1
  • 11
  • 15

2 Answers2

2

Perhaps it's due to the JSplitPane's maximum size not being big enough. You can either set this or nest it in a JPanel that uses BorderLayout:

  JSplitPane split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);
  JPanel splitHolder = new JPanel(new BorderLayout());
  splitHolder.add(split_pane);

  mQueryField = new JTextField();
  mQueryField.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));

  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  add(splitHolder);
  add(mQueryField);

1+ for the SSCCE by the way. It makes working with your problem much easier.

Edit:
I was wrong, the problem is simply one of alignment as JTextFields default to an x alignment of Component.CENTER_ALIGNMENT. The solution is set set the X alignment to the left:

  JSplitPane split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
        message_pane, table_pane);
  System.out.println("split_pane.getAlignmentX() before:"
        + split_pane.getAlignmentX());
  split_pane.setAlignmentX(LEFT_ALIGNMENT); // NOT REALLY NEEDED
  System.out.println("split_pane.getAlignmentX() after:"
        + split_pane.getAlignmentX());

  mQueryField = new JTextField();
  mQueryField.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
  System.out.println("mQueryField.getAlignmentX() before: "
        + mQueryField.getAlignmentX());
  mQueryField.setAlignmentX(LEFT_ALIGNMENT);
  System.out.println("mQueryField.getAlignmentX() after: "
        + mQueryField.getAlignmentX());
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Interestingly, making the `splitHolder`'s layout a horizontal `BoxLayout` solves the problem while making it vertical does nothing. I'm still wondering *why* this happens though. – Anton Mar 10 '12 at 14:45
  • @AntonMårtensson: please see Edit. The problem is simply one of x alignment. I guess JPanels default to left alignment x while JTextFields don't. – Hovercraft Full Of Eels Mar 10 '12 at 15:48
  • 1
    Indeed, `JTextField`s default to center alignment. Thanks :) – Anton Mar 10 '12 at 15:56
2

JSplitPane knows own size after method pack(), this issue could be same by using BorderLayout

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319