Make parts of a JTextArea non editable (not the whole JTextArea!)

前端 未结 5 1589
甜味超标
甜味超标 2020-12-16 18:42

I\'m currently working on a console window in Swing. It\'s based on a JTextArea and works like a common command line. You type a command in one line and press enter. In the

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 19:14

    You do not need to create your own component.

    This can be done (as in I have done it) using a custom DocumentFilter.

    You can get the document from textPane.getDocument() and set a filter on it by document.setFilter(). Within the filter, you can check the prompt position, and only allow modifications if the position is after the prompt.

    For example:

    private class Filter extends DocumentFilter {
        public void insertString(final FilterBypass fb, final int offset, final String string, final AttributeSet attr)
                throws BadLocationException {
            if (offset >= promptPosition) {
                super.insertString(fb, offset, string, attr);
            }
        }
    
        public void remove(final FilterBypass fb, final int offset, final int length) throws BadLocationException {
            if (offset >= promptPosition) {
                super.remove(fb, offset, length);
            }
        }
    
        public void replace(final FilterBypass fb, final int offset, final int length, final String text, final AttributeSet attrs)
                throws BadLocationException {
            if (offset >= promptPosition) {
                super.replace(fb, offset, length, text, attrs);
            }
        }
    }
    

    However, this prevents you from programmatically inserting content into the output (noneditable) section of the terminal. What you can do instead is either a passthrough flag on your filter that you set when you're about to add the output, or (what I did) set the document filter to null before appending the output, and then reset it when you're done.

提交回复
热议问题