在JavaFx应用中对创建表格最重要的TableView, TableColumnh和TableCell这三个类。
你可以通过实现数据模型(data model) 和 实现 单元格工厂(cell factory) 来填充表格。
表格类提供了表格列嵌入式的排序能力和必要时调整列宽度的功能。
下面开始学习表格类的相关内容。
1.创建一个表格
先创建一个表格:
public class table extends Application {
public void start(Stage procedrue) throws Exception{
//定义表格的行标
TableView<Person> table = new TableView();
TableColumn id = new TableColumn("id");
TableColumn name = new TableColumn("name");
TableColumn phone = new TableColumn("phone");
TableColumn address = new TableColumn("Address");
//表格列宽宽度设置
id.setMinWidth(100);
name.setMinWidth(100);
phone.setMinWidth(100);
address.setMinWidth(100);
table.getColumns().addAll(id, name, phone, address);
Scene scene = new Scene(table,400, 200);
procedrue.setTitle("Table View Sample");
procedrue.setScene(scene);
procedrue.show();
}
}
主函数运行table类
import javafx.application.Application;
pub
lic class start_mai
public static void main(String[] args) {
Application.launch(table.class);
}
}
运行后得到:
2.定义数据模型,导入数据到表中
注意数据类型要一致,否则会向表中导入失败
确定表中数据类型
public class table extends Application {
public void start(Stage procedrue) throws Exception{
//向Student类中的Student函数中存入数据
ObservableList<Student> data =
FXCollections.observableArrayList(
new Student(1, "小民", "jacob.smith@example.com","2222"),
new Student(2, "始衰", "isabella.johnson@example.com","2222"),
new Student(3, "随鼠", "ethan.williams@example.com","2222"),
new Student(4, "从vv", "emma.jones@example.com","2222"),
new Student(5, "Brown", "michael.brown@example.com","2222")
);
//表使用的数据
TableView<Student> table = new TableView();
//定义表格的行标
TableColumn Id = new TableColumn("id");
TableColumn name = new TableColumn("name");
TableColumn phone = new TableColumn("phone");
TableColumn address = new TableColumn("Address");
//表格列宽宽度设置
Id.setMinWidth(100);
name.setMinWidth(100);
phone.setMinWidth(100);
address.setMinWidth(100);
//确定数据导入的列
Id.setCellValueFactory(
new PropertyValueFactory<>("id"));
name.setCellValueFactory(
new PropertyValueFactory<>("name"));
phone.setCellValueFactory(
new PropertyValueFactory<>("phone"));
address.setCellValueFactory(
new PropertyValueFactory<>("address"));
//向表中导入数据
table.setItems(data);
table.getColumns().addAll(Id, name, phone, address);
Scene scene = new Scene(table,400, 200);
procedrue.setTitle("Table View Sample");
procedrue.setScene(scene);
procedrue.show();
}
新建一个类,确定数据后,构造数据模型,注:数据模型数据必需跟表中数据类型一致
/*定义数据模型*/
public class Student {
private final SimpleIntegerProperty id;
private final SimpleStringProperty name;
private final SimpleStringProperty phone;
private final SimpleStringProperty address;
Student(int id, String name, String phone, String address){
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.phone = new SimpleStringProperty(phone);
this.address = new SimpleStringProperty(address);
}
public int getId() {
return this.id.get();
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return this.name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getPhone() {
return this.phone.get();
}
public void setPhone(String phone) {
this.phone.set(phone);
}
public String getAddress() {
return this.address.get();
}
public void setAddress(String address) {
this.address.set(address);
}
}
运行main得结果图:
3.从数据库中导入数据到表格
1.数据库:
1.加载数据库驱动, 2.连接数据库, 3.得到数据库数据, 4. 把数据库导入到表中, 5. 关闭数据库
public class table extends Application {
String url = "jdbc:mysql://127.0.0.1:3306/fruitsystem?serverTimezone=UTC";
String user = "root";
String pwd = "123456";
String jdbc = "com.mysql.cj.jdbc.Driver";
ResultSet rst = null;
Connection cont = null;
Statement ppst = null;
public void start(Stage procedrue) throws Exception{
TableView<Student> table = new TableView();
//定义表格的行标
TableColumn Id = new TableColumn("id");
TableColumn name = new TableColumn("name");
TableColumn phone = new TableColumn("phone");
TableColumn address = new TableColumn("Address");
//表格列宽宽度设置
Id.setMinWidth(100);
name.setMinWidth(100);
phone.setMinWidth(100);
address.setMinWidth(100);
//确定数据导入的列
Id.setCellValueFactory(
new PropertyValueFactory<>("id"));
name.setCellValueFactory(
new PropertyValueFactory<>("name"));
phone.setCellValueFactory(
new PropertyValueFactory<>("phone"));
address.setCellValueFactory(
new PropertyValueFactory<>("address"));
//向表中导入数据
//table.setItems(data);
date( table, Id, name, phone, address);
table.getColumns().addAll(Id, name, phone, address);
Scene scene = new Scene(table,400, 200);
procedrue.setTitle("Table View Sample");
procedrue.setScene(scene);
procedrue.show();
}
public void date(TableView table, TableColumn Id,
TableColumn name, TableColumn phone, TableColumn address) {
try {
Class.forName(jdbc);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
String sql4 = "SELECT All(kh_id), kh_name, kh_phone, kh_address FROM kh_table";
ObservableList<Student> data = FXCollections.observableArrayList();
try {
cont = DriverManager.getConnection(url, user, pwd);
ppst = cont.createStatement();
rst = ppst.executeQuery(sql4);
//System.out.print("连接成功");
while(rst.next()) {
data.add(new
Student(rst.getInt(1),rst.getString(2),rst.getString(3),rst.getString(4)));
table.setItems(data);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(cont != null && ppst != null && rst != null) {
try {
cont.close();
ppst.close();
rst.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
运行效果图:
4.插入数据,显示到表中
1.在javafx界面上,新建几个文本框,和一个添加按钮,方便添加数据
public class table extends Application {
TableView<Student> table = new TableView();
//定义表格的行标
TableColumn Id = new TableColumn("id");
TableColumn name = new TableColumn("name");
TableColumn phone = new TableColumn("phone");
TableColumn address = new TableColumn("Address");
public void start(Stage procedrue) throws Exception{
Pane rootNode = new Pane();
;
//表格列宽宽度设置
Id.setMinWidth(180);
name.setMinWidth(180);
phone.setMinWidth(180);
address.setMinWidth(180);
//确定数据导入的列
Id.setCellValueFactory(
new PropertyValueFactory<>("id"));
name.setCellValueFactory(
new PropertyValueFactory<>("name"));
phone.setCellValueFactory(
new PropertyValueFactory<>("phone"));
address.setCellValueFactory(
new PropertyValueFactory<>("address"));
table.getColumns().addAll(Id, name, phone, address);
table.setPrefSize(720, 570);
//添加文本框实现增删改
HBox hb = new HBox();
TextField t_name = new TextField();
t_name.setPrefSize(180,20);
TextField t_phone = new TextField();
t_phone.setPrefSize(180,20);
TextField t_address = new TextField();
t_address.setPrefSize(180, 20);
Button bt = new Button("Refrsh");
bt.setPrefSize(75, 20);
hb.getChildren().addAll(t_name, t_phone, t_address, bt);
hb.setPrefSize(720, 30);
hb.setLayoutY(570);
//向表中导入数据
sql_operation sql = new sql_operation();
//点击事件,点击添加数据
sql.date( table);
bt.setOnAction(e -> {
try {
new sql_operation().sqladd(table,t_name.getText(),
t_phone.getText(), t_address.getText());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
});
rootNode.getChildren().addAll(table, hb);
Scene scene = new Scene(rootNode,720, 600);
procedrue.setTitle("Table View Sample");
procedrue.setScene(scene);
procedrue.show();
}
}
2.创建一个class专门用来写sql的。
public class sql_operation {
//sql的基本参数 /连接数据库的IP及端口/数据库名字
String url = "jdbc:mysql://127.0.0.1:3306/fruitsystem?serverTimezone=UTC";
String user = "root";
String pwd = "123456";
String jdbc = "com.mysql.cj.jdbc.Driver";
PreparedStatement pst = null;
ResultSet rst = null;
Connection cont = null;
Statement ppst = null;
//加载驱动
public void jdbcload() throws ClassNotFoundException {
Class.forName(jdbc);
}
//从数据库读取数据,并把数据导入到表格中
public void date(TableView table) throws ClassNotFoundException {
jdbcload();
String sql4 = "SELECT All(kh_id), kh_name, kh_phone, kh_address FROM kh_table";
//引用数据模型
ObservableList<Student> data = FXCollections.observableArrayList();
try {
//连接数据库时需要用到数据库的名字,用户名,密码
cont = DriverManager.getConnection(url, user, pwd);
ppst = cont.createStatement();
rst = ppst.executeQuery(sql4);
//System.out.print("连接成功");
while(rst.next()) {
data.add(new Student(rst.getInt(1),rst.getString(2),rst.getString(3),rst.getString(4)));
table.setItems(data);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(cont != null && ppst != null && rst != null) {
try {
cont.close();
ppst.close();
rst.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
//增加数据
public void sqladd(TableView table, String name,
String phone, String address) throws ClassNotFoundException {
jdbcload();
String sql = "insert into kh_table (kh_name, kh_phone, kh_address) values(?,?,?)";
try {
cont = DriverManager.getConnection(url, user, pwd);
pst = cont.prepareStatement("insert into kh_table (kh_name, kh_phone, kh_address) values(?,?,?)");
pst.setString(1, name);
pst.setString(2, phone);
pst.setString(3, address);
pst.execute();
}catch(Exception e) {
e.printStackTrace();
}
date(table);
}
}
3.运行主函数参考图: