Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

creating a custom command prompt with java gui  RSS feed

 
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?
 
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Rancher
Posts: 2990
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple approach:



More complex approach:

Protected Text Component
 
Rob Spoor
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Rob. You just taught me something new
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!