• Post Reply Bookmark Topic Watch Topic
  • New Topic

register an event for a TextArea every time user presses space bar

 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friens,

i'm developing a text manipulation project. here i am supposed to read words from a text box. I wish to perform some aaction every time user presses space bar(i.e., spearation between two words). i want to register an event for the textArea only at the pressing of space bar!!
please Help!!

sincerely
RohitRusty314
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried anything yet? Is there a specific question, or do you just want someone to code for you?

Be sure, also, you know the difference between "registering an event" and "responding to an event". You say you want to register an event each time the space bar is pressed; I doubt that. I think what you want to do is execute some code of yours when the space bar is pressed; that would be "responding"; you would "register" a routine to be executed when the space bar is pressed; that would be done once, and then the routine would execute each time the key is pressed.

rc
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be possible with key bindings, but you then need to ensure that the default behaviour (adding the actual space) is also executed. It's probably easier to use a simple KeyListener and filter out any character that isn't a space.
 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:Have you tried anything yet? Is there a specific question, or do you just want someone to code for you?

Be sure, also, you know the difference between "registering an event" and "responding to an event". You say you want to register an event each time the space bar is pressed; I doubt that. I think what you want to do is execute some code of yours when the space bar is pressed; that would be "responding"; you would "register" a routine to be executed when the space bar is pressed; that would be done once, and then the routine would execute each time the key is pressed.

rc


i 'm trying to code b myself. the problems that i am facing right now is, first of all for following declarations

JTextArea txtInput = new JTextArea(15,1);
txtInput.TextListener(new TextInputListener());
this is giving the error:
cannot find symbol.

and secondly i want my code to respond to do a specific task each time space bar is pressed!!!
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if you are attempting a task that is too advanced for you.

I cannot tell what the error message refers to; you don't say. Messages normally indicate what symbol they cannot find.

I cannot tell whether you are just capitalizing carelessly in what you write in your questions, or if you really are attempting to invoke a "TextListener" method on an instance of JTextArea. There are no methods on JTextArea that start with a capital letter.

If you use "new TextInputListener()" when you add a text listener to your text area, I do not see how you are ever going to provide the code you want to run when it is invoked. You need a text listener that has the code for this 'specific task' you want executed when the space bar is pressed.

The only way I know of to answer your question is write a program that does what I think you are asking about. I'm not inclined to do that, having other programming that I'd rather do, especially since I'm having to guess a bit about just what you want. I am willing to answer questions if you have them, but they need to be more specific and provide me the information I need to try to help you. Just doing it for you does not interest me.

rc
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KeyBinding is definitely the way to go.

you just tie the keystroke you want to the component's inputmap (note: there is more than 1 inputMap).
you create the Action you want to happen, then tie the inputMap/action to the component's actionMap.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to register an event for the textArea only at the pressing of space bar!!


What if somebody pastes text into the text area? For example "some pasted text", which also contains 2 spaces.

You should be using a DocumentListener. The listener will notify you every time the Document is updated. Or if you want to intercept the text before it is added to the Document you can use a DocumentFilter. In both cases:

a) If the text is a single character you test for a space.

b) If multiple characters you parse the string looking for spaces.
 
Randall Twede
Ranch Hand
Posts: 4519
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was going to suggest using a KeyListener as Rob Spoor suggested, but as Rob Camick observed, what if someone pastes. KeyBinding is something i will have to check out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!