当前位置:   article > 正文

JAVAFX控件——TableView数据的导入和插入(数据库)_javafx中的tableview新增数据按钮

javafx中的tableview新增数据按钮

在JavaFx应用中对创建表格最重要的TableView, TableColumnh和TableCell这三个类。

你可以通过实现数据模型(data model) 和 实现 单元格工厂(cell factory) 来填充表格。

表格类提供了表格列嵌入式的排序能力和必要时调整列宽度的功能。
下面开始学习表格类的相关内容。

1.创建一个表格  

先创建一个表格:

  1. public class table extends Application {
  2. public void start(Stage procedrue) throws Exception{
  3. //定义表格的行标
  4. TableView<Person> table = new TableView();
  5. TableColumn id = new TableColumn("id");
  6. TableColumn name = new TableColumn("name");
  7. TableColumn phone = new TableColumn("phone");
  8. TableColumn address = new TableColumn("Address");
  9. //表格列宽宽度设置
  10. id.setMinWidth(100);
  11. name.setMinWidth(100);
  12. phone.setMinWidth(100);
  13. address.setMinWidth(100);
  14. table.getColumns().addAll(id, name, phone, address);
  15. Scene scene = new Scene(table,400, 200);
  16. procedrue.setTitle("Table View Sample");
  17. procedrue.setScene(scene);
  18. procedrue.show();
  19. }
  20. }

 

主函数运行table类

import javafx.application.Application;

pub

lic class start_mai

    public static void main(String[] args) {
        Application.launch(table.class);
    }
}

运行后得到:

2.定义数据模型,导入数据到表中

注意数据类型要一致,否则会向表中导入失败

