我正在尝试实现一个分页 TableView,它允许按 JavaFX 中的所有项目进行排序。我从这里实现了分页 tableview:https://stackoverflow.com/a/25424208/12181863。由jewelsea 和tim buthe 提供。
我在想,因为表视图只访问项目的子列表,所以我想根据我在 Java 文档中关于排序的部分的理解,将排序从表列扩展到完整列表:{{ 3}}
// bind the sortedList comparator to the TableView comparator
//i m guessing it extends the sorting from the table to the actual list?
sortedList.comparatorProperty().bind(tableView.comparatorProperty());
然后刷新 tableview 以获取相同的子列表索引(由于整个列表已排序,因此现在应该对其进行排序)。
基本上,我想使用表格列比较器对完整列表进行排序,然后使用新的排序列表“刷新”tableview。这可行吗?或者有没有更简单的方法来解决这个问题?
我还参考了其他参考资料,例如:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.html#setItems-javafx.collections.ObservableList-,但我发现很难理解,因为所有内容都随处可见,而且解释含糊不清。
快速提取我的 TouchDisplayEmulatorController 类中的核心组件
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.util.ArrayList;
import java.util.List;
public class TouchDisplayEmulatorController extends Application {
public TableView sensorsTable;
public List<Sensor> sensors;
public int rowsPerPage = 14;
public GridPane grids = new GridPane();
public long timenow;
public void start(final Stage stage) throws Exception {
grids = new GridPane();
setGridPane();
Scene scene = new Scene(grids,1024,768);
stage.setScene(scene);
stage.setTitle("Table pager");
stage.show();
}
//public static void main(String[] args) throws Exception {
// launch(args);
//}
public void setGridPane(){
processSensors();
sensorsGrid();
}
public void sensorsGrid(){
buildTable();
int numOfPages = 1;
if (sensors.size() % rowsPerPage == 0) {
numOfPages = sensors.size() / rowsPerPage;
} else if (sensors.size() > rowsPerPage) {
numOfPages = sensors.size() / rowsPerPage + 1;
}
Pagination pagination = new Pagination((numOfPages),0);
pagination.setPageFactory(this::createPage);
pagination.setMaxPageIndicatorCount(numOfPages);
grids.add(pagination,0);
}
private Node createPage(int pageIndex) {
int fromIndex = pageIndex * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage,sensors.size());
sensorsTable.setItems(FXCollections.observableArrayList(sensors.subList(fromIndex,toIndex)));
return new BorderPane(sensorsTable);
}
public void processSensors(){
sensors = new ArrayList<>();
// long timenow = OffsetDateTime.now(ZoneOffset.UTC).toInstant().toEpochMilli()/1000;
// StringTokenizer hildetoken = new StringTokenizer(msg);
for (int i=0; i<20; i++) {
sensors.add(new Sensor(String.valueOf(i),"rid-"+i,"sid-"+i,"0","no condition"));
}
}
public void buildTable() {
sensorsTable = new TableView();
TableColumn<Sensor,String> userid = new TableColumn<>("userid");
userid.setCellValueFactory(param -> param.getvalue().userid);
userid.setPrefWidth(100);
TableColumn<Sensor,String> resourceid = new TableColumn<>("resourceid");
resourceid.setCellValueFactory(param -> param.getvalue().resourceid);
resourceid.setPrefWidth(100);
TableColumn<Sensor,String> column1 = new TableColumn<>("sid");
column1.setCellValueFactory(param -> param.getvalue().sid);
column1.setPrefWidth(100);
TableColumn<Sensor,String> column2 = new TableColumn<>("timestamp");
column2.setCellValueFactory(param -> param.getvalue().timestamp);
column2.setPrefWidth(100);
TableColumn<Sensor,String> column3 = new TableColumn<>("reading");
column3.setCellValueFactory(param -> param.getvalue().reading);
column3.setPrefWidth(100);
TableColumn<Sensor,String> column4 = new TableColumn<>("last contacted");
column4.setCellFactory(new Callback<TableColumn<Sensor,String>,TableCell<Sensor,String>>() {
@Override
public TableCell<Sensor,String> call(TableColumn<Sensor,String> sensorStringTableColumn) {
return new TableCell<Sensor,String>() {
public void updateItem(String item,boolean empty) {
super.updateItem(item,empty);
if (!isEmpty()) {
this.setTextFill(Color.WHITE);
if (item.contains("@")) {
this.setTextFill(Color.BLUEVIOLET);
} else if (item.equals("> 8 hour ago")) {
this.setStyle("-fx-background-color: red;");
} else if (item.equals("< 8 hour ago")) {
this.setStyle("-fx-background-color: orange;");
//this.setTextFill(Color.ORANGE);
} else if (item.equals("< 4 hour ago")) {
this.setStyle("-fx-background-color: yellow;");
this.setTextFill(Color.BLACK);
} else if (item.equals("< 1 hour ago")) {
this.setStyle("-fx-background-color: green;");
//this.setTextFill(Color.GREEN);
}
setText(item);
}
}
};
}
});
column4.setCellValueFactory(param -> param.getvalue().condition);
column4.setPrefWidth(100);
sensorsTable.getcolumns().addAll(userid,resourceid,column1,column2,column3,column4);
}
}
class Sensor {
public SimpleStringProperty userid;
public SimpleStringProperty resourceid;
public SimpleStringProperty sid;
public SimpleStringProperty timestamp;
public SimpleStringProperty reading;
public SimpleStringProperty condition;
public Sensor(String userid,String resourceid,String sid,String timestamp,String reading,String condition){
this.userid = new SimpleStringProperty(userid);
this.resourceid = new SimpleStringProperty(resourceid);
this.sid = new SimpleStringProperty(sid);
this.timestamp = new SimpleStringProperty(timestamp);
this.reading = new SimpleStringProperty(reading);
this.condition = new SimpleStringProperty(condition);
//we can use empty string or condition 3 here
}
public Sensor(String sid,String condition){
this.userid = new SimpleStringProperty("-1");
this.resourceid = new SimpleStringProperty("-1");
this.sid = new SimpleStringProperty(sid);
this.timestamp= new SimpleStringProperty(timestamp);
this.reading= new SimpleStringProperty(reading);
this.condition = new SimpleStringProperty(condition);
}
public String getUserid() { return this.userid.toString(); }
public String getResourceid() { return this.resourceid.toString(); }
public String getSid() { return this.sid.toString(); }
public String getTimestamp() { return this.timestamp.toString(); }
public String getReading() { return this.reading.toString(); }
public String getcondition() { return this.condition.toString(); }
public String toString() { return "userid: "+getUserid()+" resourceid: "+getResourceid()+" sid: "+getSid()+
"\ntimestamp: "+getTimestamp()+" reading: "+getReading()+" condition: "+getcondition();}
}
单独的类:
public class tester {
public static void main(String[] args) {
Application.launch(TouchDisplayEmulatorController.class,args);
}
}