Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set the cut/copy/paste keybindings ?

0 views
Skip to first unread message

Juancho

unread,
Sep 5, 2002, 12:55:55 PM9/5/02
to
Hi,

I'm using the Motif look and feel, but I'd like to keep the win32
cut/copy/paste keybindings.

Does anyone know any way to do this in JTextComponent ?

I've been trying to set them like:

c.getInputMap().put(KeyStroke.getKeyStroke('X',
java.awt.Event.CTRL_MASK), "cut");

But with no sucess.

On the other hand, I tried to copy them from the Win LnF:

LookAndFeel win = UIManager.getLookAndFeel();

...

UIManager.put("TextArea.keyBindings",
win.getDefaults().get("TextArea.keyBindings"));

Again, with no sucess.

Any solution would be really welcome.

Thanks in advance,

- Juancho

Linda Radecke

unread,
Sep 5, 2002, 2:41:15 PM9/5/02
to

Juancho wrote:

> Hi,

> I'm using the Motif look and feel, but I'd like to keep the win32
> cut/copy/paste keybindings.


> Does anyone know any way to do this in JTextComponent ?
>
> I've been trying to set them like:
>
> c.getInputMap().put(KeyStroke.getKeyStroke('X',
> java.awt.Event.CTRL_MASK), "cut");
>
> But with no sucess.


I have thought it would be the same for all three, via Ctrl+X, but
possibly it is Shift-X on Motif, I don't remember it right now. In
DefaultEditorKit cutAction is registered as "cut-to-clipboard",thus
IMO it should work if you change the InputMap then with this as the
actionMapKey in InputMap.put() and then with textComponent.cut();.

KeyStroke key = KeyStroke.getKeyStroke("ctrl X");

c.getInputMap().put(key, "cut-to-clipboard");

c.getActionMap().put("cut-to-clipboard", new AbstractAction() {

public void actionPerformed(ActionEvent ae) {
c.cut();
}
});

Or as you have had that via:

c.getInputMap()
.put(KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK),
"cut-to-clipboard");


for example - hope this helps


Linda
--
'. (v) .' I have all I want, is that simple enough? A whole lot
'( \.' more I'm thinking of every night about six o'clock
``\ birds come back to the pond to talk, they talk to me, it
feels like nothing matters, In our private universe *Crowded House

Christian Kaufhold

unread,
Sep 5, 2002, 2:45:42 PM9/5/02
to
Hello!

Juancho <jua...@people-com.com> wrote:

> I'm using the Motif look and feel, but I'd like to keep the win32
> cut/copy/paste keybindings.
>
> Does anyone know any way to do this in JTextComponent ?
>
> I've been trying to set them like:
>
> c.getInputMap().put(KeyStroke.getKeyStroke('X',
> java.awt.Event.CTRL_MASK), "cut");

^^^^^

Who said it was called like that?

Use DefaultEditorKit.cutAction (which is "cut-to-clipboard").


> LookAndFeel win = UIManager.getLookAndFeel();
^^^

?


>
> ...
>
> UIManager.put("TextArea.keyBindings",
> win.getDefaults().get("TextArea.keyBindings"));
>
> Again, with no sucess.

Too little context to say what it wrong.

Christian

Juancho

unread,
Sep 6, 2002, 12:00:18 PM9/6/02
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0t3d77a60...@simia.chka.de>...

> Who said it was called like that?
>
> Use DefaultEditorKit.cutAction (which is "cut-to-clipboard").

> > LookAndFeel win = UIManager.getLookAndFeel();


> > UIManager.put("TextArea.keyBindings",
> > win.getDefaults().get("TextArea.keyBindings"));
>

> Too little context to say what it wrong.

Whay I'm trying to do is:

When the application starts (so the standard windows look and feel
is selected), store it into a variable.

LookAndFeel win = UIManager.getLookAndFeel();

And them, after setting the motif look and feel, retrieve the old
key bindings.
Ex.

UIManager.put("TextArea.keyBindings", win.getDefaults().get
("TextArea.keyBindings"));

This -at least the way I'm doing it- doesn't work.


I've been browsing the help in the sun site, and I found how to set
normal key bindings, but no info about how to hook them to the usual
clipboard commands.

