2017-05-25 9 views
0

こんにちは、私はいくつかのコンボボックスに問題があります。私は4つのコンボボックスとのインターフェイスを持っており、同じリストをアイテムとして使用します。これらのコンボボックスの1つにリスナーを追加しました。リストを変更したときではなく、アイテムを選択したときにリスナーを実行したかったのですが、このリスナーでリストからアイテムを削除すると、自動的に実行されます。設定リスナーCombobox JavaFX

  class1.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) ->{ 

      System.out.println(oldValue); 
      System.out.println(newValue); 

      class1.getItems().add(oldValue); 
      class1.getItems().remove(newValue); 

      }); 

はそう終わりに、彼は私が削除を行うたびに、このリスナーを実行し、それはエラーで終了しますが観測リスト(選択モデルのselectedItems)しばらく変えてしまうので、結局

+0

は、その後、明らかに選択が変更する必要があります。ここで実際に何をしたいのですか? (なぜセレクションの変更のリストを変更していますか?) –

+0

comboBoxに表示したい値を選択しましたが、そのアイテムをリストから削除したいので、そのコンボを再度クリックします既に選択されているものを除くすべてのオプションとして表示されます –

答えて

0

問題は基本的に発生しますそのリストの変更が処理されています。これはAPIの欠陥の一種です。回避策(ハック)、再びそれを変更する前に、完全に最初の変更をできるようになるように、フィルタリングされたリストを使用し、Platform.runLater(...)で、リスト上の述語を更新することです:

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.collections.transformation.FilteredList; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ComboBoxNoSelectedItem extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ObservableList<String> allItems = FXCollections.observableArrayList("One", "Two", "Three", "Four", "Five"); 
     ComboBox<String> combo = new ComboBox<>(); 
     combo.setValue(allItems.get(0)); 
     FilteredList<String> items = allItems.filtered(item -> item != combo.getValue()); 
     combo.setItems(items); 

     combo.valueProperty().addListener((obs, oldValue, newValue) -> Platform.runLater(() -> items.setPredicate(item -> item != newValue))); 


     BorderPane root = new BorderPane(); 
     root.setTop(combo); 
     primaryStage.setScene(new Scene(root, 400, 400)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

たぶんクリーナーアプローチはちょうどにあります近いが、全く同じではない、UXで現在選択されている項目、表示セルを無効にする:あなたが選択した項目を削除した場合

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ComboBoxNoSelectedItem extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ObservableList<String> allItems = FXCollections.observableArrayList("One", "Two", "Three", "Four", "Five"); 
     ComboBox<String> combo = new ComboBox<>(allItems); 

     combo.setCellFactory(lv -> new ListCell<String>() { 

      { 
       disableProperty().bind(combo.valueProperty().isEqualTo(itemProperty())); 
      } 

      @Override 
      protected void updateItem(String item, boolean empty) { 
       super.updateItem(item, empty); 
       setText(item); 
      } 
     }); 

     BorderPane root = new BorderPane(); 
     root.setTop(combo); 
     primaryStage.setScene(new Scene(root, 400, 400)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

これはうまくいきますが、最初の2〜3回の変更で何らかの理由で他の要素も削除されます..しばらくしてからは完璧に動作しますが、リスト –

+0

FilteredList items = classi.filtered(item - > item!= class1.getValue()); class1.setItems(items); class1.valueProperty()。addListener((obs、oldValue、newValue) - > Platform.runLater() - > items.setPredicate(item - > item!= newValue)))); –

関連する問題