• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating a custom command prompt with java gui  RSS feed

 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there.

im just wondering if this is possible.

like creating your own cmd for configuration for my app.

Procedures
• create a frame with a textarea on it.
• put the output stream to the text area
• text area accepts input but the prompt couldn't be deleted.

  C:\>                   cls
(undeletable) (deleteable)

• works really just like the CMD.

is it possible to make it with java gui?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirement to mark parts of your text area content as read-only can be done using DocumentFilter. In short:
- get the Document from your JTextArea
- cast it to AbstractDocument (this cast is safe unless the JTextArea has a custom document)
- create a custom DocumentFilter subclass and instantiate it (perhaps an anonymous inner class will do)
- call setDocumentFilter on the document

The custom DocumentFilter is where the hard work is done. Override insertString to make sure you can't insert anything before your current prompt position. Override remove to make sure you can't remove anything before your current prompt position. Override replace to make sure you can't replace anything before your current prompt position.

That leaves one issue: the caret position. You don't want to have your cursor to be positioned before your current prompt position. Perhaps you can use a CaretListener for that. But resetting the caret position to the end (or at least after your current prompt position) may cause text highlighting (for copying data) to not work. These are things you need to consider.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple approach:



More complex approach:

Protected Text Component
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Rob. You just taught me something new
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!