- Juancho

Christian Kaufhold

unread,
Sep 8, 2002, 3:50:18 PM9/8/02
to
Hello!

Juancho <jua...@people-com.com> wrote:

> When the application starts (so the standard windows look and feel
> is selected), store it into a variable.
>
> LookAndFeel win = UIManager.getLookAndFeel();

But why should it be called "win"?
You don't know which LookAndFeel is set then.


> And them, after setting the motif look and feel, retrieve the old
> key bindings.
> Ex.
>
> UIManager.put("TextArea.keyBindings", win.getDefaults().get
> ("TextArea.keyBindings"));
>
> This -at least the way I'm doing it- doesn't work.

It only works for JTextAreas create after that line.

Christian

Juancho

unread,
Sep 9, 2002, 3:47:31 AM9/9/02
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0t3d7ba9a...@simia.chka.de>...

> > LookAndFeel win = UIManager.getLookAndFeel();
> But why should it be called "win"?
> You don't know which LookAndFeel is set then.

Just the WINdows look and feel..

> > UIManager.put("TextArea.keyBindings", win.getDefaults().get
> > ("TextArea.keyBindings"));

> It only works for JTextAreas create after that line.

The problem is that it doesn't work at all..

- Juancho

Christian Kaufhold

unread,
Sep 9, 2002, 7:59:21 AM9/9/02
to
Hello!

Juancho <jua...@people-com.com> wrote:

> use...@chka.de (Christian Kaufhold) wrote in message news:<0t3d7ba9a...@simia.chka.de>...
>
>> > LookAndFeel win = UIManager.getLookAndFeel();
>> But why should it be called "win"?
>> You don't know which LookAndFeel is set then.
>
> Just the WINdows look and feel..

No, you don't know which LookAndFeel is set then, unless there is a
line

UIManager.setLookAndFeel(new WindowsLookAndFeel());

just above, in which case that line is pointless.


>> > UIManager.put("TextArea.keyBindings", win.getDefaults().get
>> > ("TextArea.keyBindings"));
>> It only works for JTextAreas create after that line.
>
> The problem is that it doesn't work at all..

You should have provided a compilable example, which would have
been very easy in this case.


In 1.4, the magic defaults key is name "TextArea.focusInputMap" instead.


import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import com.sun.java.swing.plaf.motif.MotifLookAndFeel;

import java.util.Hashtable;


public class T
{
public static void main(String[] args)
throws Exception
{
LookAndFeel from = new WindowsLookAndFeel();

from.initialize();

Hashtable defaults = from.getDefaults();

copy(defaults, "TextArea.keyBindings");
copy(defaults, "TextArea.focusInputMap");

from.uninitialize();


UIManager.setLookAndFeel(new MotifLookAndFeel());

JTextArea t = new JTextArea();

JFrame f = new JFrame();

f.getContentPane().add(new JScrollPane(t));

f.pack(); f.show();
}

private static void copy(Hashtable from, String key)
{
Object value = from.get(key);

if (value != null)
UIManager.put(key, value);
}
}

[
Funny sidenote, WindowsLookAndFeel does the following for me in 1.4:

Exception in thread "main" java.lang.NumberFormatException: multiple points
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1073)
at java.lang.Float.valueOf(Float.java:186)
at com.sun.java.swing.plaf.windows.WindowsLookAndFeel.initialize(WindowsLookAndFeel.java:108)
at T.main(T.java:16)


because it actually tries to parse the system property "os.version" as
a float.
]

So better use MetalLookAndFeel as a source for the keybindings if you
want your code to work on all OSes.

Christian

Juancho

unread,
Sep 11, 2002, 7:00:14 AM9/11/02
to
use...@chka.de (Christian Kaufhold) wrote in message news:<0t3d7c8ba...@simia.chka.de>...

> No, you don't know which LookAndFeel is set then, unless there is a
> line

[....]

> So better use MetalLookAndFeel as a source for the keybindings if you
> want your code to work on all OSes.
>
> Christian

Christian, thank you for help !

I've completly solved the problem now. I'm also starting to
understand how key binding really works.


- Juancho

0 new messages