【问题标题】:Javafx Unable to attach contextmenu due to separate thread由于单独的线程,Javafx 无法附加上下文菜单
【发布时间】:2016-08-26 23:43:48
【问题描述】:

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

当我尝试附加上下文菜单时出错了,我知道代码可以正常工作,因为我在代码中的多个位置都使用过它。我收到以下错误:

错误

java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
    at javafx.stage.Window.setShowing(Window.java:921)
    at javafx.stage.Window.show(Window.java:937)
    at javafx.stage.PopupWindow.showImpl(PopupWindow.java:454)
    at javafx.stage.PopupWindow.show(PopupWindow.java:399)
    at javafx.scene.control.ContextMenu.doShow(ContextMenu.java:287)
    at javafx.scene.control.ContextMenu.show(ContextMenu.java:262)
    at util.Validation.attachNotFound(Validation.java:140)
    at paginas.CheckOut$1.cardInserted(CheckOut.java:105)
    at be.belgium.eid.event.CardAlivePromptTask.run(CardAlivePromptTask.java:79)

监听器

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);
            }
        }

当我调用 Validation.attachNotFound(text_id); 时出错了

找不到附件

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);
            }
        });
    }

我希望这可以通过一小段代码来解决,并且我不必重写我的程序的大部分,在此先感谢。

【问题讨论】:

    标签: java multithreading user-interface javafx


    【解决方案1】:

    如果你将attachNotFound 的主体包裹起来,就像...

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            ...
        }
    });
    

    ...一切都会好起来的。

    这确保放置到“...”的代码将在 JavaFX 线程上执行。 (attachNotFound 中的代码位于“...”处)

    【讨论】:

    • 但我猜错误来自这一行:menu.show(field, Side.RIGHT, 10, 0);。所以用我提到的 tequiniqe 来包装这条线就足够了。 :)
    • 我现在只是将方法封装在侦听器中,因为我今天必须在某个地方进行演示,但感谢您的澄清^^。
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多