-1

I try to use prompt in swingx.JXTextField with Arabic font but it it's not working.

My Code:

JXTextField field1 = new JXTextField("عربي"); //not working

But if I use English text it is working as expected:

JXTextField field1 = new JXTextField("bdff"); //it is working

How can I fix this problem?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
yazan
  • 1

3 Answers3

1

The JXTextField extends JTextField, so anything that works in a standard Swing text field should work in the JX variant.

This problem likely comes down to support by the font used in the text field. The Arabic displays correctly here using the default font, but if it's not the same there, iterate the font families to find one that supports the text.

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

public class ArabicInTextField {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String arabic = "عربي";
                JTextField textField = new JTextField(arabic);
                int result = JOptionPane.showConfirmDialog(
                        null, textField, "Does this show as expected?",
                        JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.NO_OPTION) {
                    String[] fontFamilies = GraphicsEnvironment.
                            getLocalGraphicsEnvironment().
                            getAvailableFontFamilyNames();
                    for (String fontFamily : fontFamilies) {
                        Font font = new Font(fontFamily, Font.PLAIN, 20);
                        if (font.canDisplayUpTo(arabic) < 0) {
                            textField.setFont(font);
                            JOptionPane.showMessageDialog(
                                    null, textField, fontFamily,
                                    JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                }
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Here, 23 of around 250+ installed fonts (less than 10%) will display all characters of that string. Some of those fonts are logical fonts (they are mapped to a physical font at run-time, depending on the PLAF), so the true number is actually less.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Not sure what you mean by "not working". I've been able to make it work very easily through Intellij. If you're still having issues this would set the encoding to UTF-8 which may help

        String str = new String("تعطي يونيكود رقما فريدا لكل حرف", "UTF-8");
Totalattak
  • 24
  • 5
0

Don't pass anything to the JXTextField constructor when creating the object. Create it first and then change the text displayed using setText(...). Here is a working example:

public static void main(String[] args) {
        JXTextField tb = new JXTextField(); //Keep this blank
        tb.setText("عربي");//Set your text now!
        JXPanel panel = new JXPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(tb);
        JXFrame frame = new JXFrame();
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.add(panel);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

Image

M. Al Jumaily
  • 731
  • 1
  • 6
  • 21