Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is my implemitation of a document filter acting as a console in java. However with some modifications to allow me to have a " command area" and a "log area", meaning the results from commands print in the log area and the actual command prints in the command area. The log area is just another Jtext area that is nonEditable. I found thisthread to be helpful, so mabey someone trying to achive something similar to this implementation can find some pointers!</p> <pre><code>class NonEditableLineDocumentFilter extends DocumentFilter { private static final String PROMPT = "Command&gt; "; @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String string,AttributeSet attr) throws BadLocationException { if(string == null) { return; } else { replace(fb, offset, 0, string, attr); } } @Override public void remove(DocumentFilter.FilterBypass fb, int offset,int length) throws BadLocationException { replace(fb, offset, length, "", null); } @Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length,String text, AttributeSet attrs) throws BadLocationException { Document doc = fb.getDocument(); Element root = doc.getDefaultRootElement(); int count = root.getElementCount(); int index = root.getElementIndex(offset); Element cur = root.getElement(index); int promptPosition = cur.getStartOffset()+PROMPT.length(); if(index==count-1 &amp;&amp; offset-promptPosition&gt;=0) { if(text.equals("\n")) { cmd = doc.getText(promptPosition, offset-promptPosition); if(cmd.trim().isEmpty()) { text = "\n"+PROMPT; } else { text = "\n" + PROMPT; } } fb.replace(offset, length, text, attrs); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload