解析 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中展示的文件
public class userSelectControll { @FXML private TextField selectbook; @FXML private Button commit; @FXML private Button backbtn; @FXML private TableColumn<book, String > pressCol; @FXML private TableColumn<book, Integer> idCol; @FXML private TableColumn<book, String > authorCol; @FXML private TableColumn<book, String > nameCol; @FXML private TableView<book> myTable; @FXML private TableColumn<book, Integer> borrowCol; @FXML private TableColumn<book, Float> priceCol; @FXML void commitClick(ActionEvent event) {

        String sb=selectbook.getText();
        System.out.println(sb);
        System.out.println("查找如下:"); if (bookDao.selectAllStudent(sb).isEmpty()==false){
            ArrayList<book> list = bookDao.selectAllStudent(sb);
            idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
            nameCol.setCellValueFactory(new PropertyValueFactory<>("bname"));
            authorCol.setCellValueFactory(new PropertyValueFactory<>("author"));
            pressCol.setCellValueFactory(new PropertyValueFactory<>("publish"));
            priceCol.setCellValueFactory(new PropertyValueFactory<>("bprice"));
            borrowCol.setCellValueFactory(new PropertyValueFactory<>("isborrow"));
            myTable.getItems().clear();//清空表格里面所有数据 myTable.getItems().addAll(list);
            userMenuApplication open1=new userMenuApplication(); try {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "未查找到此书!");
            alert.showAndWait(); try {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } @FXML void backbtnClick(ActionEvent event) throws Exception {
        userMenuApplication open = new userMenuApplication();
        open.start(new Stage());
        userSelectApplication.st.hide();//选择后开启一个新的窗体的同时,关闭当前窗体 }
}
  • 解析表格的使用
  1. 在图二设计视图的使用,每个表格对应的id和fxid 都要设置,并且要一致,Contrller class 要选择自己创建的xxxcontrller



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



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


#Java##程序员#
全部评论
楼主太牛了
点赞 回复 分享
发布于 2022-10-01 16:57 山西

相关推荐

hwwhwh:同双非,有大厂实习其实也没啥用,主要看运气,等就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务