GUIに2つのJTextAreasがあり、それぞれのJTextAreaにDocumentListenerがあります。たとえば、テキストエリア番号1にabcを入力すると、そのドキュメントテキストが表示されます何らかの方法でそれを修正し、JTextArea 2のドキュメントに出力してください。Java GUIのドキュメントモデル
私のリスナーと一緒に私はソースドキュメントを手に入れることができます。私はテキストを変更できますが、エラーが出る
例外スレッドで "AWT-EventQueueの-0" java.lang.IllegalStateException:通知に
PLEを変異させる試みaseヘルプ。
おかげ
はここにいくつかのコードです:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Maxi
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Test {
static JFrame frame = new JFrame("CaesarEncipherGUI");
static JPanel panel = new JPanel();
static JTextArea area = new JTextArea(5,20);
static JTextArea area1 = new JTextArea(5,20);
static class MyDocumentListener2 implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
updateLog(e,"");
}
public void removeUpdate(DocumentEvent e) {
updateLog(e,"");
}
public void changedUpdate(DocumentEvent e) {
}
public void updateLog(DocumentEvent e, String action){
Document doc = (Document)e.getDocument();
try{
System.out.println("Action detected "+doc.getProperty("type"));
String text = doc.getText(0, doc.getLength());
doc.insertString(0, "hey", null); //heres the line that throws the error.
//mutation of text here
}catch (BadLocationException catchme2){}
}
}
public static void main(String[] args){
area.getDocument().addDocumentListener(new MyDocumentListener2());
//initialize
frame.setResizable(false);
frame.setBounds(300, 300, 235, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(area);
panel.add(area1);
frame.add(panel);
frame.setSize(235,400);
frame.setVisible(true);
}
}
リスナーがドキュメント1にあり、ドキュメント2のテキストを変更しようとすると、このエラーが発生しますか?または、あなたが聴いている同じ文書のテキストを変更しようとした場合に限ります。私はそれが後者であり、問題はDocumentListener API、「注文に関する保証はありません」のセクションに詳しく記述されていると思います。 1つの解決策は、コードをラップしてRunnableのテキストを変更し、SwingUtilities.invokeLater(...)を介してEDTにキューイングすることです。 –
私がオンラインで読んだところでは、ドキュメントリスナー内のドキュメントテキストを変更しないと思われます。私はどこで文書のテキストの変更を実行しますか? – user541597
再度、EDTの変更をキューイングすることでこれを行うことができます。もう1つ、おそらくもっと良い解決策はDocumentFilterを使うことです。 –