前言
javafx中提供了分页功能,对于数据量大的数据展示,分页比较有用。
一、分页效果
二、代码
1.FXML布局
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Pagination?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vbox" alignment="CENTER" prefHeight="603.0" prefWidth="540.0" spacing="20.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/23.0.1" fx:controller="com.example.tableviewdemo.TableTestController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<TableView fx:id="table" prefHeight="328.0" prefWidth="500.0">
<columns>
<TableColumn fx:id="check" prefWidth="75.0" text="#" />
<TableColumn fx:id="username" prefWidth="75.0" text="姓名" />
<TableColumn fx:id="age" prefWidth="75.0" text="年龄" />
<TableColumn fx:id="job" prefWidth="75.0" text="职业" />
</columns>
</TableView>
<Pagination fx:id="pagination" prefHeight="43.0" prefWidth="373.0" style="-fx-padding: 0;" />
</VBox>
2.Application
package com.example.tableviewdemo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.kordamp.bootstrapfx.BootstrapFX;
import java.io.IOException;
public class TableTestApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(TableTestApplication.class.getResource("table-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 620, 440);
scene.getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());
scene.getStylesheets().add("fxstyle.css");
stage.setTitle("Table test Demo!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
3.Controller
package com.example.tableviewdemo;
import com.example.tableviewdemo.Entity.Person;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class TableTestController implements Initializable {
private static final int PAGE_SIZE = 5;
@FXML
private TableColumn<Person, Boolean> check;
@FXML
private TableColumn<Person, String> username;
@FXML
private TableColumn<Person, Integer> age;
@FXML
private TableColumn<Person, String> job;
@FXML
private Pagination pagination;
@FXML
private TableView<Person> table;
@FXML
private VBox vbox;
@FXML
protected void onHelloButtonClick() {
System.out.println("table 测试!");
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
check.setCellValueFactory(new PropertyValueFactory("check"));
username.setCellValueFactory(new PropertyValueFactory("name"));
age.setCellValueFactory(new PropertyValueFactory("age"));
job.setCellValueFactory(new PropertyValueFactory("job"));
List<Person> data = getData();
table.setMinHeight(328);
check.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
@Override
public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> personStringTableColumn) {
return new TableCell<Person, Boolean>() {
private CheckBox checkBox = new CheckBox();
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
if(item != null) {
checkBox.setSelected(item);
}
setGraphic(checkBox);
// 监听CheckBox的选择事件
checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
// 更新表格中的数据
Person row = getTableView().getItems().get(getIndex());
row.setCheck(isNowSelected);
});
}
}
};
}
});
//分页
//计算总页数
int pageCount = data.size() % 10 == 0?data.size() / 10:data.size() / 10+1;
//pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
pagination.setPageCount(pageCount);
pagination.setCurrentPageIndex(0);
pagination.setPageFactory(pageIndex -> {
return createPage(pageIndex);
});
}
public List<Person> getData(){
List<Person> data = new ArrayList<>();
ClassLoader classLoader = TableTestController.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("data.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
Person person = new Person();
String[] split = line.split(",");
boolean checkVal = split[0].equals("true")?true:false;
person.setCheck(checkVal);
person.setName(split[1]);
person.setAge(Integer.parseInt(split[2]));
person.setJob(split[3]);
data.add(person);
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private VBox createPage(int pageIndex) {
System.out.println("pageIndex: "+pageIndex);
List<Person> data = getData();
List<Person> subList;
int end = pageIndex * 10 + 10;
if(end > data.size()){
end = data.size();
}
subList = data.subList(pageIndex * 10, end);
System.out.println("======================================");
for (Person p:subList) {
System.out.println(p.getName()+" "+p.getName()+" "+p.getAge()+" "+p.getJob());
}
ObservableList<Person> items = FXCollections.observableArrayList(subList);
table.setItems(items);
return new VBox(table);
}
}
public class Person {
private Boolean check;
private String name;
private Integer age;
private String job;
public Boolean getCheck() {
return check;
}
public void setCheck(Boolean check) {
this.check = check;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
css样式
.pagination {
-fx-border-color: #0E5D79;
}
.pagination .page {
-fx-background-color: #DDF1F8;
}
.pagination .pagination-control {
-fx-background-color: #C8C6C6;
}
.pagination .pagination-control .bullet-button {
-fx-background-color: transparent, #DDF1F8, #0E5D79, white, white;
}
.pagination .pagination-control .bullet-button:selected {
-fx-background-color: transparent, #DDF1F8, #0E5D79, white, #0E5D79;
}
.pagination .pagination-control .left-arrow, .right-arrow{
-fx-background-color: #DDF1F8, #0E5D79;
.table-row-cell:empty {
-fx-background-color: lightyellow;
}
.table-row-cell:empty .table-cell {
-fx-border-width: 0px;
}
测试数据data.txt
false,张三,20,土木工程
false,李四,23,机械工程
true,王五,21,汽车工程
false,赵六,23,AI工程
false,孙七,25,自动化工程
false,周八,26,工业工程
false,吴九,28,化工工程
false,郑十,29,工艺工程
false,小张,30,园艺工程
false,小李,32,医疗工程
false,小陈,23,航天工程
false,小王,23,航空工程
总结
javafx相对于Swing,功能更加丰富,样式比较友好,可以引入自定义CSS样式,功能更强大!