JDK-4374833 : BoxLayout behaves differently with JFrame, JPanel and JSplitPane
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2000-09-28
  • Updated: 2001-12-06
  • Resolved: 2001-12-06
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description

Name: yyT116575			Date: 09/28/2000


java version "1.2.2"
Classic VM (build JDK-1.2.2_005, native threads, symcjit)


My application has a vertical layout, and I need a split-pane to show up
beneath a header (Currently implemented as a JPanel.  Adding a JSplitPane
to the same component which has a JPanel added causes the JSplitPane to be
reduced in visible width, which is unacceptable for my application.  This
bug doesn't manifest itself if I use a JLabel or a JButton instead of a
JPanel, but it does with everything else I could think of substituting
including a Panel, Canvas, JComponent, Component, Button or Label.  I don't
personally have the option of using either a button or a label, because I
can't center them above my split-pane without wrapping them in another
container.

  To reproduce the problem compile and execute the code which follows.  This
is just a GUI problem, so there are no error messages or trace information.
However, the BoxLayout is not working as advertised in this instance.  The
workaround for me was to put a JSplitPane within a JSplitPane, obviating the
need for a BoxLayout.  Here's the Code:

import java.awt.*;	//Container, Dimension, Color, FlowLayout
import java.awt.event.*;
import javax.swing.*;   //JFrame, JPanel, JScrollPane, JSplitPane, BoxLayout

public class BugSplit extends JFrame
{
   public BugSplit()//Constructor
   {
	super("Bug Split Example");
	Container content = getContentPane();
	content.setLayout(new BoxLayout(content, 1));

   	Dimension hd = new Dimension(200, 40);   //The header size
   	Dimension pd = new Dimension(300, 300);  //The panels' size
   	Dimension sd = new Dimension(200, 200); //The split pane's size

	JPanel Header = new JPanel();
	Header.setPreferredSize(hd);
	Header.setBackground(Color.blue); //To differentiate it.

	//Make some panels to scroll
   	JPanel PRed = new JPanel(), PGreen = new JPanel();
	PRed.  setBackground(Color.red);   PRed.  setPreferredSize(pd);
	PGreen.setBackground(Color.green); PGreen.setPreferredSize(pd);

	//create JSplitPane which contains scrolling panels
   	JScrollPane Top = new JScrollPane(PRed), Bottom = new JScrollPane(PGreen);
   	JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, Top, Bottom);
	jsp.setPreferredSize(sd);

	//Now add the header, and the split pane.  (Order doesn't matter)
	//Splitpane is shown in diminished size.
	content.add(Header);
	content.add(jsp);
                           addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent e) {
	     System.exit(0);
	  }
	});
	pack();
	setVisible(true);
   }

   public static void main(String[] s)
   { new BugSplit();}
}
(Review ID: 109491) 
======================================================================

Comments
EVALUATION As far as I can tell this is happening because of the differences in alignment of SplitPane (0) and JPanel (.5). SizeRequirements, which is used by BoxLayout, has some suspicious code in it that is used to determine where the components will be placed. Specifically calculateAlignedPositions is the culprit. It looks like it is using alignment to calculate size, which, may not be right. ###@###.### 2001-12-03 Name: keR10081 Date: 12/06/2001 This is yet another bug which deals with BoxLayout and components with different X alignments. And like other such bugs, this is not a bug. For more information on correcting alignments, see http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html ###@###.### ======================================================================
11-06-2004