java - Using JTextArea to simulate a text console -


my objective here obtain console-like-behaving component in java, not in jtextarea, seemed logical thing try first. output simple enough, using methods provided jtextarea, input thing. want intercept input, , act on - character character. i've found examples on using documentlistener vaguely related, doesn't seem allow me check typed in, need decide how act upon it.

am going correctly? there better method this?

i enclose pertinent parts of application code.

public class myframe extends jframe {     public myframe() {         dimension screensize=toolkit.getdefaulttoolkit().getscreensize();         dimension framesize=new dimension((int)(screensize.width/2),(int)(screensize.height/2));         int x=(int)(framesize.width/2);         int y=(int)(framesize.height/2);         setbounds(x,y,framesize.width,framesize.height);           console = new jtextarea("",25,80);         console.setlinewrap(true);         console.setfont(new font("monospaced",font.plain,15));         console.setbackground(color.black);         console.setforeground(color.light_gray);         console.getdocument().adddocumentlistener(new mydocumentlistener());          this.add(console);      }      jtextarea console;  }  class mydocumentlistener implements documentlistener {     public void insertupdate(documentevent e)     {         textchanged("inserted into");     }     public void removeupdate(documentevent e)     {         textchanged("removed from");     }     public void changedupdate(documentevent e)     {         textchanged("changed");     }     public void textchanged(string action)     {         system.out.println(action);     } } 

thanks help.

edit1: have attempted using jtextpane documentfilter, when input something, method in documentfilter isn't getting run. enclose modified code:

public class myframe extends jframe {     public myframe() {         dimension screensize=toolkit.getdefaulttoolkit().getscreensize();         dimension framesize=new dimension((int)(screensize.width/2),(int)(screensize.height/2));         int x=(int)(framesize.width/2);         int y=(int)(framesize.height/2);         setbounds(x,y,framesize.width,framesize.height);          console = new jtextpane();         //console.setlinewrap(true);         console.setfont(new font("monospaced",font.plain,15));         console.setbackground(color.black);         console.setforeground(color.light_gray);         styleddocument styleddoc = console.getstyleddocument();             if (styleddoc instanceof abstractdocument) {             doc = (abstractdocument)styleddoc;             doc.setdocumentfilter(new documentsizefilter());         }          this.add(console);      }      jtextpane console;     abstractdocument doc;  }  class documentsizefilter extends documentfilter {          public documentsizefilter() {      }      public void insertstring(filterbypass fb, int offs, string str, attributeset a) throws badlocationexception {         system.out.println(str);         if (str.equals("y")) {             system.out.println("you have pressed y.");         }     }      public void replace(filterbypass fb, int offs, int length, string str, attributeset a)  throws badlocationexception {      }  } 

i want intercept input, , act on it

then should using documentfilter. see implementing document filter more information.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -