Я уверен, что у вас есть несколько вариантов, но вот один, использующий Reflection.Вы действительно сказали, что знаете, что можете сделать это, но я не уверен, что есть способ обойти это.
Полное раскрытие: я ни в коем случае не эксперт по 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();
}
}
Результат: