JavaFX学习 过滤table中的数据

分享中的代码,大多是借鉴或者直接从别人源码中拿过来的,大家一起分享下。

在表中实现数据过滤,他的代码很简单。有2个datalist,一个用于保存原始数据,另一个实时数据。监听text里面内容,每次从原始数据中获得想要的数据放入实时中。


package customUI.filterDataInTable;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class MainApp extends Application {

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Table Filtering");

try {
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("PersonTable.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
System.err.println("Error loading PersonTable.fxml!");
e.printStackTrace();
}
}

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




<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import jfxtras.labs.scene.control.*?>

<AnchorPane minWidth="315.0" prefHeight="300.0" prefWidth="315.0" xmlns:fx="http://javafx.com/fxml" fx:controller="customUI.filterDataInTable.PersonTableController">
<children>
<TableView fx:id="personTable" prefHeight="-1.0" prefWidth="-1.0" tableMenuButtonVisible="false" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="40.0">
<columns>
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="120.0" text="First Name" fx:id="firstNameColumn" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="120.0" text="Last Name" fx:id="lastNameColumn" />
</columns>
</TableView>
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<children>
<Label text="Filter Table:" />
<TextField fx:id="filterField" prefWidth="-1.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</AnchorPane>




package customUI.filterDataInTable;

public class Person {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}




package customUI.filterDataInTable;

import java.util.ArrayList;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class PersonTableController {
@FXML
private TextField filterField;
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;

private ObservableList<Person> masterData = FXCollections.observableArrayList();
private ObservableList<Person> filteredData = FXCollections.observableArrayList();

/**
* The constructor. The constructor is called before the initialize()
* method.
*/
public PersonTableController() {
// Add some sample data to the master data
masterData.add(new Person("Hans", "Muster"));
masterData.add(new Person("Ruth", "Mueller"));
masterData.add(new Person("Heinz", "Kurz"));
masterData.add(new Person("Cornelia", "Meier"));
masterData.add(new Person("Werner", "Meyer"));
masterData.add(new Person("Lydia", "Kunz"));
masterData.add(new Person("Anna", "Best"));
masterData.add(new Person("Stefan", "Meier"));
masterData.add(new Person("Martin", "Mueller"));

// Initially add all data to filtered data
filteredData.addAll(masterData);

// Listen for changes in master data.
// Whenever the master data changes we must also update the filtered data.
masterData.addListener(new ListChangeListener<Person>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Person> change) {
updateFilteredData();
}
});
}

/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
// Initialize the person table
firstNameColumn.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
lastNameColumn.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));

// Add filtered data to the table
personTable.setItems(filteredData);

// Listen for text changes in the filter text field
filterField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {

updateFilteredData();
}
});
}

/**
* Updates the filteredData to contain all data from the masterData that
* matches the current filter.
*/
private void updateFilteredData() {
filteredData.clear();

for (Person p : masterData) {
if (matchesFilter(p)) {
filteredData.add(p);
}
}

// Must re-sort table after items changed
reapplyTableSortOrder();
}

/**
* Returns true if the person matches the current filter. Lower/Upper case
* is ignored.
*
* @param person
* @return
*/
private boolean matchesFilter(Person person) {
String filterString = filterField.getText();
if (filterString == null || filterString.isEmpty()) {
// No filter --> Add all.
return true;
}

String lowerCaseFilterString = filterString.toLowerCase();

if (person.getFirstName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
return true;
} else if (person.getLastName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
return true;
}

return false; // Does not match
}

private void reapplyTableSortOrder() {
ArrayList<TableColumn<Person, ?>> sortOrder = new ArrayList(personTable.getSortOrder());
personTable.getSortOrder().clear();
personTable.getSortOrder().addAll(sortOrder);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值