swing - Java documentlistener - program stop working after input -
hi have problem documentlistener. program stop working after insert value textfield. program should xor 1st row of textfields 2nd row of textfields , put result 3rd row of textfields
package opa.beta1; import java.awt.borderlayout; import java.math.biginteger; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.event.documentevent; import javax.swing.event.documentlistener; public class textfieldevent { jtextfield arrayiv[] = new jtextfield[8]; jtextfield plaintextarray[] = new jtextfield[8]; jtextfield ciphertextarray[] = new jtextfield[8]; jframe frame = new jframe("opa"); jpanel panel1 = new jpanel(); jpanel panel2 = new jpanel(); jpanel panel3 = new jpanel(); public void setjpanel(jtextfield array[], jpanel container, string s) { (int = 0; < 8; i++) { array[i] = new jtextfield(s, 4); array[i].getdocument().adddocumentlistener(new documentlistener() { @override public void insertupdate(documentevent de) { setciphertext(); } @override public void removeupdate(documentevent de) { setciphertext(); } @override public void changedupdate(documentevent de) { setciphertext(); } }); container.add(array[i]); } } public string setxortext(jtextfield textfield1, jtextfield textfield2) { string okno1, okno2; okno1 = textfield1.gettext(); okno2 = textfield2.gettext(); if (okno1.equals("")) { return okno2; } biginteger pom1 = new biginteger(okno1, 16); if (okno2.equals("")) { return okno1; } biginteger pom2 = new biginteger(okno2, 16); biginteger res = pom1.xor(pom2); string s = res.tostring(16); return s; } public string setxortext(jtextfield pole1[], jtextfield pole2[], int i) { string okno1 = pole1[i].gettext(); string okno2 = pole2[i].gettext(); if (okno1.equals("")) { return pole2[i].gettext(); } biginteger pom1 = new biginteger(okno1, 16); if (okno2.equals("")) { return pole1[i].gettext(); } biginteger pom2 = new biginteger(okno2, 16); biginteger res = pom1.xor(pom2); string s = res.tostring(16); return s; } public void setciphertext() { //textfield3.settext(setxortext(textfield1, textfield2)); (int = 0; < 8; i++) { ciphertextarray[i].settext(setxortext(arrayiv, plaintextarray, i)); } } public textfieldevent() { setjpanel(arrayiv, panel1, "1a"); setjpanel(plaintextarray, panel2, "1b"); setjpanel(ciphertextarray, panel3, "1"); frame.add(panel1, borderlayout.north); frame.add(panel2, borderlayout.center); frame.add(panel3, borderlayout.south); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.pack(); frame.setvisible(true); frame.setsize(675, 275); frame.setresizable(false); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new textfieldevent(); } }); } }
stacktrace
exception in thread "awt-eventqueue-0" java.lang.illegalstateexception: attempt mutate in notification @ javax.swing.text.abstractdocument.writelock(abstractdocument.java:1338) @ javax.swing.text.abstractdocument.replace(abstractdocument.java:659) @ javax.swing.text.jtextcomponent.settext(jtextcomponent.java:1718) @ opa.beta1.textfieldevent.setciphertext(textfieldevent.java:82) @ opa.beta1.textfieldevent$1.removeupdate(textfieldevent.java:33)
this line
setjpanel(ciphertextarray, panel3, "1");
also adds documentlistener
every element of ciphertextarray
textfield
. every time value of 1 of cipher text textfield
s changes, method setciphertext()
invoked.
this leads following sequence:
- you enter new value in 1 of
textfield
s ofarrayiv
orplaintextarray
setciphertext()
invokedsetciphertext()
changes value of cipher texttextfield
setciphertext()
again invoked (explanation above). go 2.
thats why illegalstateexception
.
so solution not add documentlistener
textfields
in ciphertextarray
.
solution implemented follows:
public void setjpanel(jtextfield array[], jpanel container, string s, boolean doclistener) { (int = 0; < 8; i++) { array[i] = new jtextfield(s, 4); if (doclistener) { array[i].getdocument().adddocumentlistener(new documentlistener() { @override public void insertupdate(documentevent de) { setciphertext(); } @override public void removeupdate(documentevent de) { setciphertext(); } @override public void changedupdate(documentevent de) { setciphertext(); } }); } container.add(array[i]); } } [...] setjpanel(arrayiv, panel1, "1a", true); setjpanel(plaintextarray, panel2, "1b", true); setjpanel(ciphertextarray, panel3, "1", false);
Comments
Post a Comment