当前位置:   article > 正文

解析 FXML 表格的使用,图书列表功能的实现_fxml调用的流程图

fxml调用的流程图

解析 FXML 表格的使用,图书列表功能的实现

1.基本操作

  • 安装

首先要配置FXML,安装JavaFX Scene Builder ,这里可以参考网上教程

  • 创建

FXML使用的基本步骤(基本)

  1. xxx.fxml文件
  2. xxxApplication文件
  3. xxxControll文件

扩展

  1. 可以建立css文件,在文件中写自己的css样式

2.使用方法

  • 第一步: 选中fxml文件右击选择Open In SceneBuilder ,即可打开视图页面,如图

  • 第二步骤

然后选择tableview 创建表格,在设计完表格之后,选择view中show sample controller skeleton 复制代码,粘贴到xxxControll文件中

  • xxxControll中展示的文件
  1. public class userSelectControll {
  2. @FXML
  3. private TextField selectbook;
  4. @FXML
  5. private Button commit;
  6. @FXML
  7. private Button backbtn;
  8. @FXML
  9. private TableColumn<book, String > pressCol;
  10. @FXML
  11. private TableColumn<book, Integer> idCol;
  12. @FXML
  13. private TableColumn<book, String > authorCol;
  14. @FXML
  15. private TableColumn<book, String > nameCol;
  16. @FXML
  17. private TableView<book> myTable;
  18. @FXML
  19. private TableColumn<book, Integer> borrowCol;
  20. @FXML
  21. private TableColumn<book, Float> priceCol;
  22. @FXML
  23. void commitClick(ActionEvent event) {
  24. String sb=selectbook.getText();
  25. System.out.println(sb);
  26. System.out.println("查找如下:");
  27. if (bookDao.selectAllStudent(sb).isEmpty()==false){
  28. ArrayList<book> list = bookDao.selectAllStudent(sb);
  29. idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
  30. nameCol.setCellValueFactory(new PropertyValueFactory<>("bname"));
  31. authorCol.setCellValueFactory(new PropertyValueFactory<>("author"));
  32. pressCol.setCellValueFactory(new PropertyValueFactory<>("publish"));
  33. priceCol.setCellValueFactory(new PropertyValueFactory<>("bprice"));
  34. borrowCol.setCellValueFactory(new PropertyValueFactory<>("isborrow"));
  35. myTable.getItems().clear();//清空表格里面所有数据
  36. myTable.getItems().addAll(list);
  37. userMenuApplication open1=new userMenuApplication();
  38. try {
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }else{
  43. Alert alert = new Alert(Alert.AlertType.INFORMATION, "未查找到此书!");
  44. alert.showAndWait();
  45. try {
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. @FXML
  52. void backbtnClick(ActionEvent event) throws Exception {
  53. userMenuApplication open = new userMenuApplication();
  54. open.start(new Stage());
  55. userSelectApplication.st.hide();//选择后开启一个新的窗体的同时,关闭当前窗体
  56. }
  57. }
  • 解析表格的使用
  1. 在图二设计视图的使用,每个表格对应的id和fxid 都要设置,并且要一致,Contrller class 要选择自己创建的xxxcontrller

     

  1. 表格中:idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
  2. idCol 对应的是自己在图二设置的id,bid对应的是要展示数据库中的字段名称
  3. tableColumn中传入的泛型第一个是对象,第二个是数据类型,注意如果数据类型是int 要写成Integer 引用数据类型
  1. @FXML
  2. private TableColumn<book, Integer> borrowCol;
  1. 关联的是自己要打印在报个中的对象
  1. @FXML
  2. private TableView<book> myTable;
  1. 绑定的是图书表的字段名,通过这个方法,可以将获取到的数据库字段中的属性绑定到表格中
  1. idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
  1. 展示表格,注意这里要先清空在展示表格信息,不然内容会重复,list是一个集合,存放图书对应的数据
  1. myTable.getItems().clear();//清空表格里面所有数据
  2. myTable.getItems().addAll(list);
  1. book 基础包的封装,返回的是book类型的集合

     

这样就可以通过fxml展示一个表格,结果:

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

闽ICP备14008679号