Nathan Kreider
Nathan Kreider

Reputation: 516

How to listen to JTextArea

I'm creating a personal Java terminal, and I need it to do a couple of things that I'm not quite understanding right now. I need the program to listen to what is being input into the JTextArea, while also knowing that the program will always display

[USERNAME]@[OPERATING SYSTEM]:~$

when "Enter" has been hit. And I also need the program to set the part as said above, to be uneditable, and for allowance of character input to be set after the permanent declaration. If anyone is able to help me with this, my program is below, then there is the code for the listener, which will most likely be needing a lot of editing.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;

public class testGUI extends JFrame {
      boolean _active = true;
     String _username = System.getProperty("user.name").toLowerCase();
     String _os = System.getProperty("os.name").trim().toLowerCase();
     JTextArea _commandLine = new JTextArea(_username + "@" + _os + ":~$ ");

    public testGUI() {
        super("Java Terminal");

        setSize(800,600);
        setLocation(100,100);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GUISetup();

        setVisible(true);
    }

    public void GUISetup() {
        add(_commandLine);
        _commandLine.addActionListener(new CommandLineListener());
    }


        public static void main(String[] args) {
        new testGUI();
    }
}   

The listener code is below.

        try {
        while(_active) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print(_username + "@" + _os + ":~$ ");
            String command = br.readLine();
                if(command.equals("cf")) {
                    new commandCreateFile();
                } else if(command.equals("cof")) {
                    new commandCompile();
                } else if(command.equals("help")) {
                    System.out.println("Commands");
                    System.out.println(" cf              - Creates .java files, does not compile.");
                    System.out.println(" ccf             - Creates .java files, compiles on creation.");
                    System.out.println(" help            - Shows help documentation.");
                } else if(command.equals("exit")) {
                    System.out.print("Are you sure you want to exit? (Y/N) ");
                    String exit = br.readLine();
                    if(exit.equalsIgnoreCase("y")) {
                    System.exit(0);
                    }
                } else if(command.isEmpty()) {
                    // do nothing.
                } else {
                    System.out.println("\"" + command + "\" does not exist. Please review the \"help\" menu");
                }
        }
    } catch(Exception ex) {
        System.out.println("There was a problem: " + ex);
    }

Upvotes: 2

Views: 471

Answers (1)

StanislavL
StanislavL

Reputation: 57381

Use DocumentListener attached to the JTextArea's Document and a DocumentFilter attached to the Document to check which edit is allowed.

Upvotes: 3

Related Questions