Я использую библиотеку Apache PDF Box, а затем, когда я извлекаю PDF-файл, из извлеченного PDF-файла появляется табличное представление, а затем, когда я нажимаю элемент управления f, появляется еще одно окно для глобального поиска, и это я хочу сделать, чтобы выполните поиск или покажите извлеченный PDF-файл.
Основной контроллер:
public void openFilterResultsWindow() {
try {
FXMLLoader loader = new FXMLLoader(getClass()) .getResource("/fxml/SecondaryView.fxml"));
Родительский корень = loader.load();
Код: Выделить всё
SecondaryController controller = loader.getController();
controller.setData(prepareSpellRowEntries());
Scene scene = new Scene(root);
Stage newStage = new Stage();
newStage.setTitle("Global Search");
newStage.setWidth(900); // Set the width to 800 pixels
newStage.setHeight(600);
newStage.setScene(scene);
newStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
private Thread startThread(File file) {
Task task = new Task() {
@Override
protected Void call() {
PdfDoc pdf = PDx.load(file.getAbsolutePath());
checker = new Intec(pdf, dictGroup, getSelectedVariant());
checker.performAnalysis();
Platform.runLater(() -> {
tabPane.getTabs().clear();
Map rowEntries = checker.tableRows();
fillTabPane(rowEntries);
List miscRowEntries = checker.miscRows();
fillMiscTab(miscRowEntries);
KeyCombinationSetup();
});
return null;
}
частный окончательный ObservableList masterList = FXCollections.observableArrayList();
частный окончательный FilteredList filteredList = новый FilteredList(masterList) ;
Код: Выделить всё
public void initialize() {
setupSpellTable();
setupSearchFunctionality();
noResultsContainer.setVisible(true);
}
private void setupSpellTable() {
// Word Column
TableColumn wordColumn = new TableColumn("Word");
wordColumn.setCellValueFactory(new PropertyValueFactory("word"));
wordColumn.setCellFactory(column -> new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
highlightMatchingText(this, item, empty, searchField.getText());
}
});
wordColumn.setPrefWidth(250);
// Suggestion Column
TableColumn suggestionColumn = new TableColumn("Suggestion");
suggestionColumn.setCellValueFactory(new PropertyValueFactory("suggestion"));
suggestionColumn.setCellFactory(column -> new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
highlightMatchingText(this, item, empty, searchField.getText());
}
});
suggestionColumn.setPrefWidth(300);
// Count Column
TableColumn countColumn = new TableColumn("Count");
countColumn.setCellValueFactory(new PropertyValueFactory("count"));
countColumn.setCellFactory(column -> new TableCell() {
});
countColumn.setPrefWidth(100);
// Locations Column
TableColumn locationsColumn = new TableColumn("Locations");
locationsColumn.setCellValueFactory(new PropertyValueFactory("locations"));
locationsColumn.setCellFactory(column -> new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
highlightMatchingText(this, item, empty, searchField.getText());
}
});
locationsColumn.setPrefWidth(300);
// Adding columns to the TableView
tableView.getColumns().addAll(wordColumn, suggestionColumn, countColumn, locationsColumn);
// Set the TableView to use the filtered list as its items
tableView.setItems(filteredList);
}
private void highlightMatchingText(TableCell cell, T item, boolean empty, String searchText) {
if (item == null || empty) {
cell.setText(null);
cell.setStyle("");
} else {
String itemText = item.toString();
if (!searchText.isEmpty() && itemText.toLowerCase().contains(searchText.toLowerCase())) {
cell.setText(itemText);
cell.setStyle("-fx-background-color: yellow;"); // Highlight matching text
} else {
cell.setText(itemText);
cell.setStyle(""); // Use default styling
}
}
}
private void setupSearchFunctionality() {
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
updateFilterPredicate(newValue);
checkForEmptyResults();
});
}
private void updateFilterPredicate(String searchText) {
filteredList.setPredicate(spellRowEntry -> {
if (searchText == null || searchText.isEmpty()) {
return true; // Show all items if search text is empty
}
String lowerCaseSearchText = searchText.toLowerCase();
// Adjust these checks based on your actual data structure
return (spellRowEntry.getWord() != null && spellRowEntry.getWord().toLowerCase().contains(lowerCaseSearchText))
|| (spellRowEntry.getSuggestion() != null && spellRowEntry.getSuggestion().toLowerCase().contains(lowerCaseSearchText))
|| (spellRowEntry.getLocations() != null && spellRowEntry.getLocations().toLowerCase().contains(lowerCaseSearchText))
|| String.valueOf(spellRowEntry.getCount()).contains(searchText);
});
}
private void checkForEmptyResults() {
boolean hasResults = !filteredList.isEmpty();
tableView.setVisible(hasResults);
noResultsContainer.setVisible(!hasResults);
if (!hasResults) {
noResultsLabel.setText("No results found for \"" + searchField.getText() + "\"");
}
}
public void setData(List spellEntries) {
masterList.clear();
masterList.addAll(spellEntries);
// Optionally, if you need to refresh the view or reset the search field
searchField.setText("");
tableView.refresh(); // Refresh the TableView to display the new data
}
Источник: https://stackoverflow.com/questions/781 ... her-window