UTF-8 support issue to Java Swing?

10,698

Solution 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());
}

Solution 2

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();
            }
        });
    }
}
Share:
10,698
Human Being
Author by

Human Being

Always willing to learn the new things to enhance my knowledge .

Updated on June 04, 2022

Comments

  • Human Being
    Human Being over 1 year

    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..

  • Human Being
    Human Being almost 11 years
    I used area.setText(tf.getText()); . bue doesn't work..
  • Human Being
    Human Being almost 11 years
    I used area.setText(tf.getText()); . but doesn't work..It shows lot of square boxes in text box and ???????? in textarea.
  • Philippe Marschall
    Philippe Marschall almost 11 years
    If you look at the result of tf.getText() in the debugger, does the string look ok you do you just get ?????????
  • Rafael Odon
    Rafael Odon over 5 years
    This is more a comment ;) [2]