Results 1 to 10 of 10
Thread: Key combination
- 11-29-2009, 04:10 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Key combination
I have simple question but i can't directly find it in tutorials etc.
I'm making a texteditor and i use the GUI builder in Netbeans to generate my user interface code.
I made a menu that contains the menu items: cut copy and paste.
I use a jTextArea as my textfield but in this textfield cut copy and paste allready works as it should.
My question is how can i, when i press the menuitem cut in the menu, let the computer execute the ctrl-x combination?
(since it allready works when i select text and then press ctrl-x in the textfield, i only need to bind the cut menuitem to the ctrl-x keypress)
this is my event code for the menuitem cut
Java Code:private void cutActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }
-
The Sun tutorial on Text Components will show you exactly how this is done, complete with sample code: Using Text Components, page 2
- 11-29-2009, 04:55 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Doesn't that tutorial show how you add the menuitems and get the actions etc??
Everything works in my jTextArea, i can cut copy and paste by pressing ctrl+c +x and +v, i have made a menu called edit and it contains the menuitems: cut copy and paste, wich i have given shortkeys ctrl+c, ctrl+x and ctrl+v.
The only thing that doesnt work is when i go to the menu and click with my mouse on the cut (or copy or paste) menuitem, nothing happens
(because no code has been inserted there)
So actually i just need to know how to write in java: that when i press with my mouse on the cut menuitem, it will execute the keycombination ctrl+c
I dont need to write code or anything for a cut, copy or paste function, just wanna know how i can make java execute a specific keycomination.
- 11-29-2009, 05:00 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 11
think java can't.
write a seperate cut and paste method
- 11-29-2009, 05:14 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
k, if java really can't, I guess I have to.
I think it's kinda weird though since its allready working. :D
thx for the comment
-
Java can send key presses and releases I believe through the Robot class, but I don't recommend it in this situation as I don't feel it's an elegant solution.
- 11-29-2009, 05:22 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 11-29-2009, 06:40 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Thx for the fast replies
JosAH's solution worked :D
- 11-29-2009, 10:09 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 12
It may have worked, but it is not the proper solution. It will only work for the hard coded component.
You where given the answer in your first reply. The tutorial has a working example that shows you how to use the Actions defined by the EditorKit. These Actions will work on the last text component that had focus.
If you don't like the code that looks up the Actions you can just create a new DefaultEditorKit.CopyAction and use it.
- 11-30-2009, 08:11 AM #10
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 11
If your menu item doesn't work then your hotkey shouldn't. What I'm saying is that the menu item and ctrl+c should be linked, not seperate. Link them with setAccelerator() method that JMenuItem has. You should also have the cut copy items run the method. Not have the ctrl+c...etc run it. Here's how I accomplished it:
Java Code:public void actionPerformed(ActionEvent e) { String event = e.getActionCommand(); if(event.equals("Cut")) { textPane.cut(); } if(event.equals("Copy")) { textPane.copy(); } if(event.equals("Paste")) { textPane.paste(); } }
Last edited by collin389; 11-30-2009 at 08:18 AM.
Similar Threads
-
COMBINATION TEXT..help me..urgent
By conv_bay in forum New To JavaReplies: 10Last Post: 04-28-2009, 12:34 AM
Bookmarks