I'm trying to make an app to connect computer with arduino via bluetooth. So far it was going well (I had simple buttons to sent 0 or 1 and connection was going smoothly), but when I added key listeners it stopped responding right after it connected. It's basically like this: - I click 'run', app opens, I can click buttons, I can push keys on keyboard and I get info that I'm not connected - click 'connect' - it's connected via bluetooth - I can still click on buttons (which now don't do anything) but as soon as I push a key I can't do anything - even click on an 'x' to close it. It appeared that the frame lost focus, so I set focusable on false on everything accept the frame. I'm out of ideas where the problem might be...
here's the code of connect button:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!connected){
textArea.append("Connecting to the device on port "+ selectedCOM + "....\n");
try {
serialPort = new SerialPort(selectedCOM);
textArea.append("Port opened: " + serialPort.openPort() + "\n");
textArea.append("Params set: " + serialPort.setParams(4800, 8, 1, 0) + "\n");
textArea.append("Connected succesfully!\n");
connected = true;
} catch (SerialPortException e1) {
//e1.printStackTrace();
textArea.append("Error: "+e1.getExceptionType()+"\n");
}
} else {
textArea.append("You can't do that, you are already connected!\n");
}
}
});
And a fragment of key listener (the rest if analogical):
addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) {
up.getModel().setPressed(true);
if(connected){
try {
serialPort.writeInt(FORWARD);
System.out.println(FORWARD);
} catch (SerialPortException e1) {
textArea.append("Error: "+e1.getExceptionType()+"\n");
e1.printStackTrace();
}
}
else {
textArea.append("You are not connected.\n");
}
FORWARD is defined as 1 :
private final int FORWARD = 1;
And also here is the whole main class: public class Communication{
private static void createAndShowGUI() {
//Create and set up the window.
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setFocusable(true);
frame.setSize(new Dimension(600,600));
frame.setLayout(new GridLayout());
frame.setLocation(500, 250);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The whole Frame class (which extends JFrame) has about 300 lines, so I didn't want to post it whole here. If it helps it's in here
EDIT: I changed key listeners to key bindings with swing workers, as suggested in comments, but it still doesn't responding. I think there may be some errors with connection but I have no idea how to resolve them...
Changes in the code:
forwardAction = new AbstractAction(){
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
SwingWorker<Void,Integer> worker = new SwingWorker<Void,Integer>(){
@Override
protected Void doInBackground() throws Exception {
goForward();
return null;
}
};
worker.execute();
}
};
pane.getInputMap(IFW).put(KeyStroke.getKeyStroke("UP"), MOVE_FORWARD);
pane.getActionMap().put(MOVE_FORWARD,forwardAction);