我有一个主窗口,用户可以在其中单击ListView项来选择程序功能。但是,即使他完全在Items(白色箭头)程序之外单击,仍会启动“数字生成器”功能。我不喜欢那样。



public class MainRootController2 extends Controller implements Initializable {

    @FXML
    StackPane stackPane;
    @FXML
    ListView<String> listView;
    private ObservableList<String> list;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //making a list and setting a background
        list = FXCollections.observableArrayList("Number Generator", "Cards");
        listView.setBackground(new Background(new BackgroundImage(new Image("MainRootBackground.jpg"),
                BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
        listView.setItems(list);
        listView.getSelectionModel().clearSelection();
    }

    @Override
    public Pane getMainPane() {
        return stackPane;
    }

    /************************ EVENTS *************************/
    @FXML
    private void listViewMouseDoubleClicked(MouseEvent e){
        if(e.getClickCount() > 1){
            String s = listView.getSelectionModel().getSelectedItem();

            switch(s){
                case "Number Generator":
                    ChangeTheRoot change = new ChangeTheRoot();
                    if( change.initialize(Main.primaryStage, true, Main.NUMBER_GENERATOR_ROOT, "Number Generator", true, new NumberGeneratorController()) == false)
                        Dialogs.create().title("Error").masthead(null).message("Couldn't load the components!").showInformation();

                    change.doFadingTransition(stackPane, Main.fadeOutMillis, Main.fadeInMillis, true);
                    break;

                default:
                    break;
            }
        }
    }

}


编辑....固定版本:

public class MainRootController2 extends Controller implements Initializable {

    @FXML
    StackPane stackPane;
    @FXML
    ListView<String> listView;
    private ObservableList<String> list;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //making a list and setting a background
        list = FXCollections.observableArrayList("Number Generator", "Cards");

        listView.setBackground(new Background(new BackgroundImage(new Image("MainRootBackground.jpg"),
                BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
        listView.setItems(list);
        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                                    @Override
                                    public ListCell<String> call(ListView<String> list) {
                                        return new MouseClickListCell();
                                    }

        });
        listView.getSelectionModel().clearSelection();
    }

    @Override
    public Pane getMainPane() {
        return stackPane;
    }

    /************************ EVENTS *************************/
    private void fireUpTheFeature(String name){
        String s = listView.getSelectionModel().getSelectedItem();

        switch(s){
            case "Number Generator":
                ChangeTheRoot change = new ChangeTheRoot();
                if( change.initialize(Main.primaryStage, true, Main.NUMBER_GENERATOR_ROOT, "Number Generator", true, new NumberGeneratorController()) == false)
                    Dialogs.create().title("Error").masthead(null).message("Couldn't load the components!").showInformation();

                change.doFadingTransition(stackPane, Main.fadeOutMillis, Main.fadeInMillis, true);
                break;
            case "Cards":
                break;
            default:
                break;
        }

    }

    public class MouseClickListCell extends ListCell<String>
    {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if(empty) {
                setText(null);
                setOnMouseClicked(null);
            }
            else {
                setText(item.toString());
                setOnMouseClicked(new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        if(event.getClickCount() > 1){
                            fireUpTheFeature(getItem());
                        }
                    }
                });
            }
        }
    }
}

最佳答案

我看不到您的FXML文件,但是从您的命名来看,我可以猜测,只要单击您的private void listViewMouseDoubleClicked(MouseEvent e),就会触发您的控制器方法ListView

每当用户单击ListView区域中的任何位置时,都会触发此事件。您要删除该onMouseClicked处理程序并将该代码移到您的ListCell渲染器中。

如果将鼠标侦听器仅附加到非空单元格,则仅在单击ListCell时才会触发代码。

例如:

public static class MouseClickListCell<T> extends ListCell<T>
{
    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);

        if(empty) {
            setText(null);
            setOnMouseClicked(null);
        }
        else {
            setText(item.toString());
            setOnMouseClicked(ev -> doMagic());
        }
    }
}

10-04 17:39