5

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);
                }
                }}
    }
E-Riz
  • 31,431
  • 9
  • 97
  • 134
user2653179
  • 393
  • 1
  • 6
  • 21
  • 1
    What kind of JDK are you using ? – Arnaud Feb 19 '16 at 16:02
  • 1
    *"I tried different fonts (even fonts that "Hebrew" is mentioned in them) .."* See [`Font.canDisplayUpTo(String)`](https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html#canDisplayUpTo-java.lang.String-). – Andrew Thompson Feb 19 '16 at 16:03
  • 5
    Maybe this BUG is relevant to your case : https://bugs.openjdk.java.net/browse/JDK-8133246 – Arnaud Feb 19 '16 at 16:11
  • 6
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Feb 19 '16 at 20:38
  • I tried and I don't have any problem, post your code. – user1803551 Feb 23 '16 at 00:20
  • Thanks for the help. I added it. – user2653179 Feb 23 '16 at 01:24
  • Are you setting the [`RUN_DIRECTION`](https://docs.oracle.com/javase/tutorial/i18n/text/bidi.html)? – Elliott Frisch Feb 23 '16 at 01:32
  • @ElliottFrisch I tried it. It didn't help – user2653179 Feb 23 '16 at 03:11
  • 2
    I still have no trouble. Please post a proper [mcve], include the exact string path you that gives you the problem. Add the information of your JDK version and your OS. – user1803551 Feb 23 '16 at 17:08

1 Answers1

2

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.
Gee Bee
  • 1,794
  • 15
  • 17
  • I tend to put a notice on the top along the lines of "This is not an answer yet, but a comment with a lot of code and details" for this type of post to avoid the Not An Answer flag. – user1803551 Feb 23 '16 at 21:41
  • The answer is that the problem could not be reproduced with Java 8 and Windows 7, therefore the original code in question works well. – Gee Bee Feb 23 '16 at 22:03
  • It's not an answer, it's an observation. You do not instruct the OP how to fix or work around their current problem. Finally, you are asking the OP a question in your answer which further invalidates it as an answer. See my comments [here](http://stackoverflow.com/questions/35509414/hebrew-text-in-jtextfield-swing/35588736?noredirect=1#comment58821206_35509414) abd [here](http://stackoverflow.com/questions/35509414/hebrew-text-in-jtextfield-swing/35588736?noredirect=1#comment58854173_35509414). – user1803551 Feb 23 '16 at 22:05
  • True. The problem can not be reproduced, which makes it tricky to fix. I think the problem is in the path returned by the underlying OS, but that is an _assumption_. I tried to do my best to help solving the problem, and given some hints what to do. – Gee Bee Feb 23 '16 at 22:13
  • I didn't say that anything you did is wrong. If you read my initial comment again, I suggested you "protect" your "answer" by adding a notice to it explaining what it's trying to do. If you are flagged as Not An Answer your post will just be deleted. – user1803551 Feb 23 '16 at 22:20
  • Thank you for the suggestion, I put there that "not an answer". – Gee Bee Feb 23 '16 at 22:30
  • 1
    The Hebrew output of the proposed solution is reversed, the `setComponentOrientation` function alone does not behave properly (please refer to this link: http://stackoverflow.com/questions/9659133/java-display-text-on-right-side-of-text-area#9659309), usage of `JTextField#setHorizontalAlignment` is preferred. – Elian Kamal Feb 26 '16 at 20:07