2016-04-09 38 views
0

我正在使用JavaFX,我想實現一個過濾器來訪問數據庫中的數據。根據選中的複選框,我想執行特定的sql語句來從我的數據庫中檢索數據。問題是,如果我添加多個過濾器,我不知道如何管理它們。我不想爲每個組合編寫不同的選擇,因爲有太多的選擇。 enter image description hereJavaFX過濾器實現(如何處理多個布爾選項)

我覺得這是一個合乎邏輯的問題。有沒有辦法以某種方式存儲這些複選框,然後輕鬆地檢查它們是否被選中,並基於此,我希望能夠構建sql語句來檢索我想要的數據。

我只需要從頭開始,而不是代碼示例,也許是從網站上實現此目的的人開始的。

回答

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
真真真= 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

您可以將CheckBox es存儲在數組中,並將Function<Boolean, String>存儲在另一個數組/ List中,使用流將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(); 
}