确定表中数据类型

  1. public class table extends Application {
  2. public void start(Stage procedrue) throws Exception{
  3. //向Student类中的Student函数中存入数据
  4. ObservableList<Student> data =
  5. FXCollections.observableArrayList(
  6. new Student(1, "小民", "jacob.smith@example.com","2222"),
  7. new Student(2, "始衰", "isabella.johnson@example.com","2222"),
  8. new Student(3, "随鼠", "ethan.williams@example.com","2222"),
  9. new Student(4, "从vv", "emma.jones@example.com","2222"),
  10. new Student(5, "Brown", "michael.brown@example.com","2222")
  11. );
  12. //表使用的数据
  13. TableView<Student> table = new TableView();
  14. //定义表格的行标
  15. TableColumn Id = new TableColumn("id");
  16. TableColumn name = new TableColumn("name");
  17. TableColumn phone = new TableColumn("phone");
  18. TableColumn address = new TableColumn("Address");
  19. //表格列宽宽度设置
  20. Id.setMinWidth(100);
  21. name.setMinWidth(100);
  22. phone.setMinWidth(100);
  23. address.setMinWidth(100);
  24. //确定数据导入的列
  25. Id.setCellValueFactory(
  26. new PropertyValueFactory<>("id"));
  27. name.setCellValueFactory(
  28. new PropertyValueFactory<>("name"));
  29. phone.setCellValueFactory(
  30. new PropertyValueFactory<>("phone"));
  31. address.setCellValueFactory(
  32. new PropertyValueFactory<>("address"));
  33. //向表中导入数据
  34. table.setItems(data);
  35. table.getColumns().addAll(Id, name, phone, address);
  36. Scene scene = new Scene(table,400, 200);
  37. procedrue.setTitle("Table View Sample");
  38. procedrue.setScene(scene);
  39. procedrue.show();
  40. }

 新建一个类,确定数据后,构造数据模型,注:数据模型数据必需跟表中数据类型一致

  1. /*定义数据模型*/
  2. public class Student {
  3. private final SimpleIntegerProperty id;
  4. private final SimpleStringProperty name;
  5. private final SimpleStringProperty phone;
  6. private final SimpleStringProperty address;
  7. Student(int id, String name, String phone, String address){
  8. this.id = new SimpleIntegerProperty(id);
  9. this.name = new SimpleStringProperty(name);
  10. this.phone = new SimpleStringProperty(phone);
  11. this.address = new SimpleStringProperty(address);
  12. }
  13. public int getId() {
  14. return this.id.get();
  15. }
  16. public void setId(int id) {
  17. this.id.set(id);
  18. }
  19. public String getName() {
  20. return this.name.get();
  21. }
  22. public void setName(String name) {
  23. this.name.set(name);
  24. }
  25. public String getPhone() {
  26. return this.phone.get();
  27. }
  28. public void setPhone(String phone) {
  29. this.phone.set(phone);
  30. }
  31. public String getAddress() {
  32. return this.address.get();
  33. }
  34. public void setAddress(String address) {
  35. this.address.set(address);
  36. }
  37. }

 运行main得结果图:

3.从数据库中导入数据到表格

1.数据库:

1.加载数据库驱动, 2.连接数据库, 3.得到数据库数据, 4. 把数据库导入到表中, 5. 关闭数据库

  1. public class table extends Application {
  2. String url = "jdbc:mysql://127.0.0.1:3306/fruitsystem?serverTimezone=UTC";
  3. String user = "root";
  4. String pwd = "123456";
  5. String jdbc = "com.mysql.cj.jdbc.Driver";
  6. ResultSet rst = null;
  7. Connection cont = null;
  8. Statement ppst = null;
  9. public void start(Stage procedrue) throws Exception{
  10. TableView<Student> table = new TableView();
  11. //定义表格的行标
  12. TableColumn Id = new TableColumn("id");
  13. TableColumn name = new TableColumn("name");
  14. TableColumn phone = new TableColumn("phone");
  15. TableColumn address = new TableColumn("Address");
  16. //表格列宽宽度设置
  17. Id.setMinWidth(100);
  18. name.setMinWidth(100);
  19. phone.setMinWidth(100);
  20. address.setMinWidth(100);
  21. //确定数据导入的列
  22. Id.setCellValueFactory(
  23. new PropertyValueFactory<>("id"));
  24. name.setCellValueFactory(
  25. new PropertyValueFactory<>("name"));
  26. phone.setCellValueFactory(
  27. new PropertyValueFactory<>("phone"));
  28. address.setCellValueFactory(
  29. new PropertyValueFactory<>("address"));
  30. //向表中导入数据
  31. //table.setItems(data);
  32. date( table, Id, name, phone, address);
  33. table.getColumns().addAll(Id, name, phone, address);
  34. Scene scene = new Scene(table,400, 200);
  35. procedrue.setTitle("Table View Sample");
  36. procedrue.setScene(scene);
  37. procedrue.show();
  38. }
  39. public void date(TableView table, TableColumn Id,
  40. TableColumn name, TableColumn phone, TableColumn address) {
  41. try {
  42. Class.forName(jdbc);
  43. }catch(ClassNotFoundException e) {
  44. e.printStackTrace();
  45. }
  46. String sql4 = "SELECT All(kh_id), kh_name, kh_phone, kh_address FROM kh_table";
  47. ObservableList<Student> data = FXCollections.observableArrayList();
  48. try {
  49. cont = DriverManager.getConnection(url, user, pwd);
  50. ppst = cont.createStatement();
  51. rst = ppst.executeQuery(sql4);
  52. //System.out.print("连接成功");
  53. while(rst.next()) {
  54. data.add(new
  55. Student(rst.getInt(1),rst.getString(2),rst.getString(3),rst.getString(4)));
  56. table.setItems(data);
  57. }
  58. }catch(Exception e) {
  59. e.printStackTrace();
  60. }finally {
  61. if(cont != null && ppst != null && rst != null) {
  62. try {
  63. cont.close();
  64. ppst.close();
  65. rst.close();
  66. }catch(Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. }
  72. }

运行效果图:

4.插入数据,显示到表中

1.在javafx界面上,新建几个文本框,和一个添加按钮,方便添加数据

  1. public class table extends Application {
  2. TableView<Student> table = new TableView();
  3. //定义表格的行标
  4. TableColumn Id = new TableColumn("id");
  5. TableColumn name = new TableColumn("name");
  6. TableColumn phone = new TableColumn("phone");
  7. TableColumn address = new TableColumn("Address");
  8. public void start(Stage procedrue) throws Exception{
  9. Pane rootNode = new Pane();
  10. ;
  11. //表格列宽宽度设置
  12. Id.setMinWidth(180);
  13. name.setMinWidth(180);
  14. phone.setMinWidth(180);
  15. address.setMinWidth(180);
  16. //确定数据导入的列
  17. Id.setCellValueFactory(
  18. new PropertyValueFactory<>("id"));
  19. name.setCellValueFactory(
  20. new PropertyValueFactory<>("name"));
  21. phone.setCellValueFactory(
  22. new PropertyValueFactory<>("phone"));
  23. address.setCellValueFactory(
  24. new PropertyValueFactory<>("address"));
  25. table.getColumns().addAll(Id, name, phone, address);
  26. table.setPrefSize(720, 570);
  27. //添加文本框实现增删改
  28. HBox hb = new HBox();
  29. TextField t_name = new TextField();
  30. t_name.setPrefSize(180,20);
  31. TextField t_phone = new TextField();
  32. t_phone.setPrefSize(180,20);
  33. TextField t_address = new TextField();
  34. t_address.setPrefSize(180, 20);
  35. Button bt = new Button("Refrsh");
  36. bt.setPrefSize(75, 20);
  37. hb.getChildren().addAll(t_name, t_phone, t_address, bt);
  38. hb.setPrefSize(720, 30);
  39. hb.setLayoutY(570);
  40. //向表中导入数据
  41. sql_operation sql = new sql_operation();
  42. //点击事件,点击添加数据
  43. sql.date( table);
  44. bt.setOnAction(e -> {
  45. try {
  46. new sql_operation().sqladd(table,t_name.getText(),
  47. t_phone.getText(), t_address.getText());
  48. } catch (ClassNotFoundException e1) {
  49. e1.printStackTrace();
  50. }
  51. });
  52. rootNode.getChildren().addAll(table, hb);
  53. Scene scene = new Scene(rootNode,720, 600);
  54. procedrue.setTitle("Table View Sample");
  55. procedrue.setScene(scene);
  56. procedrue.show();
  57. }
  58. }

2.创建一个class专门用来写sql的。

  1. public class sql_operation {
  2. //sql的基本参数 /连接数据库的IP及端口/数据库名字
  3. String url = "jdbc:mysql://127.0.0.1:3306/fruitsystem?serverTimezone=UTC";
  4. String user = "root";
  5. String pwd = "123456";
  6. String jdbc = "com.mysql.cj.jdbc.Driver";
  7. PreparedStatement pst = null;
  8. ResultSet rst = null;
  9. Connection cont = null;
  10. Statement ppst = null;
  11. //加载驱动
  12. public void jdbcload() throws ClassNotFoundException {
  13. Class.forName(jdbc);
  14. }
  15. //从数据库读取数据,并把数据导入到表格中
  16. public void date(TableView table) throws ClassNotFoundException {
  17. jdbcload();
  18. String sql4 = "SELECT All(kh_id), kh_name, kh_phone, kh_address FROM kh_table";
  19. //引用数据模型
  20. ObservableList<Student> data = FXCollections.observableArrayList();
  21. try {
  22. //连接数据库时需要用到数据库的名字,用户名,密码
  23. cont = DriverManager.getConnection(url, user, pwd);
  24. ppst = cont.createStatement();
  25. rst = ppst.executeQuery(sql4);
  26. //System.out.print("连接成功");
  27. while(rst.next()) {
  28. data.add(new Student(rst.getInt(1),rst.getString(2),rst.getString(3),rst.getString(4)));
  29. table.setItems(data);
  30. }
  31. }catch(Exception e) {
  32. e.printStackTrace();
  33. }finally {
  34. if(cont != null && ppst != null && rst != null) {
  35. try {
  36. cont.close();
  37. ppst.close();
  38. rst.close();
  39. }catch(Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }
  45. //增加数据
  46. public void sqladd(TableView table, String name,
  47. String phone, String address) throws ClassNotFoundException {
  48. jdbcload();
  49. String sql = "insert into kh_table (kh_name, kh_phone, kh_address) values(?,?,?)";
  50. try {
  51. cont = DriverManager.getConnection(url, user, pwd);
  52. pst = cont.prepareStatement("insert into kh_table (kh_name, kh_phone, kh_address) values(?,?,?)");
  53. pst.setString(1, name);
  54. pst.setString(2, phone);
  55. pst.setString(3, address);
  56. pst.execute();
  57. }catch(Exception e) {
  58. e.printStackTrace();
  59. }
  60. date(table);
  61. }
  62. }

 3.运行主函数参考图:

 参考:https://blog.csdn.net/Hanniel/article/details/78875503

第一篇博客,请多多关照, 希望能对你有所帮助

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/191649
推荐阅读
相关标签
  

闽ICP备14008679号