Abu Dhabi
Abu Dhabi

Reputation: 331

Using JTextArea to simulate a text console

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.

Am I going about this correctly? Is there a better method for this?

I enclose the pertinent parts of my 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 for any help.

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:

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 {

    }

}

Upvotes: 2

Views: 8537

Answers (3)

bmoran
bmoran

Reputation: 1529

I'm using a text area as a console in an app I built which will sign a jar file. JTextArea has an append() method.

Jar Signer

JTextArea console=new JTextArea();
console.append("Insert console text here \n") \\  \n for new line

Upvotes: 0

Russ Hayward
Russ Hayward

Reputation: 5667

There seems to be lots of different ways to customise Swing text components. When I did something similar to you I had success with a custom Document:

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class CustomDocument extends PlainDocument {
    @Override
    public void insertString(int offset, String string, AttributeSet attributeSet) throws BadLocationException {
        // Parse input - in this case convert everything to upper case
        string = string.toUpperCase();
        super.insertString(offset, string, attributeSet);
    }
}

Here is a main method to be able to test the code:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 300, 300);
    frame.add(new JTextArea(new CustomDocument()));
    frame.setVisible(true);
}

Upvotes: 0

camickr
camickr

Reputation: 324098

I want to intercept input, and act on it

Then you should probably be using a DocumentFilter. See Implementing a Document Filter for more information.

Upvotes: 1

Related Questions