Adding click event to JavaFX TableCell


Question

I have TableView and I want my program to doSomething() when user clicks on a cell. After searching on Internet (stackoverflow included), I found this.

Tried that method, but I got a compile error on these code :

EventHandler click = new EventHandler() {
    public void handle(MouseEvent t) {
        System.out.println("CLICKED");
    }
};

NetBeans asked me to override all abscract method, so I did it.

EventHandler click = new EventHandler() {
    @Override
    public void handle(MouseEvent t) {
        System.out.println("CLICKED");
    }
};

Still got same error :

error: method does not override or implement a method from a supertype

If I remove @Override annotation, I got :

error: <anonymous pengamatan.penginderaan.FXMLDocumentController$4> is not abstract and does not override abstract method handle(Event) in EventHandler

Any help? Thank you.

1
0
3/17/2014 11:24:10 AM

Accepted Answer

Found the solution! Here is the code :

....
import javafx.scene.input.MouseEvent;
....
....
EventHandler click = new EventHandler<MouseEvent>() {
@Override
    public void handle(MouseEvent t) {
        if(t.getClickCount()>1) {
        System.out.println("DOUBLE CLICK");
        }
    }
};
....

Hope it helps. Thank you.

-4
3/18/2014 8:18:30 AM

You can try:

cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
         System.out.println("cell clicked!");
    }
});

found here


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon