Button: Allow to press and release button by mouse and key
In the previous implementation a click event was triggered when the button was pressed. This implementation allows to set and release buttons independently both by mouse clicking and by key typing.
This commit is contained in:
parent
bc6b7535d0
commit
f49e1b8f5f
|
@ -1,4 +1,5 @@
|
||||||
/*
|
/*
|
||||||
|
* Copyright (c) 2014, TU Braunschweig.
|
||||||
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -30,8 +31,10 @@
|
||||||
|
|
||||||
package org.contikios.cooja.interfaces;
|
package org.contikios.cooja.interfaces;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
@ -130,10 +133,51 @@ public abstract class Button extends MoteInterface {
|
||||||
|
|
||||||
panel.add(clickButton);
|
panel.add(clickButton);
|
||||||
|
|
||||||
clickButton.addActionListener(new ActionListener() {
|
clickButton.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
clickButton();
|
sim.invokeSimulationThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doPressButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
sim.invokeSimulationThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doReleaseButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
clickButton.addKeyListener(new KeyAdapter() {
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
sim.invokeSimulationThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doPressButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
sim.invokeSimulationThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doReleaseButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue