java - 按属性值动态过滤 ObservableList

标签 java javafx dynamic properties observable

我正在使用 JavaFX 编写日志读取器应用程序,并且我想根据所选属性实现基于搜索的过滤。 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;
                        }
                    }
                }
            }

    );

最佳答案

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

全面披露:我无论如何都不是 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();
    }
}
<小时/>

The Result:

screenshot

关于java - 按属性值动态过滤 ObservableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580649/

相关文章:

java - Java 中基于月份的日期选择器

c# - Asp.net 或 Silverlight 中的语法荧光笔

java - JasperReports 在导出 pdf 时不保留字体

Command+T 的 JavaFX 组合键(新标签)

html - 无法让动态图像与 css 一起正常工作

c# - 使用 Dynamic LINQ 过滤子查询

java - 修复此继承结构的最佳方法?

Java ChoiceDialog 更改默认按钮顺序

javascript - JavaFX : Set default printer for WebEngine/Webview

在运行时有条件地替换 C 函数