用户管理业务是针对管理员角色开展的,管理员对注册的用户管理,即禁用与启用
我的实现逻辑是在设计一个表格。将除管理员以外的其他用户,选择适宜的字段信息展示在表格中,因为管理员只对用户有启用/禁用的业务,因此我这里设计是字段是:id、name、status
我们使用Scene Builder开发页面
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="LeaveMessageSystem.controller.UserTypeManageFrameController"
prefHeight="700.0" prefWidth="800.0">
<children>
<VBox alignment="CENTER" prefHeight="700.0" prefWidth="800.0">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="用户注册管理">
<font>
<Font name="System Bold" size="40.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="296.0" prefWidth="600.0">
<children>
<TableView fx:id="UserTypeManageView" prefWidth="605.0" prefHeight="399.0" tableMenuButtonVisible="true">
<columns>
<TableColumn fx:id="idTableColumn" prefWidth="197.0" text="编号"/>
<TableColumn fx:id="userTypeNameColumn" minWidth="0.0" prefWidth="201.0" text="用户昵称"/>
<TableColumn fx:id="userTypeStatus" prefWidth="201.0" text="用户状态"/>
</columns>
</TableView>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="263.0" prefWidth="600.0">
<children>
<VBox fx:id="formVbox" prefHeight="240.0" prefWidth="602.0">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="编号:"/>
<TextField fx:id="idTextField" prefHeight="30.0" prefWidth="165.0"/>
<Label text="用户昵称:"/>
<TextField fx:id="userTypeNameField" prefHeight="30.0" prefWidth="174.0" />
</children>
<padding>
<Insets left="40.0"/>
</padding>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="10.0">
<children>
<Label text="用户状态:" />
<TextField fx:id="userTypeStatusField" prefHeight="80.0" prefWidth="165.0" />
</children>
<padding>
<Insets left="40.0"/>
</padding>
</HBox>
<HBox alignment="CENTER" prefWidth="100.0" prefHeight="200.0" spacing="100.0">
<children>
<Button fx:id="alterButton" mnemonicParsing="false" onAction="#do_alterButtom_event" text="修改"/>
</children>
<padding>
<Insets left="40.0"/>
</padding>
</HBox>
</children>
</VBox>
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
效果图如下
在controller新建java文件,用于处理用户管理的逻辑
将该逻辑细分为三个颗粒度更小的三个小事件
- 初始化页面数据
- 选中行后将数据显示到下面文本框中
- 修改事件,即对用户实现启用或禁用
package LeaveMessageSystem.controller;
import LeaveMessageSystem.beans.UserTypeNewBeanData;
import LeaveMessageSystem.dao.UserDao;
import LeaveMessageSystem.tools.SimpleTools;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import java.sql.SQLException;
import java.util.List;
/**
* 用户注册管理
*/
public class UserTypeManageFrameController {
@FXML
public TableView<UserTypeNewBeanData> UserTypeManageView;
@FXML
public TableColumn<UserTypeNewBeanData,String> idTableColumn;
@FXML
public TableColumn<UserTypeNewBeanData,String> userTypeNameColumn;
@FXML
public TableColumn<UserTypeNewBeanData, String> userTypeStatus;
@FXML
public VBox formVbox;
@FXML
public TextField idTextField;
@FXML
public TextField userTypeNameField;
@FXML
public TextField userTypeStatusField;
@FXML
public Button checkButton;
@FXML
public Button alterButton;
private SimpleTools simpleTools=new SimpleTools();
private UserDao userDao=new UserDao();
/**
* 修改事件,即对用户实现启用或禁用
* @param actionEvent
*/
public void do_alterButtom_event(ActionEvent actionEvent) {
//获取到用户输入内容
String id=idTextField.getText();
String status=userTypeStatusField.getText();
if(status==null||status.equals("")){
simpleTools.informationDalog(Alert.AlertType.ERROR, "提示", "错误", "不可为空!");
return;
}
//实例化UserDao对象
UserDao dao=new UserDao();
//更新结果
int result=0;
//更新
try {
result=dao.modifyUser(Integer.parseInt(id),Integer.parseInt(status));
if(result>0) {
//更新成功 清空各文本框并弹出提示框
initialize();
simpleTools.clearTextField(idTextField, userTypeNameField, userTypeStatusField);
simpleTools.informationDalog(Alert.AlertType.INFORMATION, "提示", "信息", "修改成功");
}
else {
//更新失败 给出提示
simpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "修改失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
//更新失败 给出提示
simpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "修改失败!");
}
}
/**
* 初始化界面数据
*/
public void initialize(){
// 实例化UserDao对象
UserDao userDao = new UserDao();
//为id文本框设置不可编辑
idTextField.setEditable(false);
//name也是不可修改
userTypeNameField.setEditable(false);
//查询用户
List<UserTypeNewBeanData> userTypeNewBeanDataList=null;
try {
userTypeNewBeanDataList=userDao.findTableDataList();
//将数据库数据转换成javafx需要的数据
ObservableList<UserTypeNewBeanData> data=simpleTools.getUserTypeTableViewData(userTypeNewBeanDataList);
//将数据加入到表格中
simpleTools.setUserTypeTableViewData(UserTypeManageView,data,idTableColumn,userTypeNameColumn,userTypeStatus);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//为表格控件注册事件监听
UserTypeManageView.getSelectionModel().selectedItemProperty().addListener(
((observable, oldValue, newValue) -> showUserTypeDetails(newValue)));
}
/**
* 选中行后将数据显示到下面文本框中
*/
public void showUserTypeDetails(UserTypeNewBeanData userTypeNewBeanData){
//判断是否选中
if(userTypeNewBeanData==null){
return;
} else {
//如果表格行被选中,把数据显示在下面
idTextField.setText(userTypeNewBeanData.getId()+"");
userTypeNameField.setText(userTypeNewBeanData.getUsername());
userTypeStatusField.setText(userTypeNewBeanData.getStatus()+"");
}
}
}
为UserDao开发用户状态查询与用户状态管理的逻辑
/**
* 实现对用户状态查询
*/
public List<UserTypeNewBeanData> findTableDataList() throws SQLException{
MyDataSource dataSource=new MyDataSource();
QueryRunner runner=new QueryRunner(dataSource);
String sql="select id,username,status from users where roled!=1";
return runner.query(sql,new BeanListHandler<UserTypeNewBeanData>(UserTypeNewBeanData.class));
}
/**
* 实现对用户状态的管理
* @return
*/
public int modifyUser(int id, int status) throws SQLException{
MyDataSource dataSource=new MyDataSource();
QueryRunner runner=new QueryRunner(dataSource);
String sql = "update users set status=? where id=?";
int result=runner.update(sql,status,id);
return result;
}
之前是提过了使用dbUtils踩坑,就是我们从数据库拿到数据,把这些数据可以用User封装,当然可以新设计一个实体类UserTypeNewBeanData,因为我们需要把拿到的数据展示在JavaFx表格中Simple类
package LeaveMessageSystem.beans;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class UserTypeNewBeanData {
private int id;
public UserTypeNewBeanData(int id, String username, int status) {
this.id = id;
this.username = username;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
@Override
public String toString() {
return "UserTypeNewBeanData{" +
"id=" + id +
", username='" + username + '\'' +
", status=" + status +
'}';
}
public void setUsername(String username) {
this.username = username;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public UserTypeNewBeanData() {
}
public SimpleStringProperty idProperty() {
String newid=id+"";
SimpleStringProperty idfx=new SimpleStringProperty(newid);
return idfx;
}
public SimpleStringProperty statusProperty() {
String newstatus=status+"";
//需将int转成integer
SimpleStringProperty statusfx=new SimpleStringProperty(newstatus);
return statusfx;
}
public SimpleStringProperty usernameProperty() {
SimpleStringProperty usernamefx=new SimpleStringProperty(username);
return usernamefx;
}
private String username;
private int status;
}
另外对表格数据的一些操作是在之前工具类中封装好的
还有一点需要注意是,我们在设计的时候,要对id、name设置为不可编辑。
代码已经放入GitHub,需要的同学自取。别忘了给一个star!!!