java - trouble passing input from one JTextArea into another JTextArea -
my issue lies in documentlister arealistener. can't seem figure out how pass in text user enters 1 of jtextareas conversions , return other jtextarea.
the goal of program take in user entered roman or arabic number in 1 of fields, convert , return value of conversion other field in real time.
my gui , conversion methods work, can't seem wrap head around obtaining string user , printing in real time.
public class arabictoromangui_hard extends jframe { private static final long serialversionuid = 1l; jpanel panel = new jpanel(); //constructor add text fields frame public arabictoromangui_hard() { jtextarea left = new jtextarea(10, 20); jtextarea right = new jtextarea(10, 20); setlayout(new gridlayout(1, 2)); add(new jscrollpane(left)); add(new jscrollpane(right)); mirrordocument leftdoc = new mirrordocument(); mirrordocument rightdoc = new mirrordocument(); left.setdocument(leftdoc); right.setdocument(rightdoc); leftdoc.adddocumentlistener(new documenthandler(rightdoc)); rightdoc.adddocumentlistener(new documenthandler(leftdoc)); leftdoc.getdocument().adddocumentlistener(arealistener); rightdoc.getdocument().adddocumentlistener(arealistener); } documentlistener listener = new documentlistener() public class arealistener implements documentlistener { //documentlistener listener = new documentlistener() @override public void changedupdate(documentevent e) { convertinput(); } @override public void insertupdate(documentevent e) { convertinput(); } @override public void removeupdate(documentevent e) { convertinput(); } private void convertinput(documentevent e) { boolean arabicentered = false; boolean romanentered = false; (char ch : userinputtedtext.tochararray()) { if(character.isletter(ch)) { romanentered = true; } if(character.isdigit(ch)) { arabicentered = true; } } if(romanentered = true) { if(conversionlogic_hard.getcheckfail() == false) { conversionlogic_hard.convertfromromantoarabic(userinputtedtext); //converts string of romannumerals arabic int string arabicnumberasstring = conversionlogic_hard.getconvertedromannumeral(); //converts number int string } } if(arabicentered == true) { if(conversionlogic_hard.getcheckfail() == false) { conversionlogic_hard.convertfromarabictoroman(userinputtedtext); //converts string arabicnumberal string roman numberal string romannumberalasstring = conversionlogic_hard.getconvertedromannumeral(); //gets romannumberal string } } }//end convertinput }//end arealistener //creates flag test state of textarea public class mirrordocument extends plaindocument { private boolean ignoreupdatedtext; public void setignoreupdates(boolean ignoreupdatestext) { this.ignoreupdatedtext = ignoreupdatestext; } public boolean isignoreupdates() { return ignoreupdatedtext; } } //when event occurs checks ignoreupdatedtext flag of document check if false. //then sets flag in checkdocument true prevent document listener processing new events. //then updates checkdocument. public static class documenthandler implements documentlistener { private mirrordocument checkdocument; private boolean ignoreupdatedtext = false; private jtextarea leftdoc, rightdoc; boolean arabicentered = false; boolean romanentered = false; public documenthandler(mirrordocument checkdocument) { this.checkdocument = checkdocument; } @override public void removeupdate(documentevent e) { document doc = e.getdocument(); if (doc instanceof mirrordocument) { mirrordocument mirrordoc = (mirrordocument) doc; if (!mirrordoc.isignoreupdates()) { try { checkdocument.setignoreupdates(true); checkdocument.remove(e.getoffset(), e.getlength()); } catch (badlocationexception exc) { exc.printstacktrace(); } { checkdocument.setignoreupdates(false); } } } }//end removeupdate @override public void changedupdate(documentevent e) { //not used } @override public void insertupdate(documentevent e) { document doc = e.getdocument(); if (doc instanceof mirrordocument) { mirrordocument mirrordoc = (mirrordocument) doc; if( !mirrordoc.isignoreupdates()) { try { string textinput = e.getdocument().gettext(e.getoffset(), e.getlength()); checkdocument.setignoreupdates(true); checkdocument.insertstring(e.getoffset(), textinput, null); } catch(badlocationexception exc) { exc.printstacktrace(); } { checkdocument.setignoreupdates(false); } } } }//end insertupdate } }//class
you can use documentfilter
listen changes.
when create documentfilter
can specify text field updated converted text.
using approach can remove documentfilter
text field before set text avoid recursion of 2 text fields trying update 1 another.
the filter might like:
import javax.swing.*; import javax.swing.text.*; import java.awt.toolkit; public class conversionfilter extends documentfilter { private boolean arabic; private jtextfield converted; public conversionfilter(boolean arabic, jtextfield converted) { this.arabic = arabic; this.converted = converted; } @override public void insertstring(filterbypass fb, int offs, string str, attributeset a) throws badlocationexception { super.insertstring(fb, offs, str, a); convertinput(fb); } @override public void replace(final filterbypass fb, final int offs, final int length, final string str, final attributeset a) throws badlocationexception { super.replace(fb, offs, length, str, a); convertinput(fb); } @override public void remove(documentfilter.filterbypass fb, int offset, int length) throws badlocationexception { super.remove(fb, offset, length); convertinput(fb); } private void convertinput(documentfilter.filterbypass fb) { // remove documentfilter text field converted abstractdocument document = (abstractdocument)converted.getdocument(); documentfilter df = document.getdocumentfilter(); document.setdocumentfilter( null ); // conversion , update text field string text = fb.getdocument().gettext(); string convertedtext = arabic ? converttoroman(text) : converttoarabic(text); converted.settext( convertedtext ); // restore documentfilter on converted text field document.setdocumentfilter( df ); } }
then use filter code might like:
jtextfield arabictextfield = new jtextfield(...); jtextfield romantextfield = new jtextfield(...); abstractdocument arabicdocument = (abstractdocument)arabictextfield.getdocument(); arabicdocument.setdocumentfilter( new conversonfilter(true, romantextfield) ); abstractdocument romandocument = (abstractdocument)romantextfield.getdocument(); romandocument.setdocumentfilter( new conversonfilter(false, arabictextfield) );
i used documentfilter instead of documentlistener notified of changes document because of getter/setter method documentfilter make easy remove , restore filter.
Comments
Post a Comment