RemoveAccents.java Source code

Java tutorial

Introduction

Here is the source code for RemoveAccents.java

Source

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Normalizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RemoveAccents extends JFrame {
    public RemoveAccents() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel pnl = new JPanel();
        pnl.add(new JLabel("Enter text"));
        final JTextField txtText;
        txtText = new JTextField("to be removed");
        pnl.add(txtText);

        JButton btnRemove = new JButton("Remove");
        ActionListener al;
        al = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = txtText.getText();
                text = Normalizer.normalize(text, Normalizer.Form.NFD);
                txtText.setText(text.replaceAll("[^\\p{ASCII}]", ""));
            }
        };
        btnRemove.addActionListener(al);
        pnl.add(btnRemove);

        getContentPane().add(pnl);

        pack();
        setVisible(true);
    }

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