2016-04-09 37 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(); 
}