JLabel
, JTextField
and JTextArea
are able to handle bi-directional text
and display it correctly
import javax.swing.*;
import java.awt.*;
public class BiDi extends JFrame {
public final static char RIGHT_TO_LEFT_EMBEDDING = '\u202B';
public final static char RIGHT_TO_LEFT_OVERRIDE = '\u202E';
public final static char POP_DIRECTIONAL_FORMATTING = '\u202C';
/** This string is logically "abcDEFghi". But by overriding the
* direction, it will appear as "abcFEDghi"
*/
public static String bidiStr = "abc" + RIGHT_TO_LEFT_OVERRIDE + "DEF" +
POP_DIRECTIONAL_FORMATTING + "ghi";
public static void main(String[] args) {
BiDi app = new BiDi();
app.pack();
app.setVisible(true);
}
public BiDi() {
Font font = new Font("Monospaced,", Font.PLAIN, 24);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
JLabel label = new JLabel(bidiStr);
label.setFont(font);
content.add(label);
JTextField textField = new JTextField(bidiStr);
textField.setFont(font);
content.add(textField);
JTextArea textArea = new JTextArea(bidiStr + '\n' + bidiStr + '\n' + bidiStr);
textArea.setFont(font);
content.add(textArea);
}
}
GridLayout
use left-to-right by default
ComponentOrientation
class
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
public class Orientation extends JFrame {
public final static char RIGHT_TO_LEFT_EMBEDDING = '\u202B';
public final static char RIGHT_TO_LEFT_OVERRIDE = '\u202E';
public final static char POP_DIRECTIONAL_FORMATTING = '\u202C';
/** This string is logically "abcDEFghi". But by overriding the
* direction, it will appear as "abcFEDghi"
*/
public static String bidiStr = "abc" + RIGHT_TO_LEFT_OVERRIDE + "DEF" +
POP_DIRECTIONAL_FORMATTING + "ghi";
public static void main(String[] args) {
Orientation app = new Orientation();
app.pack();
app.setVisible(true);
}
public Orientation() {
Font font = new Font("Monospaced,", Font.PLAIN, 24);
ComponentOrientation orientation =
ComponentOrientation.getOrientation(Locale.getDefault());
GridLayout layout = new GridLayout(1, 2);
Container content = getContentPane();
// GridLayout uses the orientation of its container
content.setComponentOrientation(orientation);
content.setLayout(layout);
JLabel label = new JLabel(bidiStr);
label.setFont(font);
content.add(label);
JTextField textField = new JTextField(bidiStr);
textField.setFont(font);
content.add(textField);
}
}
java -Duser.language=ar -Duser.country=EG Orientation