Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaFX 8 Buttons in ListView

Pavel SedivyMar 24 2014 — edited Mar 24 2014

Hi,

I have a problem that neither onAction handler nor onMouseClicked handler are triggered when I click on a Button which are included in a ListView (ListCells). In JavaFX 2.X this works OK (means both handlers are triggered).

Here is a code sample:

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ListCell;

import javafx.scene.control.ListView;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.stage.Stage;

import javafx.util.Callback;

public class ButtonInList extends Application {

    @Override

    public void start(Stage primaryStage) {

        ObservableList<String> items = FXCollections.observableArrayList();;

        items.addAll(

                "Adam", "Alex", "Alfred", "Albert"

                );

        final ListView<String> listView = new ListView<String>(items);

        listView.setCellFactory(new Callback<ListView<String>,

                ListCell<String>>() {

                    @Override

                    public ListCell<String> call(ListView<String> list) {

                        return new ButtonCell();

                    }

                });

        Button refButton = new Button("Reference");

        refButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override

            public void handle(ActionEvent arg0) {

                System.out.println("Reference Button Handler");

            }

        });

        Pane root = new VBox();

        root.getChildren().addAll(refButton, listView);

        primaryStage.setScene(new Scene(root, 200, 250));

        primaryStage.show();

    }

    static class ButtonCell extends ListCell<String> {

        @Override

        public void updateItem(final String item, boolean empty) {

            super.updateItem(item, empty);

            if (item != null) {

                Button button = new Button(item);

                button.setOnAction(new EventHandler<ActionEvent>() {

                    @Override

                    public void handle(ActionEvent arg0) {

                        System.out.println(item + " Button Handler");

                    }

                });

                setGraphic(button);

            }

        }

    }

    public static void main(String[] args) {

        launch(args);

    }

}

Reference button prints correctly "Reference Button Handler", but buttons in ListView don't print anything.

Can anyone explain me why? Has something changed in event handling in JavaFX 8?

Many thanks for help!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2014
Added on Mar 24 2014
3 comments
5,074 views