Java combat the housekeeper billing system (11) - right-click menu to achieve refresh, additions and deletions function

This clause Overview

This section speaks to implement CRUD through the right-click menu function.

 

Refresh context menu

"Refresh" pop-up menu, click refresh all data interfaces, namely initialization method is called initialize () method to re-initialize it again.

    /**
     * ”刷新“右键菜单的事件监听器
     *
     * @param actionEvent 事件
     */
    public void refreshContextMenuEvent(ActionEvent actionEvent) {
        // 刷新数据
        initialize();
    }

 

Adding context menu

The same "add" pop-up menu is simple, just jump to the interface can be added after clicking on the menu item.

    /**
     * “添加”右键菜单的事件监听器
     *
     * @param event 事件
     */
    @FXML
    public void addContextMenuEvent(ActionEvent event) {
        // 跳转到添加界面即可
        mainApp.initAddFrame();
    }

 

Delete context menu

"Delete" context menu event handling, namely to obtain data of the selected table rows, and then delete the recording method of performing an operation invocation RecordDao.java in deleteRecord ().

Data acquisition line is selected by "getSelectedItem () tableView.getSelectionModel ().".

    /**
     * “删除”右键菜单的事件监听器
     *
     * @param event 事件
     */
    @FXML
    public void deleteContextMenuEvent(ActionEvent event) {
        // 给出提示框提示用户是否选择删除
        boolean b = SimpleTools.informationDialog(Alert.AlertType.CONFIRMATION, "提示", "提示", "请问是否删除?");
        if (b) {
            RecordDao recordDao = new RecordDao();
            // 获取所选择行的索引,从0开始
            int selectedIndex = tableView.getSelectionModel().getSelectedIndex();
            // 获取所选中行是数据
            TableData td = tableView.getSelectionModel().getSelectedItem();
            // 判断是否选中表格行
            if (selectedIndex >= 0) {
                // 删除选中的表格行根据索引
                tableView.getItems().remove(selectedIndex);
                // 执行删除操作
                recordDao.deleteRecord(new Record(Integer.parseInt(td.getId())));
            }
            // 刷新表格数据
            initialize();
        }
    }
 

 

Modify the right-click menu

"Modify" Right-click to achieve a little more difficult, because the data transfer to the selected table row you want to modify the interface a bit difficult.

Modify the context menu event handling code is as follows:

    /**
     * “修改”右键菜单的事件监听器
     *
     * @param event 事件
     */
    @FXML
    public void alterContextMenuEvent(ActionEvent event) {
        // 获取所选中行的数据
        TableData td = tableView.getSelectionModel().getSelectedItem();
        // 设置一个标志,判断是右键菜单触发的修改还是由菜单条上的菜单触发的修改
        boolean isContextMenu = true;
        mainApp.initAlterFrame(td, isContextMenu);
    }

You can see the call initAlterFrame () method, which is loaded alterAccountFrame.fxml document, realized in the modification function, but no parameters, and this modified the code so that it has two parameters, the first that is to modify the parameter data, the second parameter is a Boolean type, to determine whether the revision is triggered or a modified context menu on the menu bar of the trigger.

