1 2 Previous Next 19 Replies Latest reply: Jan 11, 2010 12:15 AM by 793415 RSS

    Display Arabic text

    843810
      Hello All,


      I have a sample arabic text, which i have stored into file. I want to read that file and display the arabic text on the screen.

      If anyone has any idea please give me some inputs.


      Thanks & Regards,
      Faiz.
        • 1. Re: Display Arabic text
          793415
          faiz.india wrote:
          ..I have a sample arabic text, which i have stored into file. I want to read that file and display the arabic text on the screen.
          This is probably not the answer to your question. I myself am a 'newbie' when it comes to internationalization, but I thought this code was cool when I first wrote it. And I still do. ;-)

          See [http://forums.sun.com/thread.jspa?threadID=5311160].
          • 2. Re: Display Arabic text
            843810
            Hi Andrew,

            Thanks for your reply.

            Actually i am looking out for a different case, my question is quite clear. I have arabic text in a file, i want to read it and print it on the system console.

            Faiz.
            • 3. Re: Display Arabic text
              793415
              faiz.india wrote:
              ..system console.
              On the console! It is good you mentioned that. From your first post, I thought you meant in a GUI.

              I am pretty sure most consoles will not handle such character sets (some IL8N expert correct me if I am wrong). Instead the console will display all question marks '?' or something like that.
              • 4. Re: Display Arabic text
                843810
                What do i have to do if i want to display the arabic text in a text field, some other solution other that what you gave me earlier.

                thanks.
                • 5. Re: Display Arabic text
                  793415
                  faiz.india wrote:
                  What do i have to do if i want to display the arabic text in a text field, ..
                  Do you mean a java.awt.TextField, a javax.swing.JTextField, or something else?

                  For programmers, it pays to be exact about what you mean.
                  • 6. Re: Display Arabic text
                    843810
                    The swings javax.swing.JTextField control.
                    • 7. Re: Display Arabic text
                      DrClap
                      By "swings" I expect you must mean "Swing".

                      Anyway in that case you read the data from the file, making sure to use the correct charset for the file, and display the data in that component, making sure to use a font which can render the characters.
                      • 8. Re: Display Arabic text
                        843810
                        Hi,

                        If would be great if you can give me some working example.

                        Thanks & Regards,
                        Faiz.
                        • 9. Re: Display Arabic text
                          DrClap
                          The code is pretty trivial. Perhaps we could start from your non-working code instead?
                          • 10. Re: Display Arabic text
                            843810
                            Hi,

                            I have wrote some code for reading the text from file, and have also hardcoded some arabic text in TextField which shows up properly. But after reading the arabic text from file and setting the text to TextField the text is not shown properly its like ??????.

                            Please see the source code below.
                            /*
                             * ReadText.java
                             *
                             * Created on December 2, 2009, 8:42 AM
                             */
                            package arabicsample;
                            
                            import java.awt.Color;
                            import java.awt.ComponentOrientation;
                            import java.awt.Font;
                            import java.io.BufferedReader;
                            import java.io.File;
                            import java.io.FileInputStream;
                            import java.io.IOException;
                            import java.io.InputStreamReader;
                            import java.io.UnsupportedEncodingException;
                            import java.nio.charset.Charset;
                            
                            /**
                             *
                             * @author  faiz.chachiya
                             */
                            public class ReadText extends javax.swing.JFrame {
                            
                                /** Creates new form ReadText */
                                public ReadText() {
                                    initComponents();
                                }
                            
                                /** This method is called from within the constructor to
                                 * initialize the form.
                                 * WARNING: Do NOT modify this code. The content of this method is
                                 * always regenerated by the Form Editor.
                                 */
                                // <editor-fold defaultstate="collapsed" desc="Generated Code">
                                private void initComponents() {
                            
                                    jLabel1 = new javax.swing.JLabel();
                                    txtArabic = new javax.swing.JTextField();
                                    jButton1 = new javax.swing.JButton();
                            
                                    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                            
                                    jLabel1.setText("Text");
                            
                                    txtArabic.setFont(new java.awt.Font("Lucida Sans", 0, 10));
                                    txtArabic.setText("&#1603;&#1610;&#1601; &#1581;&#1575;&#1604;&#1603; ");
                            
                                    jButton1.setText("Read");
                                    jButton1.addActionListener(new java.awt.event.ActionListener() {
                                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                            jButton1ActionPerformed(evt);
                                        }
                                    });
                            
                                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                                    getContentPane().setLayout(layout);
                                    layout.setHorizontalGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addContainerGap()
                                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(txtArabic, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGap(162, 162, 162)
                                                    .addComponent(jButton1)))
                                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    );
                                    layout.setVerticalGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(txtArabic, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addContainerGap(22, Short.MAX_VALUE))
                                    );
                            
                                    pack();
                                }// </editor-fold>
                                private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
                                    // TODO add your handling code here:
                                    readTextFromFile();
                                }
                            
                                /**
                                 * @param args the command line arguments
                                 */
                                public static void main(String args[]) {
                                    java.awt.EventQueue.invokeLater(new Runnable() {
                            
                                        public void run() {
                                            new ReadText().setVisible(true);
                                        }
                                    });
                                }
                            
                                
                                public void readTextFromFile() {
                            
                                    try {
                                        final File file = new File(System.getProperty("user.dir") + "/conf/arabictext.txt");
                                        final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("utf-8")));
                                        final String str = br.readLine();
                            
                            //Charset.forName("utf-8").newEncoder().encode(in)
                            
                                        System.out.println("File text : " + str);
                                        System.out.println("            " + txtArabic.getText());
                                        System.out.println("Reading Process Completly Successfully.");
                            
                                        Font font = txtArabic.getFont();
                                        System.out.println("GetFont " + font);
                            
                                        txtArabic.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                                        txtArabic.setBackground(Color.ORANGE);
                                        txtArabic.setText(str);
                            
                            
                                        final String UTF8Str = new String(str.getBytes());
                                        System.out.println(UTF8Str);
                            
                                    } catch (UnsupportedEncodingException ue) {
                            
                                        System.out.println("Not supported : ");
                            
                                    } catch (IOException e) {
                            
                                        System.out.println(e.getMessage());
                            
                                    }
                            
                            
                            
                                }
                                // Variables declaration - do not modify
                                private javax.swing.JButton jButton1;
                                private javax.swing.JLabel jLabel1;
                                private javax.swing.JTextField txtArabic;
                                // End of variables declaration
                            }
                            Please see the UTF-8 text file below. When i open the file, i see the text something similar in below.
                            ??? ????
                            One from UTF file from wordpad.
                            &#1603;&#1610;&#1601; &#1581;&#1575;&#1604;&#1603;
                            So can you please correct where i am going wrong.


                            Thanks and Regards,
                            Faiz.
                            • 11. Re: Display Arabic text
                              843810
                              Hi,

                              I have got the example working.

                              Thanks for all the help.

                              Regards,
                              Faiz.
                              • 12. Re: Display Arabic text
                                843810
                                Hi,

                                I have one another question, when i save the arabic text in the properties file, it looks like the text below.

                                Message.properties*

                                value=&#1603;&#1610;&#1601; &#1581;&#1575;&#1604;&#1603;

                                If i open the file in notepad, i can see the text like below

                                value=\u0643\u064A\u0641 \u062D\u0627\u0644\u0643

                                i think this text is into UTF16 format.


                                Now i will come my point. I have my arabic string i would like to see that string in UTF16 (e.g \u064A).

                                Any example, i have tried changing the string encoding to UTF16, but nothing really worked.


                                Regards,
                                Faiz.
                                • 13. Re: Display Arabic text
                                  843810
                                  I have solved this problem as well.


                                  Regards,
                                  Faiz.
                                  • 14. Re: Display Arabic text
                                    843810
                                    how did u make it working
                                    please help me
                                    contact me directly please
                                    amrelfaham@yahoo.com
                                    1 2 Previous Next