import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
@SuppressWarnings("serial")
public class KeyTime {
ArrayList<JButton> buttons;
JFrame frame;
JPanel panel;
JPanel buttonPanel;
JTextArea main;
public void setUpWindow() {
frame = new JFrame("Key Problem Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
main = new JTextArea(20,70);
main.setEditable(false); //<<<<<<< To keep from getting focus
panel.add(main); //<<<<<<<<<<<<<<<<< THIS GETS the focus!!!
buttonPanel = new JPanel();
buttons = new ArrayList<JButton>();
ListenForButtons lfb = new ListenForButtons();
for(int i=0;i<2;i++) {
JButton b = new JButton();
if(i==0) {b.setText("Click me!");}
else if(i==1) {b.setText("Me too!");}
b.addActionListener(lfb);
buttons.add(b);
buttonPanel.add(b);
}
// Call listener when the '0' key is pressed
DoBtnClick dbc = new DoBtnClick(buttons.get(0));
addKeyAccelerator(buttonPanel, dbc, "action", '0', 0);
addKeyAccelerator(buttonPanel, dbc, "action", KeyEvent.VK_NUMPAD0, 0); // Number pad is different
frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
frame.add(BorderLayout.CENTER, panel);
frame.setBounds(30,30,500,300);
frame.pack();
frame.setVisible(true);
}
// Class to click button when a key is pressed
private class DoBtnClick extends AbstractAction {
JButton btn;
public DoBtnClick(JButton btn) {
this.btn = btn; // Save the button we're to click
}
public void actionPerformed(ActionEvent evt) {
System.out.println("bound evt=" + evt);
btn.doClick();
}
} // end class DoBtnClick
public void addKeyAccelerator(JComponent c, AbstractAction act, String name, int keyCode, int mask) {
KeyStroke key = KeyStroke.getKeyStroke(keyCode, mask);
c.getInputMap(buttons.get(0).WHEN_IN_FOCUSED_WINDOW).put(key,name);
c.getActionMap().put(name,act);
}
//---------------------------------------
public static void main(String[] args) {
KeyTime kt = new KeyTime();
kt.setUpWindow();
}
//----------------------------------------------------------------------------
class ListenForButtons extends AbstractAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("aP e=" + e);
//aP e=java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=0,when=1308913543812,modifiers=] on javax.swing.JPanel[,0,330,780x36,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=393,maximumSize=,minimumSize=,preferredSize=]
Object userPressed = e.getSource();
System.out.println("uP=" + userPressed); // uP=javax.swing.JPanel[,0,330,780x36,layout=java.awt.FlowLayout,alignmentX=0.0,a
int i = buttons.indexOf(userPressed);
System.out.println("i=" + i); // i=-1
JButton b = buttons.get(i);
String line = b.getText();
if(line.equals("Click me!")) {
main.setText("");
main.append("Both buttons call the same actionPerformed method. \n That method determines which button was pressed.");
main.append("\n Only the 'Click Me!' button should respond to the key stroke of the number '0'");
} else if(line.equals("Me too!")) {
main.setText("");
main.append("Unfortunately neither respond.");
main.append("\n Does this actionPerformed method need a way to receive the key stroke perhaps?");
}
}
}
}