Why is this code creating proportional white space?
Why JPanel
does the following code, placed separately in the subclass constructor , create proportional white space to the left of 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);
}
}
+3
source to share
2 answers
Perhaps this is because the maximum size of the JSplitPane is not large enough. You can 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 SSCCE. This makes it much easier to work with your problem .
Edit:
I was wrong, the problem is that the x alignment of the Component.CENTER_ALIGNMENT is the default for JTextFields. The solution is set to 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());
+2
source to share