Anton
Anton

Reputation: 1430

Why does this code produce proportional empty space?

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);
    }

}

Upvotes: 3

Views: 243

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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());

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

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

Upvotes: 2

Related Questions