Sajad
Sajad

Reputation: 2363

Create a keyboard shortcut for a button

I have a JTable that has a delete button to delete its rows.

I want to create a shortcut, for example when user selects a row and presses the 'Delete' button on keyboard , that line should be deleted.

My line is deleted with my JButton1 perfectly.

        if (e.getSource() == KeyEvent.VK_DELETE) {

           // Delete row Method
    }

But it doesn't work.

Upvotes: 2

Views: 20164

Answers (3)

Mikel Urkia
Mikel Urkia

Reputation: 2095

Take a look to this page:

http://www.coderanch.com/t/341332/GUI/java/setting-keyboard-navigation-shortcut-keys

Taken from there:

Create a key listener for that button (it seems you have already made that):

Button btn = new Button("Press Me");
btn.addKeyListener(myKeyListener);

And implement the keylistener:

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_DELETE ){
        //Do whatever you want
    }
}

Try it and tell me if it works.

Upvotes: 2

johnchen902
johnchen902

Reputation: 9601

I don't know what is the exact problem because you provide too few code. However, you can't use getSource() to test which key is typed (pressed, or released). Use getKeyChar() and getKeyCode().


The following is explanation of my code:

  1. You need to add a KeyListener to a component(of course)
  2. The component must have focus
    1. The component must be focusable (set focusable to true)
    2. The component need to request for focus
  3. Override keyTyped keyPressed or keyReleased to retrieve KeyEvent
    1. To check which key is typed in keyTyped, use getKeyChar()
    2. To check which key is pressed or released in keyPressed and keyReleased, use getKeyCode()

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Test {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(new Dimension(410, 330));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(null);
        JPanel panel = new JPanel();
        panel.setBackground(Color.GREEN);
        panel.setBounds(50, 50, 300, 200);

        panel.addKeyListener(new MyKeyListener()); // add KeyListener
        panel.setFocusable(true); // set focusable to true
        panel.requestFocusInWindow(); // request focus

        f.getContentPane().add(panel);
        f.setVisible(true);
    }

    static class MyKeyListener extends KeyAdapter {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == '\177') {
                // delete row method (when "delete" is typed)
                System.out.println("Key \"Delete\" Typed");
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                // delete row method (when "delete" is pressed)
                System.out.println("Key \"Delete\" Pressed");
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                // delete row method (when "delete" is released)
                System.out.println("Key \"Delete\" Released");
            }
        }
    }
}

Upvotes: 2

mKorbel
mKorbel

Reputation: 109815

  • don't to use KeyListener for this job, and in Swing never, use KeyBindings instead

  • add ListSelectionListener to JTable, notice to test if(table.getSelectedRow > 0)

  • use KeyBindings for JTable, override Delete key

Upvotes: 4

Related Questions