• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating a custom command prompt with java gui

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple approach:



More complex approach:

Protected Text Component
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Rob. You just taught me something new
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic