JavaFX 表格Tableview读取显示数据以及显示按钮

FXML

 <TableView fx:id="table" prefHeight="500.0" prefWidth="1139.0" HBox.hgrow="ALWAYS">
                         <columns>
                             <TableColumn fx:id="ID" prefWidth="75.0" text="ID" />
                             <TableColumn fx:id="Username" prefWidth="153.0" text="用户名" />
                             <TableColumn fx:id="Password" prefWidth="154.0" text="密码" />
                             <TableColumn fx:id="Admin" prefWidth="73.0" text="管理员" />
                             <TableColumn fx:id="Time" prefWidth="155.0" text="注册时间" />
                             <TableColumn fx:id="Tel" prefWidth="179.0" text="联系号码" />
                             <TableColumn fx:id="Email" prefWidth="169.0" text="邮箱" />
                             <TableColumn fx:id="Cz" prefWidth="185.0" text="操作">
                                <columns>
                                     <TableColumn fx:id="Bj" prefWidth="90.0" text="编辑" />
                                     <TableColumn fx:id="Sc" prefWidth="90.0" text="删除" />
                                </columns>
                            </TableColumn>
                        </columns>
 </TableView>

Controller

 //前面创建文件生成的代码省略
 private ObservableList<UserLoad> list = FXCollections.observableArrayList();

 private void showList(){

    list = FXCollections.observableArrayList();
    //每次显示清空一次列表
    list.clear();
    try {
    
        //链接数据库查询
        List<UserLoad> UserLoad = MybatisUtil.getSqlSession().getMapper(UserDao.class).getUserList();
        
        //循环 
        for (UserLoad user : UserLoad) {

            list.add(user);  //list添加值对象
        }

    } finally {
        MybatisUtil.closeSqlSession();

    }
    
    //映射数据进每列
    ID.setCellValueFactory(new PropertyValueFactory("id"));
    Username.setCellValueFactory(new PropertyValueFactory("username"));
    Password.setCellValueFactory(new PropertyValueFactory("password"));
    Admin.setCellValueFactory(new PropertyValueFactory("admin"));
    Time.setCellValueFactory(new PropertyValueFactory("registrationdate"));
    Tel.setCellValueFactory(new PropertyValueFactory("telphone"));
    Email.setCellValueFactory(new PropertyValueFactory("email"));
    
     //添加按钮进列表
    Bj.setCellFactory((col)->{
    
                //UserLoad换成你自己的实体名称
                TableCell<UserLoad, String> cell = new TableCell<UserLoad, String>(){
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        button1 = new JFXButton("编辑");
                        button1.setStyle("-fx-background-color: #00bcff;-fx-text-fill: #ffffff");

                        button1.setOnMouseClicked((col) -> {

                            //获取list列表中的位置,进而获取列表对应的信息数据
                            UserLoad userLoad1 = list.get(getIndex());
                            //按钮事件自己添加

                        });

                        if (empty) {
                            //如果此列为空默认不添加元素
                            setText(null);
                            setGraphic(null);
                        } else {
                            this.setGraphic(button1);
                        }
                    }
                };
                return cell;
            }
    );

    Sc.setCellFactory((col)->{
                TableCell<UserLoad, String> cell = new TableCell<UserLoad, String>(){

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        //按钮显示文字
                        button2 = new JFXButton("删除");
                        //设置按钮颜色
                        button2.setStyle("-fx-background-color: #00bcff;-fx-text-fill: #ffffff");
                        //按钮点击事件
                        button2.setOnMouseClicked((col) -> {
                         //获取list列表中的位置,进而获取列表对应的信息数据
                            UserLoad userLoad2 = list.get(getIndex());
                           //按钮事件自己添加
                        });

                        if (empty) {
                            //如果此列为空默认不添加元素
                            setText(null);
                            setGraphic(null);
                        } else {
                           //加载按钮
                            this.setGraphic(button2);
                        }
                    }



                };
                return cell;
            }
    );
    所有项目添加进list
    table.setItems(list);
}
  //点击查询显示列表
    public void cx(ActionEvent event) {
    showList();
   
    }

在这里插入图片描述

  • 15
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要使用哈希表给JavaFXTableView填入数据,可以按照以下步骤: 1. 创建一个哈希表,用于存储数据。例如: ```java HashMap<String, String> data = new HashMap<>(); data.put("name", "John"); data.put("age", "30"); data.put("city", "New York"); ``` 2. 创建一个ObservableList,用于存储TableView的行数据。ObservableList可以自动触发TableView的更新操作。 ```java ObservableList<HashMap<String, String>> rows = FXCollections.observableArrayList(); ``` 3. 将哈希表添加到ObservableList中。 ```java rows.add(data); ``` 4. 创建TableView的列,并将哈希表中的键和分别绑定到列的CellValueFactory和textProperty中。 ```java TableColumn<HashMap<String, String>, String> nameCol = new TableColumn<>("Name"); nameCol.setCellValueFactory(new MapValueFactory<>("name")); nameCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().get("name"))); TableColumn<HashMap<String, String>, String> ageCol = new TableColumn<>("Age"); ageCol.setCellValueFactory(new MapValueFactory<>("age")); ageCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().get("age"))); TableColumn<HashMap<String, String>, String> cityCol = new TableColumn<>("City"); cityCol.setCellValueFactory(new MapValueFactory<>("city")); cityCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().get("city"))); ``` 5. 将列添加到TableView中。 ```java TableView<HashMap<String, String>> table = new TableView<>(); table.setItems(rows); table.getColumns().addAll(nameCol, ageCol, cityCol); ``` 6. 最后,将TableView添加到场景中。 ```java Scene scene = new Scene(table); primaryStage.setScene(scene); primaryStage.show(); ``` 这样,就可以使用哈希表给JavaFXTableView填入数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值