wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Tab Completion? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tab Completion?" Watch "Tab Completion?" New topic
Author

Tab Completion?

Lokesh Poovaragan
Greenhorn

Joined: Aug 26, 2011
Posts: 13
I have a very peculiar and weird problem. I am trying to build a simple Editor basically for Java Only.
I have a very simple swing program right now, A simple JFrame, within that, a simple JPanel, within that a simple JTextArea.

Now when the user types something, I want to keep monitoring that, and when he wants to type "public" if he begins typing "pu" then if he presses tab, it should automatically type the rest of the word for him, this is the type of feature that is there in most consoles. Like Command Prompt, Bash, SSH etc.

Words that I want to monitor will be limited, mostly only about 50 words, Maximum, I just want to have some starting help, I tried googling it, but it didnt really help.
Also, when a tab causes a clash between two or more words, such as:

He wants to type public, so he starts with "p" and tabs it, but the list of words has "public" and "private", so now there is a clash, so when there is a clash like this, I think I'll just let the editor fill in for public, but if he presses tab again, it should change "public" to "private" and then keep alternating within the two, until the user presses Space bar on the word he wants, that is, "public"

I hope my question was clear enough, If not please let me know and I'll make it as clear as possible.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
What you describe is accomplished by a listener. A listener is just a small object whose only job is to monitor the state of another object and react to events. You can add a listener to any GUI component. When the component's state changes, it sends a notification to its listeners. There are many, many kinds of listeners that listen for specific kinds of events. The one you want is KeyListener: it listens to events that come from the keyboard. Listeners are typically interfaces which you have to implement. In this case there is a method called keyPressed where you will want to put your logic. (Often we only care about one particular method of a listener, even though its interface has others that must be implemented -- we just create an "empty" implementation for them. You'll have to call the text area's addKeyListener method to register your listener; then you'll be notified every time the user presses a key. You'll have to check whether the tab or space bar was pressed -- this information will be in the KeyEvent that's passed into the listener; you'll also have to get the text that was entered in the JTextArea and evaluate it. It will be up to you to modify the text in the JTextArea if & when it's appropriate.

Hope that's enough to get you started. Let us know if any of that is unclear. This is a great project for learning event handling.
Lokesh Poovaragan
Greenhorn

Joined: Aug 26, 2011
Posts: 13
Yup that did the trick, turns out, I was already using an Event Handler in my Menu selection. All I had to know was what the Handler for KeyEvents was. I made a class implementing the KeyListener class and I got it to work, After some level of tweaking, I have got upto the point where, If I press Tab or Space, I have a visual output using System.out.println() that confirms that I just pressed A Tab or a Space. Next thing from here is to build the completion, which I think I would need help with. I can figure out the most part, but how do I make sure that when A Tab is pressed an actual tab isnt output on the TextArea? Like, I pressed Tab right now on the editor, I just wanted it to do tab completion, but instead, A real visual 8 spaces was made on the TextArea. I was initially thinking, everytime I press a tab, let me just throw out 8 backspaces to clear whatever space the Tab created, but if I did that, then I would never be able to use tab even for Indentation. So I want help with how when I press tab with the letters "pu" alredy typed, it shouldnt move across 8 spaces, but instead should just complete the rest of the word, "public".

I hope this question made sense, If not please let me know, i'll make sure I expand it further
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tab Completion?
 
Similar Threads
Culture Clash
Clash of Civilizations!!