I am having a checkbox and a button. I want that when i press ENTER to activate the button. It works as expected if i just press enter at run, but if i use the checkbox before, it doesn't work anymore.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
public class Animation{
public Animation(){
JFrame frame = new JFrame();
Pane a = new Pane();
a.addKeyListener(a);
frame.add(a);
//frame.setUndecorated(true);
// frame.setOpacity(0.9f);
frame.setVisible(true);
frame.setSize(700, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Animation();
}
public class Pane extends JPanel implements KeyListener{
JButton buton = new JButton("BUTTON!!!! ");
JCheckBox c = new JCheckBox("Check");
public Pane(){
add(new JCheckBox("CHECKK"));
add(buton);
c.setFocusable(false);
buton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Pressed!");
}
});
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCosw() == KeyEvent.VK_ENTER){
//if(buton.isDisplayable()){
System.out.println("pressed");
//buton.doClick();
//return;
//}
}
}
}
}