我班上有一个听众。当插入比利时身份证时,它会被激活并将名称放在textField中。现在当发生这种情况时,程序还应该查看该名称是否已经插入数据库中,如果没有,则应该附加一个被设置为警告的上下文菜单。
当我试图附加上下文菜单时出错了,我知道代码可以工作,因为我一直在我的代码中的多个地方使用它。我收到以下错误:
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);
}
});
}
我希望这可用一小段代码解决,而且我不需要重写我程序的大部分内容,这要事先感谢。
答案 0 :(得分:2)
如果你将attachNotFound
的身体包裹起来......
Platform.runLater(new Runnable() {
@Override
public void run() {
...
}
});
......一切都会好的。
这确保了放在“...”上的代码将在JavaFX Thread上执行。 (来自attachNotFound的代码转到“......”的位置)