*更新:我在底部包含了堆栈跟踪。
我正在尝试使用在此处找到的监听器选择ChoiceBox中的项目:https://gist.github.com/jewelsea/3062859
然而,我注意到当我检查新String(变量newChocie)的值时,我注意到它打印出一个数组 - 与我做出选择的次数相对应 - 我选择的是而不仅仅是一次性的选择。如何更正输出,以便该函数仅返回用户选择的单个实例?我在下面列出了我的代码,以及列表的内容。为清楚起见,我在下面列出了一个输出示例。谢谢。
private void PopulateBox() {
AliceBox.setItems(FXCollections.observableArrayList("Asset", "Liability", "Income", "Capital", "Expense"));
}
@FXML
private String SelectAccount() {
AliceBox.getSelectionModel().selectedItemProperty().addListener((new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> selected, String oldChoice, String newChoice) {
System.out.println(newChoice);
}
}));
return AcctIDString;
}
输出示例:
Asset
Liability
Liability
Income
Income
Income
Capital
Capital
Capital
Capital
Expense
Expense
Expense
Expense
Expense
堆栈追踪:
[java.lang.Thread.getStackTrace(Thread.java:1552),
GLMaintAcct.GLMaintAcctController$1.changed(GLMaintAcctController.java:195),
GLMaintAcct.GLMaintAcctController$1.changed(GLMaintAcctController.java:191),
com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361),
com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81),
javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176),
javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142),
javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112),
javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146), javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102),
javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:223),
javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149),
javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:403),
com.sun.javafx.scene.control.skin.ChoiceBoxSkin.lambda$addPopupItem$282(ChoiceBoxSkin.java:254),
com.sun.javafx.scene.control.skin.ChoiceBoxSkin$$Lambda$261/232574417.handle(Unknown Source),
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86),
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238),
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191),
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58),
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),
com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74),
com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49),
javafx.event.Event.fireEvent(Event.java:198),
javafx.scene.control.MenuItem.fire(MenuItem.java:462),
com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405),
com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358),
com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$232/766617116.handle(Unknown Source),
com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218),
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80),
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238),
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191),
com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59),
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58),
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56),
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56),
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56),
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),
com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74),
com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54),
javafx.event.Event.fireEvent(Event.java:198),
javafx.scene.Scene$MouseHandler.process(Scene.java:3758),
javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486),
javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762),
javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495),
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350),
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275),
java.security.AccessController.doPrivileged(Native Method),
com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385),
com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$194/2091612855.get(Unknown Source),
com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404),
com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384),
com.sun.glass.ui.View.handleMouseEvent(View.java:555),
com.sun.glass.ui.View.notifyMouse(View.java:927),
com.sun.glass.ui.win.WinApplication._runLoop(Native Method),
com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101),
com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source), java.lang.Thread.run(Thread.java:745)]
答案 0 :(得分:1)
您似乎没有在FXML初始化方法中添加侦听器,就像您在引用的原始示例中所做的那样。相反,每次用户选择ChoiceBox中的项目时,您都会调用一个方法来添加侦听器方法,这是不正确的。通常,您只需要添加一次监听器。