-
Bug
-
Resolution: Fixed
-
P4
-
1.2.2
-
beta
-
generic
-
generic
Name: krT82822 Date: 09/26/99
The following code shows that the JSlider does not properly align itself within a container that is larger than its preferred size. Specifically, a vertical slider does not center align horizontally; instead it left-justifies itself. The setAlignmentX/Y methods are called to ensure a 0.5f alignment.
The example code uses the BorderLayout to "Center" the slider. The code also uses a GridBagLayout with two Box.glue components on either side to squeeze the slider into position, to prove that the problem is alignment, not resizing.
Windows 98
java version "1.2.2"
HotSpot VM (1.0fcs, mixed mode, build E)
<code>
import javax.swing.*;
import java.awt.*;
/**
* This class demonstrates that a vertical JSlider does not
* properly align itself horizontally when placed in a container
* larger than its preferred size.
*/
public class JSliderBug extends JPanel
{
public JSliderBug()
{
super(new GridLayout(1,2));
setBorder(BorderFactory.createTitledBorder("JSlider Alignment Bug"));
// Create two sliders, verify the alignment on the slider to be
// used in the border layout
JSlider slider1 = new JSlider(JSlider.VERTICAL, 0, 99, 50);
JSlider slider2 = new JSlider(JSlider.VERTICAL, 0, 99, 50);
slider1.setAlignmentX(0.5f);
slider1.setAlignmentY(0.5f);
// Try to center the natural way, using a border layout in the "Center"
JPanel borderPanel = new JPanel(new BorderLayout());
borderPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout"));
borderPanel.add(slider1, "Center");
// Try to center using gridbag, with glue on left
// and right to squeeze slider into place
JPanel gridbagPanel = new JPanel(new GridBagLayout());
gridbagPanel.setBorder(BorderFactory.createTitledBorder("GridBagLayout"));
GridBagConstraints c = new GridBagConstraints();
c.gridx=1; c.fill=GridBagConstraints.VERTICAL; c.weighty=1.0;
gridbagPanel.add(slider2, c);
c.gridx=0; c.fill=GridBagConstraints.BOTH; c.weighty=0.0;
gridbagPanel.add(Box.createHorizontalGlue(), c);
c.gridx=2; c.fill=GridBagConstraints.BOTH;
gridbagPanel.add(Box.createHorizontalGlue(), c);
add(borderPanel);
add(gridbagPanel);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("JSlider Alignment Bug");
Container contentPane = frame.getContentPane();
contentPane.add(new JSliderBug(), "Center");
frame.setSize(300,300);
frame.setVisible(true);
}
}
</code>
--------------
9/25/99 eval1127@eng -- tried unsuccessfully to get it to work with BoxLayout. Not clear whether this is really a bug or a minor usage error (on my part). Am filing
reference bug #.
(Review ID: 95238)
======================================================================