2016-09-23 364 views
1

下面是演示代碼:JavaFX如何取消ComboBox選擇更改?

public class ComboBoxTest extends Application { 

    public static void main(String[] args) { 
     Application.launch(ComboBoxTest.class); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     ComboBox<String> comboBox = new ComboBox<>(); 
     comboBox.getItems().addAll("option1", "option2", "option3"); 
     comboBox.getSelectionModel().select(0); 

     comboBox.getSelectionModel().selectedItemProperty().addListener(
      (_ob, _old, _new) -> { 
       if (!isValidChange(_old, _new)) { 
        // ERROR: try to cancel change and get StackOverflowError 
        comboBox.getSelectionModel().select(_old); 
       } 
     }); 

     primaryStage.setScene(new Scene(
       new BorderPane(comboBox), 300, 200)); 
     primaryStage.show(); 
    } 

    private static boolean isValidChange(String _old, String _new) { 
     // return false; 
     return !_old.equals("option1"); 
    } 
} 

當改變的comboBox選擇,它centainly拋出StackOverFlowError,我知道爲什麼(在ChangeListener對象再次被觸發,並再次)的原因,但我不知道是如何正確「取消」此選擇更改操作。


更新:

對不起@fabian我這裏寫了一個壞榜樣,讓我改變一點點:

private static boolean isValidChange(String _old, String _new) { 
    return !_old.equals("option1"); 
} 

它現在的comboBox值不能改變。不過,我得到一個奇怪的IndexOutOfBoundsException這裏,雖然是在堆棧跟蹤未行我的代碼:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException 
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136) 
    at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242) 
    at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$177(ListViewBehavior.java:269) 
    at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88) 
    at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329) 
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73) 
    at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75) 
    at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:378) 
    at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403) 
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256) 
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220) 
    at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
    at javafx.event.Event.fireEvent(Event.java:198) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
    at com.sun.glass.ui.View.notifyMouse(View.java:937) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 

這是JavaFX的一個錯誤?

+1

更新後:外核層你需要做的就是將'comboBox.getSelectionModel()。select(_old);'包裝成'Platform.runLater(())'塊,例如'Platform.runLater(() - > comboBox.getSelectionModel() .select(_old));' – DVarga

+0

@DVarga現在有效,謝謝 – JSPDeveloper01

回答

0

這裏有2個問題。

  1. 從價值a改變重視b是無效的,而且從ba的改變是無效的。因此,當值更改時,監聽器將撤消導致監聽器嘗試撤消的另一個更改事件的更改,而監聽器試圖撤消該更改事件...

  2. 您修改選擇模型。然而,選擇模型依賴於選擇沒有改變。

要解決這些問題,引入ChangeListener「知道當它否決的變化」,並運行後的變化情況,用Platform.runLater處理的變化:

public abstract class VetoListener<T> implements ChangeListener<T> { 

    private final SelectionModel<T> selectionModel; 
    private boolean changing = false; 

    public VetoListener(SelectionModel<T> selectionModel) { 
     if (selectionModel == null) { 
      throw new IllegalArgumentException(); 
     } 
     this.selectionModel = selectionModel; 
    } 

    @Override 
    public void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) { 
     if (!changing && isInvalidChange(oldValue, newValue)) { 
      changing = true; 
      Platform.runLater(() -> { 
       selectionModel.select(oldValue); 
       changing = false; 
      }); 
     } 
    } 

    protected abstract boolean isInvalidChange(T oldValue, T newValue); 

} 

下面的代碼將會阻止變化"A""B""B""C"

@Override 
public void start(Stage primaryStage) { 
    ComboBox<String> combo = new ComboBox<>(FXCollections.observableArrayList("A", "B", "C")); 

    combo.getSelectionModel().selectedItemProperty().addListener(new VetoListener<String>(combo.getSelectionModel()) { 

     @Override 
     protected boolean isInvalidChange(String oldValue, String newValue) { 
      return oldValue != null && newValue != null 
        && !oldValue.isEmpty() 
        && !newValue.isEmpty() 
        && oldValue.charAt(0)+1 == newValue.charAt(0); 
     } 

    }); 

    Scene scene = new Scene(combo); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

謝謝!你解決了我的'StackOverflowError'問題,但現在有一個新問題,我更新了我的文章。 – JSPDeveloper01