检测鼠标在SELECTION Editable ComboBox JavaFX上的单击 [英] Detect mouse click on SELECTION Editable ComboBox JavaFX

查看:46
本文介绍了检测鼠标在SELECTION Editable ComboBox JavaFX上的单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题乍看起来似乎很简单,但几天来我就已经遇到麻烦了.

The question may seem pretty easy at first, but I've already had troubles with it for a few days.

所以,我的问题是,当打开ComboBox选择并单击鼠标以选择选项时,我想检测鼠标单击和选择.

So, my problem is that I would like to detect mouse click AND the selection when the ComboBox selection is open and the mouse click is made to choose the option.

因此,它应该做的是检测所选内容上的鼠标单击"并获取所选值:

So, what it should do is detect the MOUSE CLICK on the selection and also get the selected value as well:

PS:我的ComboBox的代码可以在这里看到:在单击时选择JavaFX可编辑组合框文本

PS: The code for my ComboBox can be seen here: Select JavaFX Editable Combobox text on click

随时提出其他问题.

推荐答案

只需使用单元格工厂,然后向该单元格注册一个处理程序:

Just use a cell factory, and register a handler with the cell:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ComboBoxMouseClickOnCell extends Application {

    @Override
    public void start(Stage primaryStage) {
        ComboBox<String> combo = new ComboBox<>();
        combo.getItems().addAll("One", "Two", "Three");
        combo.setCellFactory(lv -> {
            ListCell<String> cell = new ListCell<String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    setText(empty ? null : item);
                }
            };
            cell.setOnMousePressed(e -> {
                if (! cell.isEmpty()) {
                    System.out.println("Click on "+cell.getItem());
                }
            });
            return cell ;
        });

        Scene scene = new Scene(new StackPane(combo), 300, 180);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于检测鼠标在SELECTION Editable ComboBox JavaFX上的单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