How to edit the next line of editable JTextArea (+ JScrollPane)


So im creating a server, and that works great, however I am a bit stuck on the GUI. You see, I would like it to look just like Command Prompt, where only the next line is editable, and it does not let you delete any other text. So right now I have:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

Then the frame stuff...

    f.setBounds(ss.width - 600, 50, 550, 350);
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//added window listener so closes socket connection first

Then adding it:


Finally, the method I use to output to the TextArea:

public static void append(String text) {
    jt.append("\n\n"+System.getProperty("")+" / "+getIp()+" > ");

Now I need to assign a String to what the user types into the JTextArea after they press enter:>?

    public void ActioEvent(ActionEvent e){
        String text = JTextArea.getLines().getLastLine().getText().replace(System.getProperty("")+" / "+getIp()+"       > ", "");

Maybe something like that? Then I need to have it so that only the part after the ">" is editable?

The way to do this is with a DocumentFilter. This is a fairly obscure and little-used part of Java, and is far from easy to use. However it allows you to insert a DocumentFilter between the UI (where rich text content is edited) and the underlying model (the content). You pass all the 'insert' and 'remove' operations through the filter, which can either accept, refuse or modify them. You can code the filter to only permit modifications to the command line, and not to the prompt.

As I say, this is a pretty hard piece of coding, and the Document/DocumentFilter structure has a lot of complexity that your particular application doesn't need. However it does provide you with the facilities you need.

There is a tutorial in the standard Java doc pages, but not an advanced one, and very few examples that I know of are out there on the web.

ProtectedTextComponent (thanks camickr) provides an example of how to do something similar.