Warm tip: This article is reproduced from stackoverflow.com, please click
java jtextpane keylistener swing

typed charcter of my keyboard without display it automoticlly on my JtextPane

发布于 2020-03-27 10:20:07

I have a question about use case of JtextPane. Indeed, I delevelop an application using an MVC architecture. My frame has a Jtextpane with keylistener to permit all user to edit text.

But as MVC architecture wants (and as i want too), I have to control characters typed before display it on the JtextPane. So, I use the Observer/Observable pattern to update my JtextPane.

But, how can I typed any charcter of my keyboard without display it automoticlly on my JtextPane. Indeed, when I press any key on my keyboard, it display it automaticly .. Like I said I want to update my JtextPane by myself.

Of course, if I do :

 mytextPane.setEnabled(false)

my keyListener can't works and so any control too...

Questioner
Bigote
Viewed
136
camickr 2019-10-24 22:44

Don't use a KeyListener.

The Swing Document supports a DocumentFilter, which allows you to edit/verify the text before the text is inserted into the Document.

For example, the following code will convert each character to upper case as it is typed:

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

public class UpperCaseFilter extends DocumentFilter
{
    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException
    {
        replace(fb, offs, 0, str, a);
    }

    public void replace(FilterBypass fb, final int offs, final int length, final String str, final AttributeSet a)
        throws BadLocationException
    {
        if (str != null)
        {
            String converted = convertString(str);
            super.replace(fb, offs, length, converted, a);
        }
    }

    private String convertString(String str)
    {
        char[] upper = str.toCharArray();

        for (int i = 0; i < upper.length; i++)
        {
            upper[i] = Character.toUpperCase(upper[i]);
        }

        return new String( upper );
    }

    private static void createAndShowGUI()
    {
        JTextField textField = new JTextField(10);
        AbstractDocument doc = (AbstractDocument) textField.getDocument();
        doc.setDocumentFilter( new UpperCaseFilter() );

        JFrame frame = new JFrame("Upper Case Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new java.awt.GridBagLayout() );
        frame.add( textField );
        frame.setSize(220, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }

}

See the section from the Swing tutorial on Implementing a DocumentFilter for more information and examples.