简体   繁体   中英

Filter ObservableList dynamically by property value

I am writing a Log Reader application using JavaFX and I want to implement search-based filtering, based on the selected property. Application screenshot The values in the Combobox represents names of fields of objects, which populate the table. Now, I want to display only the rows (objects) whose property contains typed text.

My objects that populate the table look like this:

public class LogEvent {

private final SimpleStringProperty timestamp = new SimpleStringProperty("");
private final SimpleStringProperty level = new SimpleStringProperty("");
private final SimpleStringProperty emitter = new SimpleStringProperty("");
private final SimpleStringProperty message = new SimpleStringProperty("");
private final SimpleStringProperty thread = new SimpleStringProperty("");
private final SimpleStringProperty mdc = new SimpleStringProperty("");
private final SimpleStringProperty stackTrace = new SimpleStringProperty("");

public LogEvent(String timestamp, String level, String emitter, String message, String thread, String mdc, String stackTrace) {
    this.timestamp.set(timestamp);
    this.level.set(level);
    this.emitter.set(emitter);
    this.message.set(message);
    this.stackTrace.set(stackTrace);
    this.thread.set(thread);
    this.mdc.set(mdc);
}

public String getTimestamp() {
    return timestamp.get();
}

public String getLevel() {
    return level.get();
}

public String getEmitter() {
    return emitter.get();
}

public String getMessage() {
    return message.get();
}

public String getStackTrace() {
    return stackTrace.get();
}

public String getThread() {
    return thread.get();
}

public String getMdc() {
    return mdc.get();
}

}

Now, to filter the events I am trying to do something along the lines:

private void filterEvents(String text) {
    String property = filterCombo.getSelectionModel().getSelectedItem().toLowerCase();
    if (!property.isEmpty() && text != null) {
        FilteredList<LogEvent> filteredList = new FilteredList<>(events);
        filteredList.setPredicate(event -> event.getProperty(property).contains(text));
        tableView.setItems(filteredList);
        tableView.refresh();
    }
}

Of course there is no such method. I know I could use reflections, or write some switch, but that doesn't seem like a good idea. Is there some cleaner way to achieve this? For example something similar to how I'm creating table columns dynamically, using PropertyValueFactory:

for (String keyword : keywords) {
        TableColumn<LogEvent, String> column = new TableColumn<>(keyword);
        column.setCellValueFactory(new PropertyValueFactory<>(keyword.toLowerCase()));
        tableView.getColumns().add(column);
    }

Each row is color-coded by it's level severity.

tableView.setRowFactory(tableView -> new TableRow<>() {
                @Override
                protected void updateItem(LogEvent item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        switch (item.getLevel()) {
                            case LogLevel.ERROR:
                                setStyle("-fx-background-color: indianred;");
                                break;
                            case LogLevel.INFO:
                                setStyle("-fx-background-color: cornflowerblue;");
                                break;
                            case LogLevel.WARN:
                                setStyle("-fx-background-color: orange;");
                                break;
                            case LogLevel.DEBUG:
                                setStyle("-fx-background-color: lightblue;");
                                break;
                            case LogLevel.TRACE:
                                setStyle("-fx-background-color: ivory");
                                break;
                            case LogLevel.FATAL:
                                setStyle("-fx-background-color: firebrick");
                                break;
                            default:
                                setStyle("-fx-backgound-color: white;");
                                break;
                        }
                    }
                }
            }

    );

I am sure you have a few options, but here is one using Reflection. You did say that you were aware you could do so, but I'm not sure there's a way around it.

Full disclosure: I am not a Java expert by any means so this is likely not the best approach. It does work, however.


First, we need to update the LogEvent class to include a couple of helper methods:

import javafx.beans.property.SimpleStringProperty;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class LogEvent {

    private final SimpleStringProperty timestamp = new SimpleStringProperty("");
    private final SimpleStringProperty level = new SimpleStringProperty("");
    private final SimpleStringProperty emitter = new SimpleStringProperty("");
    private final SimpleStringProperty message = new SimpleStringProperty("");
    private final SimpleStringProperty thread = new SimpleStringProperty("");

    public LogEvent(String timestamp, String level, String emitter, String message, String thread) {
        this.timestamp.set(timestamp);
        this.level.set(level);
        this.emitter.set(emitter);
        this.message.set(message);
        this.thread.set(thread);

    }

    // Allows our filter functionality to retrieve a list of this object's Property objects
    public static List<String> getPropertiesList() {
        List<String> list = new ArrayList<>();

        // Let's loop through all our methods, looking for any that end in "Property"
        for (Method method : LogEvent.class.getMethods()) {
            String name = method.getName();

            // If it's a Property, return just the name of the property
            if (name.endsWith("Property")) {
                list.add(name.replace("Property", ""));
            }
        }
        return list;
    }

    public Object getPropertyByName(String propertyName) throws Exception {
        Method method = this.getClass().getMethod(propertyName + "Property", (Class[]) null);
        return method.invoke(this);
    }

    public String getTimestamp() {
        return timestamp.get();
    }

    public SimpleStringProperty timestampProperty() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp.set(timestamp);
    }

    public String getLevel() {
        return level.get();
    }

    public SimpleStringProperty levelProperty() {
        return level;
    }

    public void setLevel(String level) {
        this.level.set(level);
    }

    public String getEmitter() {
        return emitter.get();
    }

    public SimpleStringProperty emitterProperty() {
        return emitter;
    }

    public void setEmitter(String emitter) {
        this.emitter.set(emitter);
    }

    public String getMessage() {
        return message.get();
    }

    public SimpleStringProperty messageProperty() {
        return message;
    }

    public void setMessage(String message) {
        this.message.set(message);
    }

    public String getThread() {
        return thread.get();
    }

    public SimpleStringProperty threadProperty() {
        return thread;
    }

    public void setThread(String thread) {
        this.thread.set(thread);
    }
}

Now a simple program to demonstrate (comments in code):

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FilteredTableViewSample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // First, let's create some sample LogEvent data
        ObservableList<LogEvent> events = FXCollections.observableArrayList();
        events.add(new LogEvent("4:02 pm", "Warning", "Emitter #1", "You're gonna need a bigger boat!", "Thread-03"));
        events.add(new LogEvent("3:34 am", "Info", "Emitter #1", "Who you gonna call?", "Thread-05"));
        events.add(new LogEvent("4:02", "Warning", "Emitter #1", "Be excellent to each other!", "Thread-JFX"));
        events.add(new LogEvent("4:02", "Warning", "Emitter #1", "What we have here is a failure to communicate.", "Thread-JFX"));

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Add our ComboBox and filter TextField
        HBox filterPane = new HBox(5);
        filterPane.setPadding(new Insets(5));

        ComboBox<String> cboProperty = new ComboBox<>();
        cboProperty.setPromptText("Filter By...");
        cboProperty.setItems(FXCollections.observableList(LogEvent.getPropertiesList()));

        TextField txtFilter = new TextField();
        HBox.setHgrow(txtFilter, Priority.ALWAYS);
        filterPane.getChildren().addAll(cboProperty, txtFilter);

        // Just a standard TableView
        TableView<LogEvent> tableView = new TableView<>();
        TableColumn<LogEvent, String> colTimestamp = new TableColumn<>("Timestamp");
        TableColumn<LogEvent, String> colLevel = new TableColumn<>("Level");
        TableColumn<LogEvent, String> colEmitter = new TableColumn<>("Emitter");
        TableColumn<LogEvent, String> colMessage = new TableColumn<>("Message");
        TableColumn<LogEvent, String> colThread = new TableColumn<>("Thread");
        tableView.getColumns().addAll(colTimestamp, colLevel, colEmitter, colMessage, colThread);

        // Setup our CellValueFactories
        colTimestamp.setCellValueFactory(tv -> tv.getValue().timestampProperty());
        colLevel.setCellValueFactory(tv -> tv.getValue().levelProperty());
        colEmitter.setCellValueFactory(tv -> tv.getValue().emitterProperty());
        colMessage.setCellValueFactory(tv -> tv.getValue().messageProperty());
        colThread.setCellValueFactory(tv -> tv.getValue().threadProperty());

        FilteredList<LogEvent> filteredList = new FilteredList<>(events);
        tableView.setItems(filteredList);

        // Finally, we create our filter functionality
        txtFilter.textProperty().addListener((observable, oldValue, newValue) -> {

            final String searchString = newValue.toUpperCase();
            filteredList.setPredicate(logEvent -> {

                // If the search field is empty or no Filtered By value is selected, show all LogEvents
                if (searchString.isEmpty() || cboProperty.getSelectionModel().getSelectedItem() == null) {
                    return true;
                }

                // Now we just add our checks for each possible search field
                String filterBy = cboProperty.getSelectionModel().getSelectedItem();

                // We retrieve the text for the property we're looking for
                String targetString = "";
                try {
                    targetString = ((StringProperty) logEvent.getPropertyByName(filterBy)).getValue();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Now, return true if there's a match
                return targetString.toUpperCase().contains(searchString.toUpperCase());

            });

        });

        root.getChildren().addAll(filterPane, tableView);

        // Show the Stage
        primaryStage.setWidth(600);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

The Result:

截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM