在我的JtextPane上不自动显示键盘上键入的字符 [英] typed charcter of my keyboard without display it automoticlly on my JtextPane

查看:108
本文介绍了在我的JtextPane上不自动显示键盘上键入的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JtextPane 的用例有疑问.确实,我使用MVC架构取消了应用程序级别.我的框架上有一个带侦听器的Jtextpane,允许所有用户编辑文本.

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.

但是,正如MVC架构所希望的(以及我也想要的那样),我必须控制键入的字符,然后才能在JtextPane上显示它.因此,我使用Observer/Observable模式来更新我的JtextPane.

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.

但是,如何在不自动在JtextPane上自动显示键盘的情况下键入键盘的任何字符.的确,当我按下键盘上的任何键时,它会自动显示它.就像我说的那样,我想自己更新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.

当然,如果我这样做:

 mytextPane.setEnabled(false)

我的keyListener无法正常工作,因此也无法进行任何控制...

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

推荐答案

请勿使用KeyListener.

Don't use a KeyListener.

Swing Document支持DocumentFilter,允许您在将文本插入Document之前编辑/验证文本.

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

}

请参见Swing教程中实现DocumentFilter 有关更多信息和示例.

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

这篇关于在我的JtextPane上不自动显示键盘上键入的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