🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
6 messages
6 total messages Started by Aven Wed, 30 Mar 2005 10:28
JTextPane KeyEvent
#632
Author: Aven
Date: Wed, 30 Mar 2005 10:28
13 lines
1341 bytes
Hi,
I add a keylistener to JTextPane to process special keypressed events, i.e., 
set the caret position
to the end of the document.
However, this *hides* the default actions, e.g., when I typed CTRL-C, the 
key event was
consumed by my keylistener, it never copied the selected content.
How can I do that?
BTW, how can I associate copy, paste etc to menu items?

Thank you!

Aven 
Re: JTextPane KeyEvent
#633
Author: John McGrath [TeamB]
Date: Wed, 30 Mar 2005 23:33
45 lines
2626 bytes
On 3/30/2005 at 5:28:17 AM, Aven wrote:

> I add a keylistener to JTextPane to process special keypressed events,
> i.e., set the caret position to the end of the document.  However, this
> hides the default actions, e.g., when I typed CTRL-C, the key event was
> consumed by my keylistener, it never copied the selected content.
> How can I do that?

With all Swing components, when you want to bind a keystroke to an action,
it is better to use the Swing key binding facility rather than using a
KeyListener.

With the key binding facility, you define Actions that can be performed
and then you bind keystokes to those actions.  This makes things more
flexible, since the bindings can be easily changed without messing with
the actions.  And in this case, the action you need is already defined, so
you just need to bind the keystroke.

   InputMap inputMap = textPane.getInputMap( JComponent.WHEN_FOCUSED );
   inputMap.put(
      KeyStroke.getKeyStroke( KeyEvent.VK_C, InputEvent.CTRL_MASK ),
      "caret-end" );

For more information on key bindings, see:

http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.htm
l

> BTW, how can I associate copy, paste etc to menu items?

You could get the required actions from the JTextArea's ActionMap and use
them to create the toolbar buttons.  For example:

   ActionMap map = textPane.getActionMap();
   toolBar.add( map.get( "copy-to-clipboard" ) );
   toolBar.add( map.get( "paste-from-clipboard" ) );

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: JTextPane KeyEvent
#631
Author: Aven
Date: Thu, 31 Mar 2005 00:24
60 lines
3261 bytes
Thanks John.
The problem arises as, my KeyListener only cares about normal characters,
not action keys. On KeyPressed events, it sets the caret position to the end
of document before the characters are entered into JTextPane.
On KeyTyped events, it performs other actions on the characters.

If I used key binding, should I bind every normal key to the action?
It seems incovenient though.


Regards,
Aven

"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0e0dhxr4ianxy00l@newsgroups.borland.com...
> On 3/30/2005 at 5:28:17 AM, Aven wrote:
>
>> I add a keylistener to JTextPane to process special keypressed events,
>> i.e., set the caret position to the end of the document.  However, this
>> hides the default actions, e.g., when I typed CTRL-C, the key event was
>> consumed by my keylistener, it never copied the selected content.
>> How can I do that?
>
> With all Swing components, when you want to bind a keystroke to an action,
> it is better to use the Swing key binding facility rather than using a
> KeyListener.
>
> With the key binding facility, you define Actions that can be performed
> and then you bind keystokes to those actions.  This makes things more
> flexible, since the bindings can be easily changed without messing with
> the actions.  And in this case, the action you need is already defined, so
> you just need to bind the keystroke.
>
>   InputMap inputMap = textPane.getInputMap( JComponent.WHEN_FOCUSED );
>   inputMap.put(
>      KeyStroke.getKeyStroke( KeyEvent.VK_C, InputEvent.CTRL_MASK ),
>      "caret-end" );
>
> For more information on key bindings, see:
>
> http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.htm
> l
>
>> BTW, how can I associate copy, paste etc to menu items?
>
> You could get the required actions from the JTextArea's ActionMap and use
> them to create the toolbar buttons.  For example:
>
>   ActionMap map = textPane.getActionMap();
>   toolBar.add( map.get( "copy-to-clipboard" ) );
>   toolBar.add( map.get( "paste-from-clipboard" ) );
>
> -- 
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>    http://www.JPMcGrath.net/newsgroups/e-mail.html 
Re: JTextPane KeyEvent
#635
Author: John McGrath [TeamB]
Date: Thu, 31 Mar 2005 05:59
22 lines
1743 bytes
On 3/30/2005 at 7:24:06 PM, Aven wrote:

> The problem arises as, my KeyListener only cares about normal characters,
> not action keys. On KeyPressed events, it sets the caret position to the
> end of document before the characters are entered into JTextPane.
> On KeyTyped events, it performs other actions on the characters.
> 
> If I used key binding, should I bind every normal key to the action?
> It seems incovenient though.

I am not sure what you are trying to do.  Are you trying to ensure that
characters inserted are always inserted at the end of the buffer?  If that
is it, somehow I missed where you said that.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Re: JTextPane KeyEvent
#639
Author: Aven
Date: Thu, 31 Mar 2005 23:02
35 lines
2401 bytes
Yes, I want to ensure that characters inserted are always inserted at
the end of the document. Other behaviors are same as those of a normal
JTextPane, i.e., I still want to reserve all Ctrl-C, Ctrl-V, ..., actions.
How can I do that?

My key listener somehow *consumes* the key event, so it prevents
further processsing of the key event.

Regards,
Aven

"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message 
news:xn0e0e4pb4w2zzt00s@newsgroups.borland.com...
> On 3/30/2005 at 7:24:06 PM, Aven wrote:
>
>> The problem arises as, my KeyListener only cares about normal characters,
>> not action keys. On KeyPressed events, it sets the caret position to the
>> end of document before the characters are entered into JTextPane.
>> On KeyTyped events, it performs other actions on the characters.
>>
>> If I used key binding, should I bind every normal key to the action?
>> It seems incovenient though.
>
> I am not sure what you are trying to do.  Are you trying to ensure that
> characters inserted are always inserted at the end of the buffer?  If that
> is it, somehow I missed where you said that.
>
> -- 
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
>    http://www.JPMcGrath.net/newsgroups/e-mail.html 
Re: JTextPane KeyEvent
#640
Author: John McGrath [TeamB]
Date: Fri, 01 Apr 2005 01:19
33 lines
2537 bytes
On 3/31/2005 at 6:02:44 PM, Aven wrote:

> Yes, I want to ensure that characters inserted are always inserted at
> the end of the document. Other behaviors are same as those of a normal
> JTextPane, i.e., I still want to reserve all Ctrl-C, Ctrl-V, ...,
> actions.  How can I do that?

I do not think you have specified the desired behavior in the detail that
you need to.  Should the characters inserted with a paste also get
inserted at the end?  How does "cut" work?  Does it delete text from the
middle of the buffer, or should this be disabled?  What should happen with
the caret when some text is inserted?  Does it stay where it was or does
it move to the end.  Should cursor keys still move the caret?

The answer to these questions will have a significant effect on how this
might be implemented.  For example, if you do not need caret movement,
then you could try forcing the caret to stay at the end of the text.

> My key listener somehow consumes the key event, so it prevents
> further processsing of the key event.

I can think of no reason this would happen, unless you explicitly call
consume() on the event.  Of course, I have not seen what your key event
handled is doing, so it is impossible to know what it is doing.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads