赞
踩
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();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。