Creating 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 the user selects a line and presses the "Delete" button on the keyboard, this line should be deleted.

My line is deleted using JButton1 perfectly.

  if (e.getSource() == KeyEvent.VK_DELETE) { // Delete row Method } 

But that will not work.

+2
source share
3 answers

I don’t know what the problem is because you are providing too little code. However, you cannot use getSource() to check which key is entered (pressed or released). Use getKeyChar() and getKeyCode() .


The following is an explanation of my code:

  • You need to add KeyListener to the component (of course)
  • Component must have focus
    • The component should be focused (set with the ability to focus on true)
    • Component must request focus
  • Cancel keyTyped keyPressed or keyReleased to retrieve KeyEvent
    • To check which key is entered in keyTyped , use getKeyChar()
    • 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"); } } } } 
+1
source
  • do not use KeyListener for this task, and in Swing never use KeyBindings instead

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

  • use KeyBindings for JTable, override Delete key

+4
source

Take a look at this page:

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

Taken from there:

Create a key listener for this button (it seems you have already done this):

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

And implement keylistener:

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

Try it and tell if it works.

+2
source

All Articles