Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hebrew text in JTextField (Swing)

When I do something like fileText.setText(path) in a JTextField, it works well unless the text is in Hebrew (or combines English and Hebrew). Then I get something like this:

enter image description here

I tried different fonts (even fonts that "Hebrew" is mentioned in them), but it didn't help. How do I fix it?

By the way, it is working properly with the ToolTipText (fileText.setToolTipText(path))

Here's my code:

// browse files or folders
    public void browse(JTextField txtField) {

        JFileChooser fileChooser = new JFileChooser();

        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));     

        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int result = fileChooser.showOpenDialog(this);

        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedDir = fileChooser.getSelectedFile();
            String path = selectedDir.getAbsolutePath();

            if (txtField == srcText) {
                srcText.setText(path); 
                srcText.setToolTipText(path); 
            }
            else {
                if (txtField == dstText) {
                    dstText.setText(path); 
                    dstText.setToolTipText(path);
                }
                }}
    }
like image 848
user2653179 Avatar asked Oct 30 '22 09:10

user2653179


1 Answers

Not an answer, since your code works well as it is. Please try to your environment.

For me it works flawlessly out of the box with the default font on Windows 7. Java JDK1.8.0_31

public class JTextFieldExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public JTextFieldExample() {
        super("TextField Test Demo");
        final Container container = getContentPane();
        final JTextField textField=new JTextField("hello \u05DD\u05D5\u05DC\u05E9 Hello \u05DD\u05D5\u05DC\u05E9"); 
        // optionally set RTL
        textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        container.add(textField);
        setSize(300,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(final String args[]) {
        new JTextFieldExample();
    }
}

Makes a window with a JTextField containing:

hello םולש Hello םולש

(I am sorry if I am using something strange or offensive in Hebrew. I just copied the unicode chars from another page, they claim it means "hello").

I've also tried you code in a test app and that is worked well, too. Also Hebrew-only, English-Hebrew mixtures work well.

However you may prefer to set the RTL orientation to better match to Hebrew, and I guess in my example, the Hebrew letters are displayed in reverse order disregarding to the actual orientation.

Do the following:

  • check if the JTextField work well in Hebrew? If so, then there is something odd in the path returned by the file selector
  • check the path by priting it to the console. Locate chars which can cause problems, e.g. \-es, or broken unicode code points
  • dump the bytes of the string in hexa. This can reveal e.g. unicode byte ordering marks or broken unicode code points.
like image 141
Gee Bee Avatar answered Nov 08 '22 05:11

Gee Bee