Martin Lukas
Martin Lukas

Reputation: 314

MouseListener - MouseClicked

After 2 hours of searching I really can't find out why my code isn't working so I wonder if you could help.

All I want to see is "Clicked" when I press the button. My class MouseInput implements MouseListener and in the method mouseClicked all I got is system.out...("clicked");

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class App implements Runnable {


    private Display display;
    private BufferStrategy bs;
    private Graphics g;


    private int cubeSide = 150;
    private String title;
    private int height,width;
    private boolean running = false;
    private Thread thread;
    private MouseInput mouseInput;



    public App(String title,int width,int height){
        this.height=height;
        this.width=width;
        this.title=title;
        display = new Display(title, width, height);
    }
    void setBufferStrategy(){
        if(display.getCanvas().getBufferStrategy()==null){
            display.getCanvas().createBufferStrategy(3);
        }
        bs = display.getCanvas().getBufferStrategy();
    }
    void init(){
        setBufferStrategy();
        mouseInput = new MouseInput();
        display.getFrame().addMouseListener(mouseInput);
    }
    public synchronized void start(){
        if(running==true)
            return;
        running=true;
        thread = new Thread(this);
        thread.start();

    }
    void render(){
    }
    @Override
    public void run() {
        init();
        while(running){
            render();
        }

    }
    public synchronized void stop(){

    }
}

MouseInput Code:

`import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class MouseInput implements MouseListener {

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    System.out.println("Click");
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}`

And for the Display class:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;


public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width,height;


public Display(String title,int width,int height){
    this.width = width;
    this.height=height;
    this.title=title;
    CreateDisplay();
}
public void CreateDisplay(){
    frame = new JFrame(title);
    canvas = new Canvas();
    frame.setSize(width,height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setFocusable(false);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);

    canvas.setPreferredSize(new Dimension(width,height));
    canvas.setMaximumSize(new Dimension(width,height));
    canvas.setMinimumSize(new Dimension(width,height));
    canvas.setBackground(Color.WHITE);
    frame.add(canvas);
    frame.pack();

}
public Canvas getCanvas(){
    return canvas;
}
public JFrame getFrame(){
    return frame;
}
}

Upvotes: 1

Views: 1127

Answers (2)

Evan
Evan

Reputation: 2546

First, you can use an inline mouse adapter rather than extending mouse listener and needing a separate file for the mouse code.

Second, if you want to observe the click on your button, add the listener to your button.

    yourJButton.addMouseListener( new MouseAdapter()
    {
        @Override
        public void mouseClicked( MouseEvent e )
        {
            {
                 //do stuff
            }
        }
    });

Upvotes: 0

Pietro Martinelli
Pietro Martinelli

Reputation: 1906

I think the problem can be in display.getFrame().addMouseListener(mouseInput): I suppose display.getFrame() returns a an instance of a class extending java.awt.Component; according to API reference, addMouseListener

Adds the specified mouse listener to receive mouse events from this component.

I think an event on your button is not an event from the component you registered the listener on: can you try to register the listener on the canvas instance instead of on frame instance? May be the event originated from canvas, not from frame...

Upvotes: 1

Related Questions