🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #633

Re: JTextPane KeyEvent

#633
From: 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
Technical Details
Message-ID: <xn0e0dhxr4ianxy00l@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <424a7eeb$1@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: "John McGrath  [TeamB]" <ng-b@JPMcGrath.net>
Subject: Re: JTextPane KeyEvent
Newsgroups: borland.public.jbuilder.java.swing
References: <424a7eeb$1@newsgroups.borland.com>
User-Agent: XanaNews/1.17.3.1
Message-ID: <xn0e0dhxr4ianxy00l@newsgroups.borland.com>
X-Ref: newsgroups.borland.com ~XNS:00000337
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
NNTP-Posting-Host: 141.158.33.210
Date: 30 Mar 2005 15:33:27 -0800
X-Trace: newsgroups.borland.com 1112225607 141.158.33.210 (30 Mar 2005 15:33:27 -0800)
Lines: 45
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail