I am creating a cmd like command prompt using swing.Now I have created a JTextArea and set the prompt text and added that text area inside a JFrame.Now what I want is that when I press enter key on the next line prompt text should be written first then cursor should be placed after the last character of prompt.If I'm unable to explain the requirement please consider the behavior of windows command prompt.What I have planned to do is to write a key listener and implement key pressed event.Now if the pressed key is VK_ENTER then i'm trying to do setText("promptText").However this approach not working.In my last thread I mentioned this requirement previous post but didn't get much from there.Sorry for reposting but I need some help on this.
You will have to modify JTextArea behavior heavily to have it behave as a console.
I'd suggest to consider a different approach: create a form with an input panel and an output panel. User types his command into the input panel, using the JTextArea standard behavior. On the enter key, this input is appended to the output panel (if requested), then it is processed and the output is appended to the output panel as well. The output panel therefore always looks like the command prompt (or console) window. It can be another JTextArea which would not permit user modifications.
This would be much, much easier to implement. Several applications already use this design, for example SQL Developer. I personally don't see any advantage of emulating a console in Swing. Consoles work the way they do since they were designed within the constraints of text terminals. Since Swing is not a text terminal, you don't need to design to the same constraints.
posted 8 years ago
Thanks Martin for your reply.Now if I have understood correctly you are telling to keep to separate JTextArea for input and output.Now like I said I want to emulate the behavior of windows command prompt or linux terminals won't it be too difficult.Cause some commands also ask user to enter some inputs.And I have to dynamically add input and output JTextAreas depending upon the input passed by the user.For example if user hits enter key without giving any commands simply the prompt should be printed on the next line same as Windows cmd.But if user gives some command the output of that command will be printed followed by prompt text.What may be the best approach ?
You say you want to emulate Windows command prompt or Linux terminals, but even this is quite vague specification -- there are certainly some differences between Linux and Windows consoles. A lot of things would have to be taken care of if you wanted to use JTextArea and have it behave exactly as a Windows command prompt, for example:
- There is no word wrap in console, lines wrap at the console width. If the console is resized, the text is not re-wrapped.
- You can only edit the last line ("prompt") in the console, the rest of the console is uneditable and cursor cannot go there (this is different from some Unix consoles, I think). Nevertheless, you can mark a text in the console (in strictly rectangular areas) and copy it to the clipboard.
- Various command completions (when Tab is pressed, for example) are available.
- Console keeps certain number of lines of text which you can scroll and inspect, when this number is reached, old lines are being scrolled away.
- You can use Alt-Enter (in Windows) to switch to text mode. Completely impossible to do with Swing.
There might be other differences I didn't think of.
I don't know your exact requirements or needs, so I don't know how important it is to handle the Enter key without any commands, but surely in the design I've suggested you could easily detect this and add the prompt to the output panel. If a program being run in your console requires user input, several possibilities exist: you might display an input box to let the user enter the input (SQL Developer does this I believe), or you might reactivate the input panel and let the user enter the input here. In both cases you would probably copy the user's input into the output panel. Output of any program would naturally go into the output panel (how to do this is a different matter not exactly specific to Swing).
My point is: you are going to use Swing. Use Swing strengths in your design, don't try to have the Swing form behave as a console. It is certainly possible (with some minor limitations, eg. Alt-Enter), but I think it is simply not worth the effort.
If you decide to implement the console, look around for existing implementations. Googling up java swing console brings a few interesting links. There is this component which seems not to be based on the JTextArea control, and several threads on various forums discussing JTextArea console implementations.
Ah, I've only now seen that the ProtectedTextComponent you've been advised to use in your other thread actually addresses lots of your concerns. That is probably a viable way to go, but as Michael said, it's up to you now.
If you're gonna buy things, buy this thing and I get a fat kickback:
Devious Experiments for a Truly Passive Greenhouse!