3

DocumentListenerドキュメントの変更を処理するためにa を使用しJTextPaneます。ユーザーが入力している間に、コンテンツを削除して、JTextPane代わりにカスタマイズされたテキストを挿入したいと考えています。でドキュメントを変更することはできませんDocumentListener。代わりに、ここで解決策が述べられています: java.lang.IllegalStateException while using Document Listener in TextArea, Java 、しかし私はそれを理解していません、少なくとも私は何をすべきかわかりません私の場合?

4

3 に答える 3

12

DocumentListener実際には変更の通知にのみ有効であり、テキスト フィールド/ドキュメントの変更には使用しないでください。

代わりに、DocumentFilter

例はこちらをご覧ください

ご参考までに

あなたの問題の根本的なコースはDocumentListener、ドキュメントが更新されている間に通知されることです。ドキュメントを変更しようとすると (無限ループのリスクは別として)、ドキュメントが無効な状態になるため、例外が発生します。

例で更新

これは非常に基本的な例です...挿入または削除を処理しませんが、私のテストでは、とにかく何もせずに削除が機能していました...

ここに画像の説明を入力

public class TestHighlight {

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

    public TestHighlight() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextPane textPane = new JTextPane(new DefaultStyledDocument());
                ((AbstractDocument) textPane.getDocument()).setDocumentFilter(new HighlightDocumentFilter(textPane));
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(textPane));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class HighlightDocumentFilter extends DocumentFilter {

        private DefaultHighlightPainter highlightPainter = new DefaultHighlightPainter(Color.YELLOW);
        private JTextPane textPane;
        private SimpleAttributeSet background;

        public HighlightDocumentFilter(JTextPane textPane) {
            this.textPane = textPane;
            background = new SimpleAttributeSet();
            StyleConstants.setBackground(background, Color.RED);
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
            System.out.println("insert");
            super.insertString(fb, offset, text, attr);
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            System.out.println("remove");
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

            String match = "test";

            super.replace(fb, offset, length, text, attrs);

            int startIndex = offset - match.length();
            if (startIndex >= 0) {

                String last = fb.getDocument().getText(startIndex, match.length()).trim();
                System.out.println(last);
                if (last.equalsIgnoreCase(match)) {

                    textPane.getHighlighter().addHighlight(startIndex, startIndex + match.length(), highlightPainter);

                }

            }
        }

    }

}
于 2013-02-06T11:13:57.807 に答える
5

ユーザーが入力している間に、JTextPane のコンテンツを削除し、代わりにカスタマイズされたテキストを挿入したいと考えています。

  • これはDocumentListenerの仕事ではありません。基本的に、このListenerは、 JTextComponentから別の JComponent、Swing GUI、使用されている Java で実装されたメソッドにイベントを発生させるように設計されています。

  • DocumentFilterを見てください。これは、実行時に独自のDocument ( JTextComponentsのモデル)を変更、変更、または更新するための望ましいメソッドを提供します

于 2013-02-06T11:14:37.290 に答える
-1

呼び出したコードをラップするSwingUtilities.invokeLater()

于 2013-02-06T11:33:47.050 に答える