KeyInput won't work while running a while (JCreator,Applet) - java

Supposedly whenever you press a key, it should access the code, but it doesn't and neither do I or my teacher know why. Here's the code:
class KeyInput implements KeyListener{
Actor player;
Graphics gBuffer;
public KeyInput(Actor player, Graphics gBuffer){
JTextField typingArea = new JTextField(20);
typingArea.addKeyListener(this);
this.player = player;
this.gBuffer = gBuffer;
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
player.modActor(1);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_RIGHT) {
player.modActor(2);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_UP) {
player.modActor(3);
gBuffer.drawString("ENTER",150,100);
}
if (key == KeyEvent.VK_DOWN) {
player.modActor(4);
gBuffer.drawString("ENTER",150,100);
}
}
}
And this is the while that plays during the program:
public void paint(Graphics g){ //main method to be called to play(g);
do{
drawBackground(gBuffer);
gBuffer.setFont(new Font("Calibri",Font.BOLD,20));
gBuffer.setColor(Color.CYAN);
gBuffer.drawString("Score: " + (int)score,18,20);
gBuffer.drawString("Score: " + (int)score,22,20);
gBuffer.drawString("Score: " + (int)score,20,22);
gBuffer.drawString("Score: " + (int)score,20,18);
gBuffer.setColor(Color.BLACK);
gBuffer.drawString("Score: " + (int)score,20,20);
play(gBuffer);
g.drawImage(virtualMem,0,0,this);
if(lose==false){
gBuffer.setFont(new Font("Calibri",Font.BOLD,50));
gBuffer.setColor(Color.CYAN);
gBuffer.drawString("GAME OVER",42,200);
gBuffer.drawString("GAME OVER",38,200);
gBuffer.drawString("GAME OVER",40,202);
gBuffer.drawString("GAME OVER",40,198);
gBuffer.setColor(Color.BLACK);
gBuffer.drawString("GAME OVER",40,200);
gBuffer.drawString("Final Score: " + (int)score,20,260);
}
}while(lose);
}
The key inputs need to move an actor I have, but it doesn't move at all, and yes, I click inside the applets every time. Any idea of whats going on?
EDIT: I have now gave up on keyListener and tried keyBindings, but I'm new with them and probably have something wrong, please help me...
public void init() {
panel = new JPanel();
this.setSize(400,700);
this.setFocusTraversalKeysEnabled(false);
this.addKeyListener(this);
player = new Actor(180,500);
virtualMem = createImage(400,800);
gBuffer = virtualMem.getGraphics();
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
panel.getActionMap().put("up", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
panel.getActionMap().put("down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
panel.getActionMap().put("left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
panel.getActionMap().put("right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
gBuffer.drawString("IT WORKS", 150, 150);
repaint();
}
});
}
The while is still the same (play). When ever I click something it doesn't access the methods either.

I am afraid that maybe that infinite loop inside paint() is interfering with the events management (remember that there is a single thread for those 2 things to be done). Why don't you just fire a repaint() on every significant event (for example, a key press)?

Related

Mouse event goes through the component

I have added a simple program to illustrate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleInternalFrame extends Frame
{
private static final long serialVersionUID = 1L;
static JLayeredPane desktop;
JInternalFrame internalFrame;
public SimpleInternalFrame()
{
super("Internal Frame Demo");
setSize(500, 400);
Panel p = new Panel();
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
desktop = new JDesktopPane();
desktop.setOpaque(true);
add(desktop, BorderLayout.CENTER);
}
public static void main(String args[])
{
SimpleInternalFrame sif = new SimpleInternalFrame();
sif.setVisible(true);
final JInternalFrame internalFrame = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame.setBounds(50, 50, 300, 200);
desktop.add(internalFrame, new Integer(1));
JTextField tf = new JTextField();
tf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
System.out.println("Text Field " + evt.getClickCount());
}
});
internalFrame.add(tf);
internalFrame.setVisible(true);
final JInternalFrame internalFrame2 = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame2.setBounds(50, 50, 200, 100);
desktop.add(internalFrame2, new Integer(1));
JButton jb = new JButton("click me");
jb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount() == 1)
{
System.out.println("Button " + evt.getClickCount());
internalFrame2.setVisible(false);
}
}
});
internalFrame2.add(jb);
internalFrame2.setVisible(true);
}
}
When running the code it will open a two internal frames.One has button.One has text field.Button will close the first Internal frame for single click.
Double click the button.It will show click count 2 as in text field.
This is the problem we have currently in the project.Thing is second frame dose not has text field in actual project.It has click-able item that work in double click event.
This is the problem we have currently.Please help.
tf.addMouseListener(new MouseAdapter() {
int cc;
public void mouseClicked(MouseEvent evt) {
int ccount = evt.getClickCount();
if(ccount == 1 || ccount == cc+1) {
cc = ccount;
System.out.println("Text Field " + evt.getClickCount());
}
}
});
This will work more than once.
Another possibility is to use components with overriden processMouseEvent():
public class SimpleInternalFrame extends Frame {
...
private MouseEvent lastMouseEvent;
public boolean checkEvent(MouseEvent e) {
if(lastMouseEvent != null) {
if(lastMouseEvent.getSource() != e.getSource()) {
if(e.getClickCount() != 1) {
return false;
}
}
}
lastMouseEvent = e;
return true;
}
class MTextField extends JTextField {
protected void processMouseEvent(MouseEvent e) {
if (checkEvent(e)) {
super.processMouseEvent(e);
}
}
}
class MButton extends JButton {
protected void processMouseEvent(MouseEvent e) {
if (checkEvent(e)) {
super.processMouseEvent(e);
}
}
}
public JTextField createText() {
return new MTextField();
}
public MButton createButton() {
return new MButton();
}
} //end of SimpleInternalFrame
create components:
final JTextField tf = sif.createText();
tf.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println("Text Field " + evt.getClickCount());
}
});
JButton jb = sif.createButton();
jb.setText("click me");
jb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 1) {
internalFrame2.setVisible(false);
}
}
});
You can use a static boolean called myTurn initialized to false and then in the textfield mouseClicked:
tf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount() >= 2 && !myTurn) {
myTurn = true;
evt.consume();
} else {
myTurn = true; //Initial one click on me
System.out.println("Text Field " + evt.getClickCount());
evt.consume();
}
}
});
This solves your problem in this basic case, but I don't know if it will in your application. The problem is that if you click the TextField before the Button, you can experience the same problem. However, if that isn't possible, this will solve it.
While searching on internet and reading some documents i was able get a solution.
int timerinterval = (int) Toolkit.getDefaultToolkit().
getDesktopProperty("awt.multiClickInterval");
final Timer timer = new Timer(timerinterval, new ActionListener()
{
public void actionPerformed(ActionEvent acEvt)
{
internalFrame2.setVisible(false);
}
});
jb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(final MouseEvent evt)
{
System.out.println("Button " + evt.getClickCount());
timer.setRepeats(false);
timer.start();
if (evt.getClickCount() > 1)
{
timer.restart();
}
}
});
One draw back of this solution is if user do a single click he need to wait
timerinterval unnecessarily.Any one has better solution please post.

