JDK-4386981 : Box class should implement setAlignmentX/Y()
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0,1.3.0_02
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 2000-11-08
  • Updated: 2005-07-14
  • Resolved: 2005-07-14
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 11/07/2000


java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


The Box class in Swing does not allow one to set its X or Y alignment.
Granted, you can extend the Box class with less than 10 lines of code to
fix this, but still.... the Box class as it stands is almost useless
for complex layouts in which one would like to nest Box classes. These
few lines of code should really be part of the library.

It's really a "request for enhancement", not a bug.

Is there some other place I should submit it?

I'm also not the only one who's pointed out the problem: 

From:

http://forum.java.sun.com/read/16799962/qAwguIMeSKbsAAA8G#LR


                       In the tutorial "How to Use BoxLayout", there is a section about "Fixing Alignment Problems" where the following advice is given: "In general, all
                        the components controlled by a top-to-bottom BoxLayout object should have the same X alignment. [...] You can set a JComponent's X
                        alignment by invoking its setAlignmentX method." 

                        The problem is that javax.swing.Box does not define the setAlignmentX and setAlignmentY methods. This means that this solution does not work
                        when we use a (horizontal) Box within a (vertical) Box. To solve the problem, I created a derived class that adds these methods (see below). It
                        works so well that I am using them all the time. 

                        My question is: why the AlignmentX and AlignmentY properties are not included in the javax.swing.Box class? Is there another way to have a
                        number of horizontal Boxes left-aligned or right-aligned (not centered) within a vertical Box? 

                        
                        import javax.swing.*; 
                        public class AlignableBox extends Box { 
                        public AlignableBox(int axis) { 
                        super(axis); 
                        } 
                        public static Box createHorizontalBox() { 
                        return new AlignableBox(BoxLayout.X_AXIS); 
                        } 
                        public static Box createVerticalBox() { 
                        return new AlignableBox(BoxLayout.Y_AXIS); 
                        } 

                        protected float alignmentX; 
                        public float getAlignmentX() { 
                        return alignmentX; 
                        } 
                        public void setAlignmentX(float alignmentX) { 
                        this.alignmentX = alignmentX; 
                        } 

                        protected float alignmentY; 
                        public float getAlignmentY() { 
                        return alignmentY; 
                        } 
                        public void setAlignmentY(float alignmentY) { 
                        this.alignmentY = alignmentY; 
                        } 
                        } 

                        



... though nobody has reported it yet as a bug/request for
enhancement yet, as far as I can tell.
(Review ID: 111576) 
======================================================================

Name: yyT116575			Date: 08/02/2001


java version "1.3.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02
Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)

Say you are using a Box with a BoxLayout.Y_AXIS configuration.

Further, say you are adding JComponents (such as JLabels) with their own defined setAlignmentX configurations.

The problem/(bug?) is with the Box's interpretation of setAlignmentX
configurations for the JComponents.  The first JComponent is granted it's
alignment, relative to the Box.  However, subsequent JComponents are granted their alignments relative to the *first* JComponent, instead of the Box.  (In cases where all JComponents are using the same configuration, there is no noticeable effect, but the effects are *very* noticeable when adding JComponents with differing setAlignmentX configurations.)

This problem is compounded when adding a Component, such as one generated with the Box.createHorizontalStrut method.  It apparently gets alignement precedence over the previously discussed JComponents in the same Box, forcing all of the JComponents to be aligned relative to it's alignment.  As far as I can determine, the strut acts as though it has a setAlignmentX value of 0.5. (Hence, no noticeable effect if all JComponents are using a setAlignmentX value of 0.5, but very noticeable effects otherwise.)

In the below source code, JLabels are generated (for four instances of Box inside of a JFrame using a FlowLayout) with different sizes and different opaque backgrounds to demonstrate the alignment problem.  They have no direct effect on causing or alleviating the problem; they are merely done to enhance the visibility of it.


// Source code follows ...

import java.awt.Color;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BoxLayoutTest {
    
  private JLabel generateLabel(String title, Color color, float alignmentX) {
    JLabel label = new JLabel(title);
    label.setBackground(color);
    label.setOpaque(true);
    label.setAlignmentX(alignmentX);
    return label;
  }
    
  public static void main(String[] args) {
    BoxLayoutTest test = new BoxLayoutTest();
    Box box1 = new Box(BoxLayout.Y_AXIS);
    box1.add(test.generateLabel("1", Color.red, 0f));
    box1.add(test.generateLabel("Label 2", Color.orange, 0.5f));
    box1.add(test.generateLabel("Label 3 - Alignment test", Color.yellow,1f));
    Box box2 = new Box(BoxLayout.Y_AXIS);
    box2.add(test.generateLabel("4", Color.green, 1f));
    box2.add(test.generateLabel("Label 5", Color.blue, 1f));
    box2.add(test.generateLabel("Label 6 - Alignment test", Color.magenta,1f));
    Box box3 = new Box(BoxLayout.Y_AXIS);
    box3.add(test.generateLabel("7", Color.cyan, 0f));
    box3.add(test.generateLabel("Label 8", Color.pink, 0f));
    box3.add(test.generateLabel("Label 9 - Alignment test", Color.white,0f));
    Box box4 = new Box(BoxLayout.Y_AXIS);
    box4.add(test.generateLabel("10", Color.black, 0.5f));
    box4.add(test.generateLabel("Label 11", Color.lightGray, 0.5f));
    box4.add(test.generateLabel("Label 12 - Alignment test",Color.darkGray, 0.5f));
        
    JFrame frame = new JFrame("setAlignmentX Test");
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(box1);
    frame.getContentPane().add(box2);
    frame.getContentPane().add(box3);
    frame.getContentPane().add(box4);
    frame.addWindowListener( new WindowAdapter(){
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.setVisible(true);
  }
    
}
(Review ID: 126500)
======================================================================

Comments
EVALUATION worth investigating. ###@###.### 2001-11-19 As part of 4304100 Box was made to extend JComponent. As a result, Box now inherits set/getAlignmentX/Y. I'm closing this out as a duplicate. ###@###.### 2005-07-14 14:31:23 GMT
14-07-2005