由于线程分离,Javafx无法附加contextmenu

由于线程分离,Javafx无法附加contextmenu,java,multithreading,user-interface,javafx,Java,Multithreading,User Interface,Javafx,我班上有一个听众。当插入比利时身份证并将姓名放入文本字段时,它看起来会被激活。现在,当这种情况发生时,程序还应该检查这个名称是否已经插入到数据库中,如果没有,它应该附加一个样式为警告的contextmenu 当我尝试附加contextmenu时出错,我知道代码可以工作,因为我在代码中的多个位置使用过它。我得到以下错误: 错误 听众 调用Validation.attachNotFound(text_id)时出错 未找到附件 publicstaticvoidattachnotfound(TextFi

我班上有一个听众。当插入比利时身份证并将姓名放入文本字段时,它看起来会被激活。现在,当这种情况发生时,程序还应该检查这个名称是否已经插入到数据库中,如果没有,它应该附加一个样式为警告的contextmenu

当我尝试附加contextmenu时出错,我知道代码可以工作,因为我在代码中的多个位置使用过它。我得到以下错误:

错误 听众 调用Validation.attachNotFound(text_id)时出错

未找到附件
publicstaticvoidattachnotfound(TextField){
field.setStyle(“-fx文本框边框:红色;-fx焦点颜色:红色;”);
MenuItem=新MenuItem(“未找到”);
ContextMenu=新建ContextMenu();
menu.getStyleClass().add(“错误”);
menu.setAutoHide(true);
menu.getItems().add(项);
菜单显示(字段,侧边,右侧,10,0);

field.focusedProperty().addListener((observeValue如果将
attachNotFound
的主体像

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        ...
    }
});
…一切都会好起来的


这确保了放在“…”上的代码将在JavaFX线程上执行。(attachNotFound中的代码将放在“…”

但我猜错误来自这一行:
menu.show(field,Side.RIGHT,10,0);
。所以用我提到的tequinique来包装这一行就足够了。:)我只是暂时将该方法包装在侦听器中,因为今天我必须在某处进行演示,但感谢您的澄清^^。
private final CardListener cl = new CardListener() {
        @Override
        public void cardInserted() {
            //de laadcirkel wordt zichtbaar wanneer het programma begint met een kaart te lezen.
            String fullName;
            System.out.println("card connected.");
            try {
                laadcirkel.setVisible(true);
                IDData data = id.getIDData();
                //voornaam ophalen, de format is nodig aangezien de reader de eerste naam en de tweede naam geeft.
                fullName = StringUtilities.format(data.get1stFirstname());

                //achternaam ophalen
                fullName = fullName + " " + data.getName();


                text_id.setText(fullName);
                text_id.autosize();

                CheckInOut c = lijst.getByCheckInName(text_id.getText());
                if (c != null) {
                    checkOut(c);
                } else {
                    Validation.attachNotFound(text_id);
                }
                laadcirkel.setVisible(false);

                //reset de layout om een visuele bug op te van in javafx.
                laadcirkel.getParent().getParent().setStyle("-fx-font: " + font + "px Tahoma;");

            } catch (EIDException ex) {
                Logger.getLogger(AanmeldPagina.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
public static void attachNotFound(TextField field) {
        field.setStyle("-fx-text-box-border: red ;-fx-focus-color: red ;");
        MenuItem item = new MenuItem("Not found");
        ContextMenu menu = new ContextMenu();
        menu.getStyleClass().add("error");
        menu.setAutoHide(true);
        menu.getItems().add(item);
        menu.show(field, Side.RIGHT, 10, 0);
        field.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
            if (newValue) {
                menu.show(field, Side.RIGHT, 10, 0);
            }
        });
    }
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        ...
    }
});