How can i make my program write in another program?

I am making a macro program. While CTRL mode is on, the program registers the mouse coordinates when the user clicks. CTRL mode is activated when "CTRL" is pressed and deactivated when it's pressed again. If deactivated, the program asks "Perform actions?" If the response is yes, the program click on the registered coordinates.
I am now trying to make the program write if the user wrote something on CTRL mode. Is this possible? If so, how?
Edit: My explanation was poor so i decided to add an example.
EXAMPLE
I active CTRL mode. Then click on notepad and write "hello". Then I deactivate CTRL and run the actions. The program would click on notepad and write "hello".
I don't think you need this but here is the code:
public class Gui extends JFrame {
private JPanel mousePanel;
private JLabel statusBar;
private JLabel keyBar;
public boolean ctrl;
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
List<KeyEvent> charTyped = new ArrayList<KeyEvent>();
public int[] x;
public int[] y;
public Gui() {
super("Program");
mousePanel = new JPanel();
mousePanel.setBackground(Color.WHITE);
add(mousePanel, BorderLayout.CENTER);
statusBar = new JLabel("No events");
keyBar = new JLabel("No key events");
add(keyBar, BorderLayout.NORTH);;
add(statusBar, BorderLayout.SOUTH);
HandlerClass handler = new HandlerClass();
mousePanel.addMouseListener(handler);
mousePanel.addMouseMotionListener(handler);
this.addKeyListener(handler);
}
public void Click(int x, int y) throws AWTException {
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
}
private class HandlerClass implements MouseListener, MouseMotionListener, KeyListener {
//Mouse Listener
public void mouseClicked(MouseEvent event) {
statusBar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
if(ctrl) {
xList.add(MouseInfo.getPointerInfo().getLocation().x);
yList.add(MouseInfo.getPointerInfo().getLocation().y);
}
}
public void mousePressed(MouseEvent event) {
statusBar.setText(String.format("You are pressing the mouse at %d, %d", event.getX(), event.getY()));
}
public void mouseReleased(MouseEvent event) {
statusBar.setText(String.format("Released at %d, %d", event.getX(), event.getY()));
}
public void mouseEntered(MouseEvent event) {
statusBar.setText(String.format("Mouse entered at %d, %d", event.getX(), event.getY()));
mousePanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event) {
statusBar.setText(String.format("Mouse exited at %d, %d", event.getX(), event.getY()));
mousePanel.setBackground(Color.WHITE);
}
//Mouse Motion
public void mouseDragged(MouseEvent event) {
statusBar.setText(String.format("Dragging mouse at %d, %d", event.getX(), event.getY()));
}
public void mouseMoved(MouseEvent event) {
statusBar.setText(String.format("Moving mouse at %d, %d", event.getX(), event.getY()));
}
//Key Listener
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_CONTROL && !(ctrl)){
keyBar.setText("CTRL ON");
ctrl = true;
}
else if(e.getKeyCode() == e.VK_CONTROL && ctrl) {
keyBar.setText("CTRL OFF");
ctrl = false;
if(JOptionPane.showOptionDialog(null, "Perform actions?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
int index = 0;
for(int actionX : xList) {
try {
Click(actionX, yList.get(index));
} catch (AWTException e1) {
e1.printStackTrace();
}
index++;
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
}
On seeing your latest edit, this is probably what you're looking for.
I deleted the other content in my post since I kept getting comments on the old stuff.

Make a KeyEvent update JLabel in Java

I'm trying to use Java's KeyListener to update a JLabel as I type. Essentially, I'm making my own text field. Here's what I have:
/**
* Constructor for objects of class Dictionary
*/
public Dictionary()
{
frame = new JFrame();
frame.setTitle("Shori Dictionary");
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createWord()
{
frame.remove(pane);
pane = new PaintPane(field.getImage());
pane.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
newWord = new JLabel(text);
newWord.setFont(newWord.getFont().deriveFont(Font.BOLD, 28));
newWord.setForeground(Color.BLACK);
newWord.setHorizontalTextPosition(JLabel.LEFT);
newWord.setVerticalAlignment(JLabel.TOP);
newWord.setVerticalTextPosition(JLabel.TOP);
newWord.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0));
pane.add(newWord);
frame.pack();
frame.setLocationRelativeTo(null);
pane.setFocusable(true);
updateInteraction();
}
private void keyPress()
{
pane.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
for(int i = 97; i <= 122; i++){
//Cycles through every lowercase letter
if(e.getKeyChar() == (char)(i)&& pane.returnImage() == field.getImage()){
text += (char)(i);
break;
}
}
//Even in the Debugger, these next if-elses have never worked
if(e.getKeyCode() == KeyEvent.VK_SPACE&& pane.returnImage() == field.getImage()) text += " ";
else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE&& pane.returnImage() == field.getImage()){
int x = text.length();
text = text.substring(0,x-1); //Not sure if this works, haven't been able to test it yet
}
else if(e.getKeyCode() == KeyEvent.VK_ENTER&& pane.returnImage() == field.getImage()){
//do something with the text
text = "";
//exit the word creator
}
newWord.setText(text);
newWord.repaint(); //Apparently this isn't necessary...
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
);
}
private void mouseAction()
{
pane.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent arg0) {
//cover page
if(open.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) displayPages();
else if(search.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) searchWord();
else if(enter.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) createWord();
//inner pages
else if(nextPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("next");
else if(prevPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("previous");
else if(cancel.contains(arg0.getPoint())&& pane.returnImage() == field.getImage()) coverPage();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
}
);
}
private void mouseMovement()
{
pane.addMouseMotionListener(new MouseMotionListener()
{
#Override
public void mouseMoved(MouseEvent e) {
if(search.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(enter.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(open.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(nextPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(prevPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(cancel.contains(e.getPoint())&& pane.returnImage() == field.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else{
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
#Override
public void mouseDragged(MouseEvent e) {
}
}
);
}
private void updateInteraction(){
mouseMovement();
mouseAction();
keyPress();
}
public class PaintPane extends JPanel {
private Image background;
private Graphics g2d;
public PaintPane(Image image) {
background = image;
}
#Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Insets insets = getInsets();
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int x = (width - background.getWidth(this)) / 2;
int y = (height - background.getHeight(this)) / 2;
g.drawImage(background, x, y, this);
}
//g.fillRect(654, 798, 358, 77); //for testing rectangle positioning
}
public Image returnImage() {
return background;
}
}
I'm using BluJ to write this, and it has a built in Debugger. I just tried adding keyPress(); before the updateInteraction(); in createWord(), and ran the Debugger to go through each method step by step. Everything worked perfectly. Then I tried without the Debugger, and it wouldn't display any text while I was typing. So, I turned on the Debugger again. It didn't detect any keys being typed at all. I don't know why it only worked that once, but it was definitely working. This is my first time working with KeyListener, MouseListener, and MouseMotionListener. Is there a better way to get this program to run properly?
and it wouldn't display any text while I was typing
A component needs to have focus in order to respond to KeyEvents. A JPanel is not focusable by default.
I'm making my own text field
Why? What functionality is missing from JTextField?
I would just use JTextField and then add a DocumentListener to the document from the text field. Read the section from the Swing tutorial on How to Write a Document Listener for more information.
Your Component must have the focus for keyboard input on Key Event. However, to check it in action, first make your JPanle focus-able by panel.setFocusable(true). Then try with panel.requestFocusInWindow()
to get the focus to panel on application start-up.
However, in your code:
for(int i = 97; i <= 122; i++){
//Cycles through every lowercase letter
if(e.getKeyChar() == (char)(i)&& pane.returnImage() == field.getImage()){
text += (char)(i);
break;
}
}
Why do you have to use a loop here? Just simply putting:
if(evt.getKeyChar() >=97 && evt.getKeyChar() <=122 && pane.returnImage() == field.getImage())
text += evt.getKeyChar()
Should do the work.
If you are just doing a learning exercise, then it's OK to make your own implementation instead of using JTextField. You can make a JPanel receive key events by calling setFocusable(true) on it.
Even after that, you should be aware of something that will probably trip you up: keyTyped callbacks don't fill out the keyCode property of KeyEvents. This is because it's possible to type in characters that do not have a single associated key. For example, on Windows you can fire keyPressed once by typing ALT+0160. You hit 5 keys but entered one character (a space) and got a single keyPressed callback.
To summarize: keyPressed and keyReleased deliver keyCodes and might deliver keyChar. keyTyped never gets a keyCode, and always gets a keyChar.
public class PanelKeyListener extends JFrame {
private JPanel _contentPane;
StringBuilder _stringBuilder = new StringBuilder();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelKeyListener frame = new PanelKeyListener();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public PanelKeyListener() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_contentPane = new JPanel() {
protected void paintComponent(java.awt.Graphics g) {
g.drawString(_stringBuilder.toString(), 10, 20);
}
};
setContentPane(_contentPane);
_contentPane.setFocusable(true);
_contentPane.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped char[" + e.getKeyChar()
+ "] code[" + e.getKeyCode() + "]");
_stringBuilder.append(e.getKeyChar());
_contentPane.repaint();
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased char[" + e.getKeyChar()
+ "] code[" + e.getKeyCode() + "]");
}
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed char[" + e.getKeyChar()
+ "] code[" + e.getKeyCode() + "]");
}
});
}
}
You shouldn't reinvent the wheel if you can avoid it. Use/extend JTextField if it's practical to do so and meets your needs.

Generate a double click mouse event in Java Swing

I am trying to generate a double click mouse event on the EDT as follows:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
component.dispatchEvent(new MouseEvent(
component,
MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis(),
InputEvent.BUTTON1_MASK,
x, y,
2, // click count
false
));
}
});
This does not seem to dispatch a double click event, even though I am setting the click count to 2.
Any suggestions or examples?
Considering:
final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();
Listeners:
clickTwiceButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 2) {
JOptionPane.showMessageDialog(null, "Double clicked!");
}
}
});
fireEventButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// Invoking later for no reason, just to simulate your code
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
clickTwiceButton.dispatchEvent(new MouseEvent(
fireEventButton,
MouseEvent.MOUSE_CLICKED,
1,
MouseEvent.BUTTON1,
0, 0,
2,
false
));
}
});
}
});
When I click fireEventButton the MouseEvent gets correctly dispatched to clickTwiceButton, and the dialog appears as expected.
So, as #Andrew pointed out, the problem seems to be that either you are firing the event to the wrong component or that something is not right with the registered MouseListener / MouseAdapter code.
Use component.getMouseListeners() to check for your component Listeners and debug the code that handles its events.
The method is very simple. You should get the time of the first click and the time of the second click, then you can do a condition in between.
Method code as below:
private boolean state=false;
private long first_pressed;
JButton btnAdd = new JButton("add");
btnAdd.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(state==false) {
first_pressed=e.getWhen();
state=true;
}
if(first_pressed!=e.getWhen())
{
JOptionPane.showConfirmDialog(null,"doubel click","Click",JOptionPane.YES_NO_OPTION);
state=false;
}
}
});
public class TestMouseListener implements MouseListener {
private boolean leftClick;
private int clickCount;
private boolean doubleClick;
private boolean tripleClick;
public void mouseClicked(MouseEvent evt) {
if (evt.getButton()==MouseEvent.BUTTON1){
leftClick = true; clickCount = 0;
if(evt.getClickCount() == 2) doubleClick=true;
if(evt.getClickCount() == 3){
doubleClick = false;
tripleClick = true;
}
Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
Timer timer = new Timer(timerinterval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(doubleClick){
System.out.println("double click.");
clickCount++;
if(clickCount == 2){
doubleClick(); //your doubleClick method
clickCount=0;
doubleClick = false;
leftClick = false;
}
}else if (tripleClick) {
System.out.println("Triple Click.");
clickCount++;
if(clickCount == 3) {
tripleClick(); //your tripleClick method
clickCount=0;
tripleClick = false;
leftClick = false;
}
} else if(leftClick) {
System.out.println("single click.");
leftClick = false;
}
}
});
timer.setRepeats(false);
timer.start();
if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}
}
public static void main(String[] argv) throws Exception {
JTextField component = new JTextField();
component.addMouseListener(new TestMouseListener());
JFrame f = new JFrame();
f.add(component);
f.setSize(300, 300);
f.setVisible(true);
component.addMouseListener(new TestMouseListener());
}
}

