How do I alter the reading order of components in a JPanel? When I change a panel's Locale to one with a Right to Left order, the components are still laid out Left to Right.

Joe Sam Shirah

The general answer in 1.2 or later is to use ResourceBundles and then call Window.applyResourceBundle(). This will do all the grunt work for you so FlowLayout and BorderLayout can take accout of the order. Just setting the locale does nothing for the reading order or ComponentOrientation, to use the API term. In that case, one also has to call

setComponentOrientation(ComponentOrientation o)
for each component affected.

I've included code that will cause components in a panel to change from left to right ( assuming that is the initial default, ) to right to left, using the locale for arabic - Kuwait. Note that only the JPanel ( and its LayoutManager ) is affected.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class JRTLLocale extends JFrame
                        implements ActionListener,
  boolean    boolInitial = true;

  JButton    jb = new JButton( "Initial/RTL" );
  JLabel     jl = new JLabel("Locale Country");
  JPanel     jp = new JPanel();
  JTextField jt = new JTextField( getLocale().getDisplayCountry());

  Locale     lRTL = new Locale( "ar", "KW" ), // Kuwait
             lInitial = getLocale();

  public JRTLLocale()
    super( "JRTLLocale" );
    addWindowListener( this );

    jb.addActionListener( this );


    Container myCP = getContentPane();
    myCP.add( jp, BorderLayout.CENTER );

  }  // end constructor

  // ActionListener Implementation
  public void actionPerformed(ActionEvent e)
    if( boolInitial ) { jp.setLocale( lRTL ); }
    else              { jp.setLocale( lInitial ); }

                   jp.getLocale() ) );

    jt.setText( jp.getLocale().getDisplayCountry() );
    boolInitial = !boolInitial; // toggle

  }  // End actionPerformed

// Window Listener Implementation
  public void windowOpened(WindowEvent e) {}
  public void windowClosing(WindowEvent e)
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
// End Window Listener Implementation

  public static void main(String[] args)
    new JRTLLocale();

}  // end class JRTLLocale
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact