根据属性值动态过滤ObservableList

时间:2019-02-07 19:14:16

标签: java javafx dynamic properties observable

我正在使用JavaFX编写Log Reader应用程序,并且我想基于所选属性实现基于搜索的过滤。 Application screenshot 组合框中的值表示填充表的对象字段的名称。现在,我只想显示其属性包含键入文本的行(对象)。

我填充表格的对象看起来像这样:

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();
}

}

现在,要过滤事件,我将按照常规进行操作:

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();
    }
}

当然没有这种方法。我知道我可以使用反射或编写一些开关,但这似乎不是一个好主意。有没有更清洁的方法来实现这一目标?例如类似于使用PropertyValueFactory动态创建表列的方式:

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

每行均按照级别的严重程度进行颜色编码。

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;
                        }
                    }
                }
            }

    );

1 个答案:

答案 0 :(得分:1)

我确定您有几种选择,但这是使用反射的一种。您确实说过自己知道可以这样做,但是我不确定是否有解决办法。

完全公开:无论如何我都不是Java专家,所以这可能不是最好的方法。但是,它确实有效。


首先,我们需要更新LogEvent类以包括几个帮助程序方法:

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);
    }
}

现在有一个简单的程序来演示(代码中的注释):

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();
    }
}

  

结果:

screenshot