JDK-4188874 : BoxLayout not aligning mixed components correctly
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1998-11-10
  • Updated: 2001-11-09
  • Resolved: 2001-11-09
Related Reports
Duplicate :  
Relates :  
Description

Name: rk38400			Date: 11/10/98


When trying to align (vertically) a set of
JLabels and JSliders, the JLabels are being
aligned at the wrong position.

If the JSliders are *not* added to the layout,
the JLabels align on the left edge.  If JButtons
are used instead of JSliders, the JLabels also
align correctly.

The first time a JSlider is added, the JLabels are 
no longer aligned at the left of the layout.  
Instead they are aligned 1/3 of the way over.

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class MySlider2
		extends 	JFrame
		implements  ChangeListener
 {
     private	JSlider		scrollerR;
     private	JSlider		scrollerG;
     private	JSlider		scrollerB;
     private JLabel		labelR;
     private JLabel		labelG;
     private JLabel		labelB;
     private JPanel		labelColor;
     private JPanel		topPanel;


     public MySlider2() {
	 setTitle( "Slider Application" );
	 setBackground( Color.gray );
	 topPanel = new JPanel();
  	 BoxLayout boxLayout = new BoxLayout(topPanel, BoxLayout.Y_AXIS);
	 topPanel.setLayout( boxLayout);
	 topPanel.setBackground(Color.green);
	 getContentPane().setLayout(new FlowLayout());
	 getContentPane().add( topPanel );
	 
	 // Create the labels
	 labelR = new JLabel( "Red", SwingConstants.LEFT );
	 labelG = new JLabel( "Green", SwingConstants.RIGHT );
	 labelB = new JLabel( "Blue" );
	 labelColor = new JPanel();
	 labelColor.setBackground( new Color( 0, 0, 0 ) );
	 
	 // Create the scrollbars
	 scrollerR = new JSlider( SwingConstants.HORIZONTAL,
				  0, 255, 0 );
	 scrollerR.setMajorTickSpacing( 40 );
	 scrollerR.setMinorTickSpacing( 10 );
	 scrollerR.setPaintTicks( true );
	 scrollerR.setPaintLabels( true );
	 scrollerR.addChangeListener( this );
	 
	 scrollerG = new JSlider( SwingConstants.HORIZONTAL,
				  0, 255, 0 );
	 scrollerG.setMajorTickSpacing( 40 );
	 scrollerG.setMinorTickSpacing( 10 );
	 scrollerG.setPaintTicks( true );
	 scrollerG.setPaintLabels( true );
	 scrollerG.addChangeListener( this );
	 
	 scrollerB = new JSlider( SwingConstants.HORIZONTAL, 0, 255, 0 );
	 scrollerB.setMajorTickSpacing( 40 );
	 scrollerB.setMinorTickSpacing( 10 );
	 scrollerB.setPaintTicks( true );
	 scrollerB.setPaintLabels( true );
	 scrollerB.addChangeListener( this );
	 topPanel.add( labelR );
  	 topPanel.add( scrollerR );
	 topPanel.add( labelB );
  	 topPanel.add( scrollerB );
	 topPanel.add( labelG );
  	 topPanel.add( scrollerG );
 	 topPanel.add( labelColor );
	 topPanel.setPreferredSize(new Dimension (300, 300));
	 pack();
     }


	// Watch for scroll bar adjustments
	public void stateChanged( ChangeEvent event )
	{
		// The event came from our scrollers handle it.
		if( event.getSource() == scrollerR ||
			event.getSource() == scrollerG ||
			event.getSource() == scrollerB )
		{
			// Get the current color settings
			int iRed = scrollerR.getValue();
			int iGreen = scrollerG.getValue();
			int iBlue = scrollerB.getValue();

			// Update the color chip
			labelColor.setBackground(
						new Color( iRed, iGreen, iBlue ) );
			labelColor.repaint();
		}
	}

	public static void main( String args[] )
	{
		// Create an instance of the test application
		MySlider2 mainFrame	= new MySlider2();
		mainFrame.setVisible( true );
	}
}
(Review ID: 42172)
======================================================================

Comments
EVALUATION Name: keR10081 Date: 11/02/2001 Sliders and panels, unlike buttons and labels, have X alignment set to 0.5, and that's why we see this behaviour. To achieve necessary appearance, call setAlignmentX(0) on all sliders and labelColor. For more information, see http://java.sun.com/j2se/1.4/docs/api/javax/swing/BoxLayout.html and http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html This is probably not a bug. ###@###.### ======================================================================
11-06-2004

WORK AROUND Name: rk38400 Date: 11/10/98 Unknown workaround ======================================================================
11-06-2004