Browse JButtons with Arrows

I created a JButton array, which is a card, there are 16, 4 by 4. How can I view arrows on a keyboard instead of a mouse among JButton and how can I "click" on a JButton by pressing ENTER instead of mouseclicking? Perhaps there is another way to do this instead of using JButton s?

Yours faithfully!

+4
source share
6 answers

I created a solution that will allow you to navigate through the arrow buttons and activate them with a space and enter.

Below is all the code. No comments are provided, so let me know if you have any questions.

 import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class ButtonPane extends JPanel { private JButton[][] buttons; public ButtonPane(int row, int col) { super(new GridLayout(row, col)); buttons = new JButton[row][col]; for (int i = 0; i < buttons.length; i++) { for (int j = 0; j < buttons[i].length; j++) { final int curRow = i; final int curCol = j; buttons[i][j] = new JButton(i + ", " + j); buttons[i][j].addKeyListener(enter); buttons[i][j].addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: if (curRow > 0) buttons[curRow - 1][curCol].requestFocus(); break; case KeyEvent.VK_DOWN: if (curRow < buttons.length - 1) buttons[curRow + 1][curCol].requestFocus(); break; case KeyEvent.VK_LEFT: if (curCol > 0) buttons[curRow][curCol - 1].requestFocus(); break; case KeyEvent.VK_RIGHT: if (curCol < buttons[curRow].length - 1) buttons[curRow][curCol + 1].requestFocus(); break; default: break; } } }); add(buttons[i][j]); } } } private KeyListener enter = new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) { ((JButton) e.getComponent()).doClick(); } } }; public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ButtonPane(4, 4)); f.pack(); f.setVisible(true); } } 

To run this, simply paste into a class called ButtonPane .

The important bit here is calling requestFocus() in the correct JButton when the arrow key is called. I also registered an additional KeyListener when the Enter key is pressed.

+7
source

Just press Enter on your keyboard ... Make sure you use

 button.addActionListener(yourActionListener); 

instead of creating a mouse listener.

Oh, I forgot the other part .: D

For viewing, you can use button.requestFocus() to change the focus to a specific button.

Here is a sample code:

 final JButton matrix[][] = new JButton[4][4]; for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { final int xx = x; final int yy = y; JButton b = new JButton(); b.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int c = e.getKeyCode(); if (c == KeyEvent.VK_UP) { // Oops, corrected (xx > 0) to (yy > 0) if (yy > 0) matrix[xx][yy-1].requestFocus(); } else if (c == KeyEvent.VK_DOWN) { if (yy < 3) matrix[xx][yy+1].requestFocus(); } // Add the other directions here } }); matrix[x][y] = b; } } 
+2
source

See key bindings .

 component[0][0].getInputMap().put(KeyStroke.getKeyStrokeForEvent(KeyEvent.VK_RIGHT), "moveRight"); component[0][0].getActionMap().put("moveRight", new Action() { @Override public void actionPerformed() { component[0][1].requestFocus(); } }); 

I'm not sure the code is completely right, but you understood this idea. You will need to make 36 of them to get all the directions between all the buttons, so you probably want to write a few cycles to automate the process.

+2
source

How can I "click" on a JButton by pressing ENTER instead of mouseclicking?

See Enter key and button .

How can I view JButton with arrows on the keyboard instead of mous

For left / right arrow keys, you can add these keys to the focus bypass keys. See How to use the focus subsystem .

For up / down keys, you need to create your own action and then bind the action to KeyStroke. See How to use key bindings . The How to Use Actions tutorial has a section.

+2
source

Using the focus subsystem.

This will help you get started using the arrow keys to select components. It is unchecked, so I apologize for any errors: D

Assuming you have your button array in a JFrame,

 JFrame frame = new JFrame(); //pseudo-method to add your buttons to the frame in the appropraite order. frame.addButtonsToPanel(buttonArray, frame); //gets the default forward traversal keys (tab) Set forwardKeys = frame.getFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); // your own set of forward traversal keys Set newForwardKeys = new HashSet(forwardKeys); // add the RIGHT ARROW key newForwardKeys.add(KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0)); //apply your new set of keys frame.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys); //gets the default backward traversal keys (shift-tab) Set backwardKeys = frame.getFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); // your own set of backward traversal keys Set newBackwardKeys = new HashSet(backwardKeys); // add the LEFT ARROW key newBackwardKeys.add(KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0)); //apply your new set of keys frame.setFocusTraversalKeys( KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, newBackwardKeys); 

ps For details, see How to use the focus subsystem.

+1
source

I recently added this little stone to my GUI utility class. It simply adds new keys to the same system, which changes focus [tab]:

 public static void addUpDownToTraversalKeys(Component c) { addTraversalKeys(c, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyEvent.VK_DOWN); addTraversalKeys(c, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, KeyEvent.VK_UP); } public static void addLeftRightToTraversalKeys(Component c) { addTraversalKeys(c, KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, KeyEvent.VK_RIGHT); addTraversalKeys(c, KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, KeyEvent.VK_LEFT); } public static void addTraversalKeys(Component c, int keysetId, int...keyCodes) { HashSet<AWTKeyStroke> newKeys = new HashSet<AWTKeyStroke>( c.getFocusTraversalKeys(keysetId)); for (int keyCode : keyCodes) newKeys.add(AWTKeyStroke.getAWTKeyStroke(keyCode, 0)); c.setFocusTraversalKeys(keysetId,newKeys); } 

Added to my GuiUtilities class, just calling GuiUtilities.addUpDownToTraversalKeys(this); inside the frame constructor, you can use the up and down arrow keys to go through all the elements. Please note that addLeftRightToTraversalKeys() not recommended if you received text areas;)

0
source

All Articles