Results 1 to 10 of 10

Thread: Key combination

  1. #1
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default 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:
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    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.

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    6

    Default

    think java can't.
    write a seperate cut and paste method

  5. #5
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dejos456 View Post
    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
    Of course it can be done in Java; in the actionPerformed( ... ) method of your, say, CopyMenuItem all you have to do is this:

    Java Code:
    public void actionPerformed(ActionEvent ae) {
    
    	// assuming 'editor' extends a JTextComponent
    	editor.copy();
    }
    kind regards,

    Jos

  8. #8
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    Thx for the fast replies

    JosAH's solution worked :D

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  10. #10
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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

  1. COMBINATION TEXT..help me..urgent
    By conv_bay in forum New To Java
    Replies: 10
    Last Post: 04-28-2009, 12:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •