如何在VBox中过滤和显示

时间:2019-02-23 06:33:32

标签: javafx scenebuilder

谁能建议我该怎么做才能在VBox中显示动态模板。下面是我的代码,我想将sortedlist包装到SearchVBox中,以便它可以显示用户搜索的动态模板。在最底层,我提供了我需要做的事的例子。

public class UserSearchController implements Initializable {

    @FXML
    private TextField filterField;   
    @FXML
    private VBox SearchVbox;

    private static final String productFilePath = "C:\\Users\\Cre8\\Desktop\\APU prgram\\APU sem3\\OODJ\\Product.txt";
    List<CSVproduct> csvProducts = new ArrayList<>();
    ObservableList<CSVproduct> data = FXCollections.observableArrayList(csvProducts);
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        try {
            addUserSearchNewPane();
        } catch (IOException ex) {
            Logger.getLogger(ViewProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

    void addUserSearchNewPane() throws IOException{
        Reader reader = Files.newBufferedReader(Paths.get(productFilePath));
        CsvToBean<CSVproduct> csvToBean = new CsvToBeanBuilder(reader)
                .withType(CSVproduct.class)
                .withIgnoreLeadingWhiteSpace(true)
                .build();
        csvProducts = csvToBean.parse();
        for (CSVproduct csvproduct : csvProducts) {
        UserSearchTemplateController usersearchTC = new UserSearchTemplateController();
        System.out.println(csvproduct.getProductName());
        usersearchTC.setSearchName(csvproduct.getProductName());
        usersearchTC.setSearchPrice(csvproduct.getProductPrice());
        usersearchTC.setSearchQuantity(csvproduct.getProductQuantity());
        usersearchTC.setSearchRate(csvproduct.getProductRate());
        usersearchTC.setSearchDescription(csvproduct.getProductDescription());
        usersearchTC.setSearchImage(csvproduct.getProductImage());
        this.SearchVbox.getChildren().add(usersearchTC);
        }
    }
    void UserFilterFunction(){
        FilteredList<CSVproduct> filteredData = new FilteredList<>(data, p -> true);
        filterField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(csvproduct -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String lowerCaseFilter = newValue.toLowerCase();

                if (csvproduct.getProductName().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
                } 
                return false;
            });
        });
        SortedList<CSVproduct> sortedData = new SortedList<>(filteredData);
        this.SearchVbox.getChildren().add(sortedData);//how do i show the sortedlist insde the SearchVbox.
    }
}

最后一行代码不起作用。 Like this example to filter and show the dynamic VBox template

0 个答案:

没有答案