Adding click event to JavaFX TableCell - java

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.

You can try:
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("cell clicked!");
}
});
found here

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.

Related

AncestorNotifier fires the event AncestorAdded twice

I've added a AncestorListener to my component and found the following problem:
Everytime when the component is shown, the method ancestorAdded(AncestorEvent event) is called twice. Once the event is fired from the method AncestorNofifier.componentShown and once from the method AncestorNotifier.propertyChange.
addAncestorListener(new AncestorListener() {
#Override
public void ancestorAdded(AncestorEvent event) {
//do sth.
}
#Override
public void ancestorRemoved(AncestorEvent event) {
//do sth.
}
#Override
public void ancestorMoved(AncestorEvent event) {
//not used
}
});
Does anyone know how to prevent one of these envents?
Thanks.

JavaFX button not responding on first click

public void handle(){
submit.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
LoginConnection login = new LoginConnection();
boolean pass = login.login(usernameField.getText(), passwordField.getText());
if(pass)
flip(SceneNames.Main);
else
invalLoginMessage.setOpacity(1.00);
}
});
register.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
flip(SceneNames.Register);
}
});
}
When i click on submit or register, it takes two click for it to do anything. How do i fix this?
What happens is that on first click it adds the handlers specified in the method and on second and consecutive clicks, it uses the handlers. To fix it just create separate methods to add through fxml or scene builder.

GWT Tree: selection Listener

I'm trying to add a SelectionListener<TreeItem> on my Tree using the addSelectionHadler() method.
For my proof on onSelection(SelectionEvent<TreeIterm> event) I put a simple Windows.alert() but it don't do anything: when I select a treeItem that color change but doesn't open the window.
I write the Handler but if you want more code tell me.
Thank you.
class SelHand implements SelectionHandler<TreeItem> {
#Override
public void onSelection(SelectionEvent<TreeItem> event) {
Window.alert(event.getSelectedItem().getText());
}
}
SelHand selezionatore = new SelHand();
tree.addSelectionHandler(selezionatore);
Use the straightforward:
tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
#Override
public void onSelection(SelectionEvent<TreeItem> event) {
Window.alert(event.getSelectedItem().getText());
}
});

Adding mouse event for secondary mouse button Javafx

so i have this anchorpane where i wish to add a mouse listner for the Secondary mouse key ive tried the following but i keep getting an error anyone know what the problem is?
mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseButton>() {
#Override
public void handle(MouseButton event) {
System.out.Println("Works");
}
});
for the record i have also tried this:
mainDisplayPanel.addEventHandler(MouseButton.SECONDARY, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("WOrks");
}
});
Stack trace:
Bound mismatch: The generic method addEventHandler(EventType,
EventHandler) of type Node is not applicable for the
arguments (MouseButton, new EventHandler(){}). The
inferred type MouseButton&Event is not a valid substitute for the
bounded parameter
And the other:
Bound mismatch: The type MouseButton is not a valid substitute for the bounded parameter of the type EventHandler
There is no EventType based on MouseButton.SECONDARY. You need to check the MouseEvent itself:
mainDisplayPanel.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
System.out.println("Works");
}
}
});

Problem with SWT Combobox addSelectionListener()

cmbCategory.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//Do things
}
});
The above code works just fine when I make selection using the control itself, but does not fire when I try changing the index programmatically.
Can anyone help me?
Maybe this could work:
cmbCategory.addModifyListener(new ModifyListener(){
#Override
public void modifyText(ModifyEvent event) {
//Do things
}
});

Categories