Reputation: 15
I have a class named "MyObject" like this:
public class MyObject {
private int type;
private byte[] file;
private String fileName;
private String titolo;
public MyObject(String tit, int t, byte[] b, String name)
{
titolo= tit;
type = t;
file = b;
fileName = name;
}
int getType(){
return type;
}
String getfileName(){
return fileName;
}
byte[] getFile(){
return file;
}
String getTitolo(){
return titolo;
}
}
I have developed how to load the element of the list, now, I want to filter with FilterList. I would like to filter by "titolo" and "fileName". This is the other class where I'm working right now:
public class ContentTab extends VBox{
Label allegati;
TextField inseriscitesto;
ListView<MyObject> view;
ObservableList<MyObject> data;
public ContentTab(int width){
setPadding(new Insets(20));
setSpacing(10);
allegati = new Label("Allegati alla lezione");
allegati.setFont(Font.font("Verdana", 13));
inseriscitesto = new TextField("");
data = FXCollections.observableArrayList();
view = new ListView<MyObject>(data);
view.setStyle("-fx-background-color: transparent;");
setPrefWidth(width);
getChildren().addAll(allegati,inseriscitesto, view);
}
public void loadElement(List<MyObject> element){
for(int i = 0; i< element.size(); i++)
data.add(element.get(i));
view.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {
@Override
public ListCell<MyObject> call(ListView<MyObject> arg0) {
return new ListCell<MyObject>() {
@Override
protected void updateItem(MyObject item, boolean bln) {
super.updateItem(item, bln);
if (item != null) {
setStyle("-fx-background-color: transparent;");
VBox vBox = new VBox(new Text(item.getTitolo()), new Text(item.getfileName()));
ImageView placeHolder;
if(item.getType() == myMediaType.PDF){
placeHolder= new ImageView(new Image("img/pdf.jpg"));
placeHolder.setScaleX(0.5);
}
else
placeHolder= new ImageView(new Image("img/video.jpg"));
HBox hBox = new HBox(placeHolder, vBox);
hBox.setSpacing(10);
setGraphic(hBox);
}
}
};
}
});
}
public void FilterElement(List<MyObject> element){
FilteredList<MyObject> filteredData = new FilteredList<>(data, s -> true);
inseriscitesto.textProperty().addListener(obs->{
String filter = inseriscitesto.getText();
if(filter == null || filter.length() == 0) {
filteredData.setPredicate(s -> true);
}
else {
filteredData.setPredicate(s -> ((List<MyObject>) s.).contains(filter));
}
});
}
So, my question is, How can I filter by a property of MyObject in FilterElement
method?
Sorry in advance if I was not clear or the code is too much, but i think I put just the necessary to explain the question.
Upvotes: 0
Views: 520
Reputation: 6911
You should set the ListView
items to be a filtered list, and keep a reference to it, changing it's predicate as the text changes:
public class ContentTab extends VBox{
Label allegati;
TextField inseriscitesto;
ListView<MyObject> view;
public ContentTab(int width){
setPadding(new Insets(20));
setSpacing(10);
allegati = new Label("Allegati alla lezione");
allegati.setFont(Font.font("Verdana", 13));
inseriscitesto = new TextField("");
FilteredList<MyObject> filteredData = new FilteredList(FXCollections.observableArrayList(), s -> true);
view = new ListView<MyObject>(filteredDate);
inseriscitesto.textProperty().addListener(obs->{
String filter = inseriscitesto.getText();
if(filter == null || filter.length() == 0) {
filteredData.setPredicate(s -> true);
}
else {
filteredData.setPredicate(s -> s.getTitolo().contains(filter));
}
});
view.setStyle("-fx-background-color: transparent;");
setPrefWidth(width);
getChildren().addAll(allegati,inseriscitesto, view);
}
Also note that your predicate should be a function that takes a MyObject
and returns true
if it should be displayed, or false
if it should be filtered out. I'm guessing you wanted to check if the Titolo
property contains the filter text, but you may want to change it to another property.
Upvotes: 1