I have a javafx application which contains a listview with a custom cell factory. each cell contains a button to do a specific action for that cell. before updating to java 8 every thing was ok but after running my app with java 8 when i click the button instead of handling event by button, the whole cell is selected. Here is my code:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
public class CustomCell extends ListCell<String> {
private Button actionBtn;
public CustomCell() {
super();
setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
//do something
}
});
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setEditable(false);
if (item != null) {
Label name = new Label(item);
actionBtn = new Button("my action");
actionBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println("hiiiiiiiii");
}
});
GridPane pane = new GridPane();
pane.getStyleClass().add("gridpane");
pane.add(name, 0, 0);
pane.add(actionBtn, 0, 1);
setGraphic(pane);
} else {
setText(null);
setGraphic(null);
}
}
}