2

Possible Duplicate:
how to implement UTF-8 format in Swing application?

In Swing application I have the send button, one text area and a text field.

If I press the send button, I need to send the text from text field to text area

It's working fine in English But not in the local language...

package package1;

import java.awt.*;
import java.awt.event.*;
import java.io.UnsupportedEncodingException;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

class AEvent extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;
    JTextField tf;
    JTextArea area ;
    Border border;

    AEvent(){

        area = new JTextArea(200,200);
        area.setBounds(60,200,300,200);

        border = BorderFactory.createLineBorder(Color.BLACK);
        area.setBorder(border);

        tf=new JTextField();
        tf.setBounds(60,70,150,20);

        Button b=new Button("click me");
        b.setBounds(100,120,80,30);

        b.addActionListener(this);

        add(b);
        add(tf);
        add(area);

        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });

        setSize(600,600);
        setLayout(null);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e){
        String s = null;
        try {
            s = new String(tf.getText().getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        area.setText(s);
    }

    public static void main(String args[]){
        new AEvent();

    }
}

Please give some Idea or some code that will help me to solve this..

Community
  • 1
  • 1
Human Being
  • 8,269
  • 28
  • 93
  • 136
  • what is the example of line in local language? Try to work around with `JTextAre.setFont()` – Nikolay Kuznetsov Dec 11 '12 at 07:29
  • This should work. I would also suspect some font problem as @NikolayKuznetsov indicates. –  Dec 11 '12 at 07:31
  • @UwePlonus: this *is* an exact duplicate. He even copied the useless repetition of dots. –  Dec 11 '12 at 07:33
  • @UwePlonus, it think he didn't post that time and topic was outdated. So he posts SSCCE now. – Nikolay Kuznetsov Dec 11 '12 at 07:33
  • @NikolayKuznetsov: but the other question does contain the same code. Enhancing a question is much preferred over creating a new one with the same problem. This one should be closed. –  Dec 11 '12 at 07:34
  • 2
    @Nikolay the other code is 90% the exact copy. So enhancing the other question would be sufficient. – Uwe Plonus Dec 11 '12 at 07:35
  • @UwePlonus, how would some one attract community attention to a question created couple of days ago? – Nikolay Kuznetsov Dec 11 '12 at 07:36
  • 3
    @Nikolay how does the community profit if we answer the same question again and again? If the question is new (with more focus on the problem) then the answers would be profitable for the community and the OP. – Uwe Plonus Dec 11 '12 at 08:08

4 Answers4

1

This breaks if the the platform default encoding is not utf-8. tf.getText().getBytes() gives you the bytes in the platform default encoding. new String(tf.getText().getBytes(), "UTF-8") will create a corrupt string if the actual encoding of the bytes is not utf-8.

try

public void actionPerformed(ActionEvent e){
    area.setText(tf.getText());
}
Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
1

OK, so if you are still not convinced of Phillipe's answer, here is a fully working code which demonstrates the good answer. I tried it with accented characters and it works just fine. If it breaks, please indicate how.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class AEvent implements ActionListener {

    private JTextField tf;
    private JTextArea area;
    private JFrame frame;

    protected void initUI() {
        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        area = new JTextArea(30, 80);
        area.setEditable(false);
        area.setFocusable(false);
        tf = new JTextField();
        JButton b = new JButton("click me");
        b.addActionListener(this);

        JScrollPane scrollPane = new JScrollPane(area);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        frame.add(scrollPane, gbc);
        gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        frame.add(tf, gbc);
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        frame.add(b, gbc);
        frame.getRootPane().setDefaultButton(b);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
        tf.requestFocusInWindow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        area.append(tf.getText() + "\n");
        tf.setText("");
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AEvent().initUI();
            }
        });
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

Why this?

String s = null;
try {
    s = new String(tf.getText().getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
area.setText(s);

Why not just:

area.setText(tf.getText());
perp
  • 3,883
  • 4
  • 31
  • 29
-1

I tried to do do your objective with a local language it worked fine for me. Can you please specify which local language are you considering and on what IDE are you working?

Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46