So modify menu event method alterMenuItemEvent () also need to modify the code:

    /**
     * ”修改“菜单项的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void alterMenuItemEvent(ActionEvent actionEvent) {
        // 刷新数据
        initialize();
        // 调用修改界面控制器
        mainApp.initAlterFrame(null,false);
    }

Then MainApp.java in initAlterFrame () method modified as follows, to be transmitted through this method to modify the data to the controller:

    /**
     * 操作结果:更改账目界面
     */
    public Scene initAlterFrame(TableData tableData,boolean isContextMenu) {
        try {
​
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/alterAccountFrame.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
​
            Stage mainFrameStage = new Stage();
            mainFrameStage.setTitle("更改账目");
            mainFrameStage.setResizable(true);
            mainFrameStage.setAlwaysOnTop(false);
            mainFrameStage.initModality(Modality.APPLICATION_MODAL);
            mainFrameStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            mainFrameStage.setScene(scene);
​
            // 判断是否由右键菜单触发打开的修改界面,如果是就将要修改的数据传到修改界面
            if(isContextMenu){
                AlterAccountFrameController controller= loader.getController();
                controller.setTableData(tableData);
            }
​
            mainFrameStage.showAndWait();
            return scene;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Was then added to a method provided in AlterAccountFrameController.java the data to be modified:

    /**
     * 将右键菜单所触发的修改的数据传到修改界面
     *
     * @param tableData 要修改的数据
     */
    public void setTableData(TableData tableData) {
        // 使界面组件可编辑
        inputRadioButton.setDisable(false);
        outputRadioButton.setDisable(false);
        moneyTextField.setDisable(false);
        classificationComboBox.setDisable(false);
        memoTextArea.setDisable(false);
        datePickerText.setDisable(false);
        alterButton.setDisable(false);
​
        // 将要修改的数据填充到各个控件中
        idTextField.setText(tableData.getId());
        if (tableData.getType().equals("支出")) {
            outputRadioButton.setSelected(true);
        } else {
            inputRadioButton.setSelected(true);
        }
        if (outputRadioButton.isSelected()) {
            List<Classification> classificationList = new ClassificationDao().selectByType("支出");
            // 实例化一个一维数组
            String[] classificationNames = new String[classificationList.size()];
            // 将查询得到的分类名称装到一维数组中
            for (int i = 0; i < classificationList.size(); i++) {
                classificationNames[i] = classificationList.get(i).getcName();
            }
            classificationComboBox.setItems(FXCollections.observableArrayList((List) Arrays.asList(classificationNames)));
        }
        if (inputRadioButton.isSelected()) {
            List<Classification> classificationList = new ClassificationDao().selectByType("收入");
            // 实例化一个一维数组
            String[] classificationNames = new String[classificationList.size()];
            // 将查询得到的分类名称装到一维数组中
            for (int i = 0; i < classificationList.size(); i++) {
                classificationNames[i] = classificationList.get(i).getcName();
            }
            classificationComboBox.setItems(FXCollections.observableArrayList((List) Arrays.asList(classificationNames)));
        }
        moneyTextField.setText(tableData.getMoney());
        // 设置分类
        String str = tableData.getClassification();
        int index = 0;
        if (tableData.getType().equals("支出")) {
            List<Classification> classificationList = new ClassificationDao().selectByType("支出");
            // 实例化一个一维数组
            String[] classificationNames = new String[classificationList.size()];
            // 将查询得到的分类名称装到一维数组中
            for (int i = 0; i < classificationList.size(); i++) {
                classificationNames[i] = classificationList.get(i).getcName();
            }
            List outputList = FXCollections.observableArrayList((List) Arrays.asList(classificationNames));
            for (int i = 0; i < outputList.size(); i++) {
                if (str.equals(outputList.get(i))) {
                    index = i;
                }
            }
            //设置选中的分类
            classificationComboBox.getSelectionModel().select(index);
        }
        if (tableData.getType().equals("收入")) {
            List<Classification> classificationList = new ClassificationDao().selectByType("收入");
            // 实例化一个一维数组
            String[] classificationNames = new String[classificationList.size()];
            // 将查询得到的分类名称装到一维数组中
            for (int i = 0; i < classificationList.size(); i++) {
                classificationNames[i] = classificationList.get(i).getcName();
            }
            List inputList = FXCollections.observableArrayList((List) Arrays.asList(classificationNames));
            for (int i = 0; i < inputList.size(); i++) {
                if (str.equals(inputList.get(i))) {
                    index = i;
                }
            }
            // 设置选中的分类
            classificationComboBox.getSelectionModel().select(index);
        }
        memoTextArea.setText(tableData.getMemo());
        datePickerText.setValue(LocalDate.parse(tableData.getDate()));
    }

Run the program, test function:

 

 

Searchable public micro-channel number [ Java example program ] or scan the Fanger Wei code number to get more public attention.

Note: Reply to [the public number background 20200331 ] can get the source code of this chapter.

 

Published 500 original articles · won praise 77 · views 160 000 +

Guess you like

Origin blog.csdn.net/cnds123321/article/details/104288443