Article View: borland.public.jbuilder.java.swing
Article #631Re: JTextPane KeyEvent
From: Aven
Date: Thu, 31 Mar 2005 00:24
Date: Thu, 31 Mar 2005 00:24
60 lines
3261 bytes
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
Technical Details
Message-ID:
<424b4319$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<424a7eeb$1@newsgroups.borland.com> <xn0e0dhxr4ianxy00l@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,128b4a821bddbb34 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Aven" <aven_02@yahoo.com> Newsgroups: borland.public.jbuilder.java.swing References: <424a7eeb$1@newsgroups.borland.com> <xn0e0dhxr4ianxy00l@newsgroups.borland.com> Subject: Re: JTextPane KeyEvent Date: Wed, 30 Mar 2005 16:24:06 -0800 Lines: 62 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 128.111.68.241 Message-ID: <424b4319$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1112228633 128.111.68.241 (30 Mar 2005 16:23:53 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail