JAVAFX 上节课基础上的增删修查

设置 查询 按钮的点击事件 获得输入框中的值

根据输入框中的值去数据库做模糊查询,获取对应的学生集合

将该学生集合绑定到表格中

tring name = f1.getText();
List<Student> list = studentDao.list(name);
tableView.setItems(FXCollections.observableList(list));

完整代码

//定义全局变量
private StudentDao sudentDao = new StudentDao();
//搜索按钮的事件
b1.setOnAction(a -> {
String name = f1.getText();
List<Student> list = studentDao.list(name);
tableView.setItems(FXCollections.observableList(list));
});

封装方法

首页默认显示的数据就是模糊查询的 ""

studentDao.list("");

就是查询表格的全部数据,也就是【首页默认数据来源】

首页显示需要设置表格数据

@Override
public void start(Stage primaryStage) throws InterruptedException, Exception {
//………………此处省略大量代码
List<Student> list = studentDao.list("");//查询所有学生
tableView.setItems(FXCollections.observableList(list));//这是首页数据显示
//………………此处省略大量代码
}

后面制作其他功能页需要刷新表格数据,容易出现代码冗余 为了避免这种情况,我们可以选择封装一个方法,专门用于刷新该页面的表格数据

/**
* 此为刷新表格数据的方法
*
* @param name 模糊查询的关键字
* 如果是"" 则是可以理解为查询所有
*/
public void refreshTable(String name) {
//调用dao包进行数据查询
List<Student> list = studentDao.list(name);
//绑定数据到表格对象中
tableView.setItems(FXCollections.observableList(list));
}

修改首页数据读取为

refreshTable("");

修改 搜索 按钮事件为

refreshTable(name);

退出功能

当我们点击这个按钮的时候,应该弹出一个询问框,告知是否需要退出

Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "确定要退出?🧖");

点击了确定之后,才会进行页面的关闭,按照上节课提到的有关 Alert 弹框按钮的判断,完整代码如下

g4.setOnAction(a -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "确定要退出?🧖");
//启用弹出并接受用户点击的按钮
Optional<ButtonType> btn = alert.showAndWait();
//进行判断操作,如果是确定,则退出程序
if (btn.get() == ButtonType.OK) {
Platform.exit();
}
});

删除功能

这个功能需要与表格产生交互 点击这个按钮的时候会产生两种情况 选中的表格的行数据,只要了需要删除的人是谁 没有选中表格的行数据

TableView 提供了 getSelectionModel() 来获取一个选择模型,这个模型中提供了许多的方法,其中我们可以调用两个方法来获 得被选中的数据

tableView.getSelectionModel().getSelectedIndex();//获取被选中的行的下标
tableView.getSelectionModel().getSelectedItem();//获取被选中的行的数据

getSelectedItem() 是我们尤其需要注意的方法 这个方法获得的值取决于 TableView 定义时指定的泛型 可以直接拿到我们想要的数据【也就是遍历时对应的学生数据】

//此处对应的泛型是Student
TableView<Student> tableView = new TableView();
//可以直接拿到Student对象
Student stu = tableView.getSelectionModel().getSelectedItem();

 如果没有选中则为 null ,当我们拿到学生对象之后,执行删除的操作如下

判断是否选中学生【若是没有选中需要给出提示】

Student stu = tableView.getSelectionModel().getSelectedItem();
if (stu == null) {
Alert alert = new Alert(Alert.AlertType.ERROR, "请选择数据!!!");
alert.showAndWait();
return;
}

获取学生编号并调用 dao 包删除方法

lert alert = new Alert(Alert.AlertType.CONFIRMATION, "确定要删除该数据?┭┮﹏┭┮");
Optional<ButtonType> btn = alert.showAndWait();
if (btn.get() == ButtonType.OK) {
studentDao.del(stu.getId());
}

刷新数据

refreshTable("");

完整代码

//删除按钮的事件
g2.setOnAction(a -> {
Student stu = tableView.getSelectionModel().getSelectedItem();
if (stu == null) {
Alert alert = new Alert(Alert.AlertType.ERROR, "请选择数据!!!");
alert.showAndWait();
return;
}
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "确定要删除该数据?┭┮﹏┭┮");
Optional<ButtonType> btn = alert.showAndWait();
if (btn.get() == ButtonType.OK) {
studentDao.del(stu.getId());
refreshTable("");
}
});

增加功能 点击 增加 按钮后,需要新开页面,并在完成学生增加之后,刷新首页的数据 增加学生 页面

//新增按钮的事件
add.setOnAction(a -> {
//1.获取用户输入
String name = f1.getText();
String password = f2.getText();
String gender = "男";
String address = choiceBox.getValue().toString();
if (miss.isSelected()) {
gender = "女";
}
StringJoiner joiner = new StringJoiner(",");
if (c1.isSelected()) {
joiner.add(c1.getText());
}
if (c2.isSelected()) {
joiner.add(c2.getText());
}
if (c3.isSelected()) {
joiner.add(c3.getText());
}
String hobby = joiner.toString();
//2.完成学生数据封装
Student student = new Student(0, name, password, gender, address, hobby);
//3.调用dao
int i = studentDao.insert(student);
});

当数据库插入成功之后,需要对主页表格数据进行刷新,并且关闭新增界面 在B类如果需要调用A类对象的方法,我们可以将A类对象当作构造函数的参数传值给B类,那么在B类中,我们通过这个参数对象就可 以调用A类的方法了【 新增界面B 需要调用 首页A 的刷新表格的方法】 定义构造函数,参数中包含首页对象,同时为了方便调用,我们可以定义全局属性并在构造函数中完成赋值

private MyIndex myIndex;//首页对象
public MyInsert(MyIndex myIndex) {
this.myIndex = myIndex;
}

然后就可以在增加之后的判断中调用首页对象刷新数据的方法了

if(i>0){
myIndex.refreshTable("");
primaryStage.close();
}

首页按钮事件 注意观察,在新开增加页面的时候需要传入首页对象,代码如下

//增加按钮的事件
g1.setOnAction(a -> {
try {
new MyInsert(MyIndex.this).start(new Stage());
} catch (IOException e) {
e.printStackTrace();
}
});

修改功能的制作

点击 增加 按钮后,需要新开页面,并在页面上显示需要被修改的学生的数据,等待我们进行数据改动,并在点击按钮时对数据库按钮 进行更新 修改学生 页面

此处布局代码可以参考之前课程 当我们新开该页面的时候,我们需要对页面控件进行数据填充,也就意味着我们需要获取到当前表格中的数据并带到该界面来 我们可以使用 getSelectedItem() ,来获取到当前表格中选中的数据,获得的数据是一个 Student 对象 上面已经介绍了,可以看上 面

只需要将选中的学生数据带到这个页面,因为修改之后还需要刷新首页数据,所以我们设置该页面的构造函数

public class MyEdit extends Application {
private MyIndex myIndex;//首页对象,用于刷新数据表格
private Student student;//学生对象,用于进行数据设置
public MyEdit(MyIndex myIndex, Student student) {
this.myIndex = myIndex;
this.student = student;
}
}

然后就可以设置对应的控件设置值了

//设置名字框的值
f1.setText(student.getName());
//设置密码框的值
f2.setText(student.getPassword());
//设置性别框的值 性别框默认是选中男 所以我们只需要判断是否为女
if (student.getGender().equals("女")) {
miss.setSelected(true);
}
//是否包含爱好1
if (student.getHobby().contains(c1.getText())) {
c1.setSelected(true);
}
//是否包含爱好2
if (student.getHobby().contains(c2.getText())) {
c2.setSelected(true);
}
//是否包含爱好3
if (student.getHobby().contains(c3.getText())) {
c3.setSelected(true);
}
//设置下拉框的值
choiceBox.setValue(student.getAddress());

修改按钮的功能 与增加页面类似 获得控件值,封装对象 调用 dao 方法 此处一定要注意 该学生的id必须为首页传递过来的学生对象的编号

//修改按钮的事件
edit.setOnAction(a -> {
//1.获取用户输入
String name = f1.getText();
String password = f2.getText();
String gender = "男";
String address = choiceBox.getValue().toString();
if (miss.isSelected()) {
ender = "女";
}
StringJoiner joiner = new StringJoiner(",");
if (c1.isSelected()) {
joiner.add(c1.getText());
}
if (c2.isSelected()) {
joiner.add(c2.getText());
}
if (c3.isSelected()) {
joiner.add(c3.getText());
}
String hobby = joiner.toString();
//2.完成学生数据封装
Student stu = new Student(student.getId(), name, password, gender, address, hobby);
//3.调用dao
int i = studentDao.edit(stu);
if (i > 0) {
myIndex.refreshTable("");
primaryStage.close();
}
});

首页按钮事件 注意观察,在新开修改页面的时候需要传入首页对象,同时还需要判断用户是否选中了表格的行,代码如下

注意观察,在新开修改页面的时候需要传入首页对象,同时还需要判断用户是否选中了表格的行,代码如下
gender = "女";
}
StringJoiner joiner = new StringJoiner(",");
if (c1.isSelected()) {
joiner.add(c1.getText());
}
if (c2.isSelected()) {
joiner.add(c2.getText());
}
if (c3.isSelected()) {
joiner.add(c3.getText());
}
String hobby = joiner.toString();
//2.完成学生数据封装
Student stu = new Student(student.getId(), name, password, gender, address, hobby);
//3.调用dao
int i = studentDao.edit(stu);
if (i > 0) {
myIndex.refreshTable("");
primaryStage.close();
}
});
//修改按钮的事件
g3.setOnAction(a -> {
//获取表格选中的行数据
Student stu = tableView.getSelectionModel().getSelectedItem();
//如果没有选中
if (stu == null) {
Alert alert = new Alert(Alert.AlertType.ERROR, "请选择数据!!!");
alert.showAndWait();
return;
}
//新开修改页面
try {
new MyEdit(MyIndex.this, stu).start(new Stage());
} catch (IOException e) {
e.printStackTrace();
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值