Unable to capture key pressed :((
Hi,
I was learning about capturing key presses via the web and I added it to one of my existing Test classes... the problem is it just does not work :(
I have added color to the parts I think are important.
My code:
Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import javax.swing.JRadioButton;
public class Test extends JFrame implements Runnable,[COLOR="Red"]KeyListener [/COLOR]{
private JPanel contentPane;
/**
* Launch the application.
*/
/*
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} */
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
[COLOR="Red"] contentPane.addKeyListener(this);[/COLOR]
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
[COLOR="Red"]//addKeyListener( this );[/COLOR]
radio1 = new JRadioButton("Radio 1");
GridBagConstraints gbc_radio1 = new GridBagConstraints();
gbc_radio1.fill = GridBagConstraints.BOTH;
gbc_radio1.insets = new Insets(0, 0, 5, 5);
gbc_radio1.gridx = 1;
gbc_radio1.gridy = 1;
contentPane.add(radio1, gbc_radio1);
if(getRadioValue()==1){System.out.println("a");}else{System.out.println("b");}
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
but1ActionPerformed(arg0);
}
});
label1 = new JLabel("New label");
GridBagConstraints gbc_label1 = new GridBagConstraints();
gbc_label1.insets = new Insets(0, 0, 5, 0);
gbc_label1.gridx = 3;
gbc_label1.gridy = 4;
contentPane.add(label1, gbc_label1);
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 6;
contentPane.add(btnNewButton, gbc_btnNewButton);
}
[COLOR="Red"]
public void keyPressed( KeyEvent e )
{
System.out.println("a "+e.getKeyText(e.getKeyCode()));
}
public void keyReleased( KeyEvent e )
{
System.out.println("b "+e.getKeyText(e.getKeyCode()));
}
public void keyTyped( KeyEvent e )
{
System.out.println("b "+e.getKeyText(e.getKeyCode()));
}[/COLOR]
private javax.swing.JLabel label1;
private JRadioButton radio1;
private int getRadioValue() {
boolean b;
b = radio1.isSelected();
if(b==true)
return 1;
else
return 2;
}
Thread runner;
public void start()
{while(runner==null){runner=new Thread(this);runner.start();}}
public void run()
{int i=0;
while (true)
{
System.out.println(""+i);
updateCounter();
//label1.setText(""+i);
try {Thread.sleep(1000);}
catch(InterruptedException e){};
i++;
}
}
int iii=0;
public void updateCounter(){
label1.setText(""+iii);
iii++;
}
private void but1ActionPerformed(ActionEvent arg0) {
//label1.setText("a");
iii=0;
}
}