• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane + KeyListener (Double Character problem); 70 line program  RSS feed

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is producing double values of everything. If you type in "a", "aa" is added to the JTextPane. I have a KeyListener on the JTextPane.
Should I use a Document Listener/Filter on it? Or an InputMap?

Thanks,
cc11rocks aka John Price
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because both the text area and your panel are being notified of the key event. They both react by inserting the character. Why are you reacting to this event when it's already handled by the text area? And if you're extending KeyAdapter, why are you overriding the default no-op methods with, well... no-op implementations? Just leave the methods out of your code and override only keyTyped().
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because both the text area and your panel are being notified of the key event. They both react by inserting the character

How do I override or take away the default key event from the JTextPane?
I want to keep things like left and right arrow, though.
Thanks,
cc11rocks aka John Price

EDIT: I also tried this:

Didn't work
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have suspected removing the InputMethodListeners and the KeyListeners would have taken care of it. Instead, maybe you can extend JTextArea and override the getInputMethodRequests() method to return null. I'm not sure if this will work, and even if it does, it is a dirty hack. Why do you want to handle the input yourself anyway?
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I want to add those font types. This is the only way I am familiar with...If you could suggest a better way, then please tell me.
Thanks,
John Price aka cc11rocks
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the methods declared in StyledDocument.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was having issues. The solution was to directly place it to the JTextPane.

For example:

Thanks,
cc11rocks aka John Price
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john price wrote:Should I use a Document Listener/Filter on it?

To prevent text from being inserted into a Document, or to modify it before adding, DocumentFilter is definitely the way to go. Note that this works with Strings, not chars, so you will need to check all individual characters of the String. After all, if you paste a piece of text that will trigger one insertion for the entire String, not one for each individual char.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using a text component. So anything you type will automatically be inserted into the text component.

Then you add a KeyListener which adds the character a second time to the text component.

I'm really not sure what you are trying to accomplish.

You don't want to use a KeyListener. What happens if somebody "pastes" text into the text component? A "key typed" event is not generated so nothing will happen.

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stopped using the KeyListener a long time ago. Here is my updated code, so you can see what I am doing:
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stopped using the KeyListener a long time ago.


So I've been wasting my time looking at old code? I guess I'm finished with this question so I don't waste any more time.

Also, you are still using a "null layout" even though you have been advised to use layout managers. There is absolutely no reason to do this. This is a simple layout that is easily handled by layout manager with simpler easy to understand code. Another reason to pass on this question.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. Anyway, I figured out a solution:

Thanks for helping,
John Price aka cc11rocks

EDIT: Updating method as I go along.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!