JDK-4230443 : ComponentOrientation has no effect on editable JComboBox
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-04-16
  • Updated: 2022-03-08
  • Resolved: 2022-03-08
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
Name: clC74495			Date: 04/16/99


Editable JComboBox is always left aligned even if its 
ComponentOrientation is set to ComponentOrientation.RIGHT_TO_LEFT.
Otherwise if it's non-editable, it responds correctly by right-aligning
its contents.


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 (###@###.###)
*/

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 );
      }
    }
  }
}

(Review ID: 56797)
======================================================================
###@###.### 10/13/04 18:48 GMT