Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4188885

BoxLayout not aligning mixed components correctly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • client-libs



      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)
      ======================================================================

            Unassigned Unassigned
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: