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

JComboBox inconsistent with RIGHT_TO_LEFT Component Orientation

    Details

    • Subcomponent:
    • Understanding:
      Fix Understood
    • CPU:
      x86
    • OS:
      windows_2000

      Description

      Name: jk109818 Date: 01/13/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

      FULL OPERATING SYSTEM VERSION :

      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      When using a JComboBox with String item and setting its
      component orientation to RIGHT_TO_LEFT, the text doesn't
      right-align while the caret does move to left (the correct
      behavior for RTL). But after making selection changes in
      the combobox and changing the component orientation a few
      times (LEFT_TO_RIGHT, then back to RIGHT_TO_LEFT), it'll
      work.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run the sample given (this is the sample code from bug
      report 4230443)
      2. click on rtl 3. select "Two" in the combo box
      4. click on ltr
      5. click on rtl

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      after step 2, you'll see that the text is left aligned.
      but after step 5, the text will be right aligned.

      It should be right aligned in both cases.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      /*
       This is a testcase for ComponentOrientation bugs in JComboBox. Please note
       the following problems:
          1- setComponentOrientation() by itself has no effect on JComboBox. You have
             to loop through its children to get the desired effect.
          2- Even when you do so, the button and the scrollbar are still always on
      the
             right side regardless of the current orientation. They should be on the
             left side for RIGHT_TO_LEFT orientation.
          3- When the JComboBox is editable, it cannot be flipped.
      */

      /*
      ** @author Mohamed Nosseir xxxxx@xxxxx
      */

      public class DemoFrame extends JFrame implements ItemListener
      {
        BorderLayout borderLayout1 = new BorderLayout();
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();

        String[] data = {"One", "Two", "Three", "Four"};
        JComboBox jCombo1 = new JComboBox(data);
        JComboBox jCombo2 = new JComboBox(data);

        JRadioButton jrbLTR = new JRadioButton("LTR", true);
        JRadioButton jrbRTL = new JRadioButton("RTL");

        public static void main(String[] argv)
        {
          Frame frame = new DemoFrame();
          //Center the window
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          Dimension frameSize = frame.getSize();
          if (frameSize.height > screenSize.height)
          {
            frameSize.height = screenSize.height;
          }
          if (frameSize.width > screenSize.width)
          {
            frameSize.width = screenSize.width;
          }
          frame.setLocation((screenSize.width - frameSize.width)/2,
      (screenSize.height - frameSize.height)/2);
          frame.addWindowListener(new WindowAdapter() { public void
      windowClosing(WindowEvent e) { System.exit(0); } });
          frame.setVisible(true);
        }

        public DemoFrame()
        {
          super();
          try
          {
            jbInit();
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }

        public void itemStateChanged(ItemEvent e)
        {
          if(e.getStateChange() == ItemEvent.SELECTED)
          {
            if(e.getSource() == jrbRTL)
              applyOrientation(jPanel1, ComponentOrientation.RIGHT_TO_LEFT);
            else if(e.getSource() == jrbLTR)
              applyOrientation(jPanel1, ComponentOrientation.LEFT_TO_RIGHT);

            jPanel1.validate();
            jPanel1.repaint();
          }
        }

        private void jbInit() throws Exception
        {
          this.setTitle("ComboBox Orientation Sample");
          this.getContentPane().setLayout(borderLayout1);
          this.setSize(new Dimension(400, 300));
          this.getContentPane().add(jPanel1, BorderLayout.CENTER);
          this.getContentPane().add(jPanel2, BorderLayout.SOUTH);

          jCombo1.setMaximumRowCount(3);
          jCombo2.setMaximumRowCount(3);
          jCombo2.setEditable(true);

          jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.X_AXIS));
          jPanel1.add(jCombo1);
          jPanel1.add(jCombo2);


          jPanel2.add(jrbLTR);
          jPanel2.add(jrbRTL);

          ButtonGroup bg = new ButtonGroup();
          bg.add(jrbLTR);
          bg.add(jrbRTL);

          jrbLTR.addItemListener(this);
          jrbRTL.addItemListener(this);
        }

        /*
        ** applyOrientation():
        ** Borrowed from SwingApplet demo!
        */
        private void applyOrientation(Component c, ComponentOrientation o)
        {
          c.setComponentOrientation(o);

          if( c instanceof JMenu )
          {
            JMenu menu = (JMenu)c;
            int ncomponents = menu.getMenuComponentCount();
            for (int i = 0 ; i < ncomponents ; ++i)
            {
              applyOrientation( menu.getMenuComponent(i), o );
            }
          }
          else if( c instanceof Container )
          {
            Container container = (Container)c;
            int ncomponents = container.getComponentCount();
            for (int i = 0 ; i < ncomponents ; ++i)
            {
              applyOrientation( container.getComponent(i), o );
            }
          }
        }
      }

      ---------- END SOURCE ----------
      (Review ID: 178492)
      ======================================================================
      ###@###.### 10/13/04 18:47 GMT

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jkimsunw Jeffrey Kim (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: