title: Using JTextArea to simulate a text console
body: <p>My objective here is to obtain a console-like-behaving component in Java, not necessarily in JTextArea, but this seemed like a logical thing to try first. Output is simple enough, using the methods provided by the JTextArea, but input is another thing. I want to intercept input, and act on it - character by character. I've found some examples on using a DocumentListener for something vaguely related, but it doesn't seem to allow me to easily check what was just typed in, which is what I need to decide how to act upon it.</p>
  
  <p>Am I going about this correctly? Is there a better method for this?</p>
  
  <p>I enclose the pertinent parts of my application code. </p>
  
  <pre><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);
      }
  }
  </code></pre>
  
  <p>Thanks for any help.</p>
  
  <p>EDIT1: I have attempted to do this using a JTextPane with a DocumentFilter, but when I input something, the method in the DocumentFilter isn't getting run. I enclose the modified code:</p>
  
  <pre><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 {
  
      }
  
  }
  </code></pre>