2016-08-31 25 views
0

私は本当にいくつかの助けを使うことができます。JavaFX接続のコンボボックス

私は2番目にproductCodeを選択した場合、productNameを選択する必要があるように、2つの接続されたコンボボックスを持つアプリケーションを作成しています。

両方のコンボボックステキストフィールドは、検索目的のためにフィルタリング可能です。

(ドロップダウンリストのレンダリングの目的で)このようにsetCellFactoriesを設定しました。

cbSifra.setCellFactory((comboBox) -> new ListCell<Product>() { 
     @Override 
     protected void updateItem(Product product, boolean empty) { 
      super.updateItem(product, empty); 

      if (product == null || empty) { 
       setText(null); 
      } else { 
       setText(product.getProductCode()); 
      } 
     } 
    }); 

cbNaziv.setCellFactory((comboBox) -> new ListCell<Product>() { 
     @Override 
     protected void updateItem(Product product, boolean empty) { 
      super.updateItem(product, empty); 

      if (product == null || empty) { 
       setText(null); 
      } else { 
       setText(product.getProductName()); 
      } 
     } 
    }); 

両方のコンボボックスは、データをコンボボックスに表示するコンバーターを実装しています。

cbNaziv.setConverter(new StringConverter<Product>() { 
     @Override 
     public String toString(Product product) { 
      if (product == null) { 
       return null; 
      } else { 
       return product.productNameProperty().get(); 
      } 
     } 

     @Override 
     public Product fromString(String productString) 
     { 
      return cbNaziv.getItems().stream().filter(item->productString.equals(item.getProductName())).findFirst().orElse(null); 

     } 
    }); 


    cbSifra.setConverter(new StringConverter<Product>() { 
     @Override 
     public String toString(Product product) { 
      if (product == null) { 
       return null; 
      } else { 
       return product.productCodeProperty().get(); 
      } 
     } 

     @Override 
     public Product fromString(String productString) 
     { 
      return cbSifra.getItems().stream().filter(item ->productString.equals(item.getProductCode())).findAny().orElse(null); 
     } 
    }); 

フィルタリングドロップダウンリストの

は次のようにtextProperty()でリスナーを使用して行われます:

cbNaziv.getEditor().textProperty().addListener((obs, oldValue, newValue) -> { 


       cbNaziv.show(); 

       final TextField editor = cbNaziv.getEditor(); 
       final Product selected = cbNaziv.getSelectionModel().getSelectedItem(); 

     /* 
     This needs run on the GUI thread to avoid the error described 
     here: https://bugs.openjdk.java.net/browse/JDK-8081700. 
     */ 

       Platform.runLater(() -> { 

      /* 
      If the no item in the list is selected or the selected item 
      isn't equal to the current input, we refilter the list. 
      */ 
        if (selected == null || !selected.equals(editor.getText())) { 
         filteredProductList.setPredicate(item -> { 
          // We return true for any items that contains the 
          // same letters as the input. We use toUpperCase to 
          // avoid case sensitivity. 

          if (item.getProductName().toUpperCase().contains(newValue.toUpperCase())) { 
           return true; 
          } else { 
           return false; 
          } 
         }); 
        } 
       }); 
      }); 



      cbSifra.getEditor().textProperty().addListener((obs, oldValue, newValue) -> { 


       cbSifra.show(); // Is used to open dropdown list as i start typing 

       final TextField editor = cbSifra.getEditor(); 
       final Product selected = cbSifra.getSelectionModel().getSelectedItem(); 

     /* 
     This needs run on the GUI thread to avoid the error described 
     here: https://bugs.openjdk.java.net/browse/JDK-8081700. 
     */ 

       Platform.runLater(() -> { 

      /* 
      If the no item in the list is selected or the selected item 
      isn't equal to the current input, we refilter the list. 
      */ 

        if (selected == null || !selected.equals(editor.getText())) { 

         filteredProductList.setPredicate(item -> { 
          // We return true for any items that contains the 
          // same letters as the input. We use toUpperCase to 
          // avoid case sensitivity. 

          if (item.getProductCode().toUpperCase().contains(newValue.toUpperCase())) { 
           return true; 
          } else { 
           return false; 
          } 

         }); 
        } 
       }); 
      }); 

私は値が選択されているかどうかを確認し、その値にいくつかのテキストフィールドを埋めたり、それらを設定するためにvaluePropertyリスナーを持っていますnullにする

cbSifra.valueProperty().addListener(new ChangeListener<Product>() { 
      @Override 
      public void changed(ObservableValue<? extends Product> observable, Product oldValue, Product newValue) { 

       if (cbSifra.getValue() == null || cbSifra.getValue().getProductName().isEmpty()) 
       { 
        cbNaziv.getSelectionModel().clearSelection(); 
        tfMpCijena.setText(null); 
        tfPopust.setText(null); 

       } else { 


        cbNaziv.setValue(cbSifra.getValue()); 
        cbSifra.setValue(cbNaziv.getValue()); 
        cbNaziv.hide(); 
        tfMpCijena.setText(cbSifra.getValue().getProductRetailPrice().toString()); 
        tfPopust.setText("0"); 
       } 

      } 
     }); 



     cbNaziv.valueProperty().addListener(new ChangeListener<Product>() { 
      @Override 
      public void changed(ObservableValue<? extends Product> observable, Product oldValue, Product newValue) { 

       if (cbNaziv.getValue() == null || cbNaziv.getValue().getProductName().isEmpty()) 
       { 
        cbSifra.getSelectionModel().clearSelection(); 
        tfMpCijena.setText(null); 
        tfPopust.setText(null); 

       } else { 

        cbSifra.setValue(cbNaziv.getValue()); 
        cbSifra.hide(); 
        tfMpCijena.setText(cbNaziv.getValue().getProductRetailPrice().toString()); 
        tfPopust.setText("0"); 
       } 

      } 
     }); 

の問題がある:私はコンボボックスに何かを入力し始めるとき

  • それは大丈夫フィルタリングし、 私は、ドロップダウンリストから項目を選択したときは、第2コンボボックスを埋めるが、 最初のコンボボックスエディタが再びフォーカスを取得しましたドロップダウンを表示する

  • コンボボックスからエントリを削除してもOKですが、もう1つは コンボボックス値remですains(削除されていません)

本当にありがとうございます。 ありがとうございます。

答えて

0

両方のComboBoxがProduct型であると仮定すると、双方向バインドを使用して、両方のComboBoxの値が常に同じ製品を指すようにすることができます。

cbNaziv.valueProperty().bindBidirectional(cbSifra.valueProperty()); 

これを使用すると、変更リスナーを削除できるようになり、うまくいけばいくつかの問題を修正できます。

関連する問題