2016-04-09 9 views
0

私はJavaFXを使用しています。データベースからデータを取得するフィルタを実装したいと思います。どのチェックボックスが選択されているかによって、特定のSQL文を実行してデータベースからデータを取得したい。問題は、複数のフィルタを追加すると、それらを管理する方法がわからないということです。私はそれぞれの組み合わせに対して別の選択肢を書いてはいけませんが、あまりにも多くあります。 enter image description hereJavaFXフィルタの実装(複数のブールオプションを処理する方法)

私は論理的な問題があるように感じます。それらのチェックボックスをどうにかして保存して、それが選択されているかどうかを簡単にチェックし、それに基づいて、私が望むデータを検索するSQL文を構築できるようにしたいのです。

私は、コード例ではなく、これをウェブサイトで実装した人から始めるには、ideeaが必要です。

答えて

0

バイナリを使用して各オプションを少し扱い、ユニークなステータスを取得する方法があります。非常に簡単な例は、(この例では、良い実装ではありませんのでご注意これは、可能性のあるアプローチの単なるデモンストレーションである。):

public static void main(String[] args) { 

     //each booleans represents an option (the state of one check box) 
     //this example simulates 3 options 
     boolean option1 = true; 
     boolean option2 = true; 
     boolean option3 = true; 

     //represent each boolean as binary (0 or 1) string 
     String binString1 = (option1) ? "1" : "0"; 
     String binString2 = (option2) ? "1" : "0"; 
     String binString3 = (option3) ? "1" : "0"; 

     //add all strings to one word 
     String binaryAsString = binString1+binString2+binString3; 

     //phrase it to int 
     // Use as radix 2 because it's binary 
     int number = Integer.parseInt(binaryAsString, 2); 

     //for 3 options (bits) you will get 8 different values, from 
     //0 (all false) to 7 (all true) 
     System.out.println(option1 + " "+ option2 + " "+ option3 + " = "+ number); 

    } 


出力リレーの例:
偽偽偽= 0
偽偽真= 2
真TRUE TRUE = 7

上記実証思想の可能な実装:

/** 
    *<pre> 
    * Converts an array of booleans, in the ordered entered, to a binary word, 
    * where each boolean represents one bit. The binary word is returned as int. 
    * <br>usage example: 
    * <br> boolToInt(false,true,false) returns 2(binary 010). 
    * <br> boolToInt(true,true,false) returns 6(binary 110). 
    * 
    *@param bols 
    *@throws IllegalArgumentException if bols is null or empty. 
    *@return 
    *<pre> 
    */ 
    private static int boolToInt(boolean ... bols) { 

     if((bols == null) || (bols.length == 0)) { 
      throw new IllegalArgumentException("Invalid (null or empty)input"); 
     } 

     StringBuilder sb = new StringBuilder(); 

     for(boolean b : bols) { 
      sb.append((b) ? "1" : "0" ); 
     } 

     return Integer.parseInt(sb.toString(), 2); 
    } 
0

あなたがList /別の配列にCheckBox、アレイ内のESとFunction<Boolean, String>を保存することができ、使用が条件にCheckBox状態からマッピングして、クエリを生成するためにCollectors.joiningを使用するストリーム。

例:

@Override 
public void start(Stage primaryStage) { 
    CheckBox[] boxes = new CheckBox[]{new CheckBox("a"), new CheckBox("b"), new CheckBox("c")}; 
    List<Function<Boolean, String>> clausesConstructor = Arrays.asList(
      b -> b ? "a >= 1" : null, 
      b -> b ? "b LIKE '%42'" : null, 
      b -> b ? "c" : null 
    ); 

    Button btn = new Button(); 
    btn.setText("Query"); 
    btn.setOnAction((ActionEvent event) -> { 
     List<String> list = IntStream.range(0, boxes.length) 
       .mapToObj(i -> clausesConstructor.get(i).apply(boxes[i].isSelected())) 
       .filter(Objects::nonNull).collect(Collectors.toList()); 

     System.out.println(list.isEmpty() ? "SELECT * FROM table" : list.stream().collect(Collectors.joining(") AND (", "SELECT * FROM table WHERE (", ")"))); 
    }); 
    VBox vbox = new VBox(10); 
    vbox.getChildren().addAll(boxes); 
    vbox.getChildren().add(btn); 
    Scene scene = new Scene(vbox); 

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