Distinguish between a single click and a double click in Java

I search the forum and see this codes:
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println(" and it's a double click!");
wasDoubleClick = true;
} else {
Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty(
"awt.multiClickInterval");
timer = new Timer(timerinterval.intValue(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (wasDoubleClick) {
wasDoubleClick = false; // reset flag
} else {
System.out.println(" and it's a simple click!");
}
}
});
timer.setRepeats(false);
timer.start();
}
}
but the code runs incorrectly(Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!"). Can anybody show me why? or can you give me some better ways to do this?
Thank you!
Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!").
That is normal. A double click only happens if you click twice within the specified time interval. So sometimes if you don't click fast enough you will get two single clicks in a row.
Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
The above line of code determines how fast the double click must be.
For what its worth here is some code I have used to do the same thing. Don't know if its any better or worse than the code you have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ClickListener extends MouseAdapter implements ActionListener
{
private final static int clickInterval = (Integer)Toolkit.getDefaultToolkit().
getDesktopProperty("awt.multiClickInterval");
MouseEvent lastEvent;
Timer timer;
public ClickListener()
{
this(clickInterval);
}
public ClickListener(int delay)
{
timer = new Timer( delay, this);
}
public void mouseClicked (MouseEvent e)
{
if (e.getClickCount() > 2) return;
lastEvent = e;
if (timer.isRunning())
{
timer.stop();
doubleClick( lastEvent );
}
else
{
timer.restart();
}
}
public void actionPerformed(ActionEvent e)
{
timer.stop();
singleClick( lastEvent );
}
public void singleClick(MouseEvent e) {}
public void doubleClick(MouseEvent e) {}
public static void main(String[] args)
{
JFrame frame = new JFrame( "Double Click Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.addMouseListener( new ClickListener()
{
public void singleClick(MouseEvent e)
{
System.out.println("single");
}
public void doubleClick(MouseEvent e)
{
System.out.println("double");
}
});
frame.setSize(200, 200);
frame.setVisible(true);
}
}
public void mouseClicked(MouseEvent evt) {
if (evt.getButton()==MouseEvent.BUTTON1){
leftClick = true; clickCount = 0;
if(evt.getClickCount() == 2) doubleClick=true;
Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
timer = new Timer(timerinterval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(doubleClick){
System.out.println("double click.");
sb = new StringBuffer();
sb.append("Double Click");
clickCount++;
if(clickCount == 2){
clickCount=0;
doubleClick = false;
}
} else {
sb = new StringBuffer();
sb.append("Left Mouse");
System.out.println("single click.");
}
}
});
timer.setRepeats(false);
timer.start();
if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}

Categories