是否可以在实例运行时刷新 JavaFX 中的程序文件



我正在构建一个库存管理系统,并创建了一个按钮,该按钮可以打开一个新的FXML,用于将新项目输入数据库。我已经创建了添加图像的功能,它使用 URL 更新数据库的图像列,当我单击保存时,它会将图像从源位置的副本保存到目标位置(这是程序中的一个包)。如果我保存并关闭程序,那么我可以在 eclipse 中刷新项目,重新登录程序,新添加的项目将显示在从数据库填充的表中视图,没有任何问题。

遇到的问题是,当我单击保存按钮时,我希望它"刷新"程序文件和包,这样当我查询零件数据库时,它将显示新添加的零件,而不会因为我在我创建的图像包中看不到图像而给我一个错误(当程序关闭和刷新时,它会显示在那里)。是否可以使用代码刷新这些映像包?图像是否应该存储在外部文件中?如果没有,是否可以将图像保存到程序包中而不必使用绝对路径?

到目前为止,我的代码是:

// Event Listener on Button[#saveItemBtn].onAction
@FXML
public void saveNewItemClicked(ActionEvent event) {
    try{
        this.dbState = (String) databaseChoiceBox.getValue();
        DBConnection dBC = new DBConnection();
        con = dBC.getDBConnection();
        String maxSQL = "SELECT MAX(id) FROM " + dbState + "_parts_list";
        ResultSet rs = con.createStatement().executeQuery(maxSQL);
        int idNumber = rs.getInt(1);
        idNumber++;
        quantity = Integer.parseInt(quantityTxt.getText());
        min = Integer.parseInt(minTxt.getText());
        max = Integer.parseInt(maxTxt.getText());

        double price = Double.parseDouble(priceTxt.getText());
        SelectedItem selectedItem = new SelectedItem();
        selectedItem.setId(idNumber);
        selectedItem.setQuantity(quantity);
        selectedItem.setMin(min);
        selectedItem.setMax(max);
        selectedItem.setEquipment_id(equipmentIDTxt.getText());
        selectedItem.setEquipment_group(equipmentGroupTxt.getText());
        selectedItem.setPrice(price);
        selectedItem.setManufacturer_name(manufacturerNameTxt.getText());
        selectedItem.setModel_number(modelNumberTxt.getText());
        selectedItem.setVendor_name(vendorNameTxt.getText());
        selectedItem.setVendor_part_number(vendorPartNumberTxt.getText());
        selectedItem.setTolmar_part_number(tolmarPartNumberTxt.getText());
        selectedItem.setDescription(descriptionTxt.getText());
        selectedItem.setAdditional_notes(additionalNotesTxt.getText());
        selectedItem.setPart_location(locationTxt.getText());
        if (img != null) {
            selectedItem.setImage("/" + dbState + "Images/" + imagePath);   
        } else {
            selectedItem.setImage("/img/NoImageFound.png");
        }
        selectedItem.setDepartment(databaseChoiceBox.getValue().toLowerCase());
        String sql = "INSERT INTO " + dbState + "_parts_list VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement myStmt = con.prepareStatement(sql);
        myStmt.setInt(1, idNumber);
        myStmt.setString(2, selectedItem.getManufacturer_name());
        myStmt.setString(3, selectedItem.getModel_number());
        myStmt.setString(4, selectedItem.getVendor_name());
        myStmt.setString(5, selectedItem.getVendor_part_number());
        myStmt.setString(6, selectedItem.getTolmar_part_number());
        myStmt.setString(7, selectedItem.getPart_location());
        myStmt.setDouble(8, selectedItem.getPrice());
        myStmt.setInt(9, selectedItem.getQuantity());
        myStmt.setInt(10, selectedItem.getMin());
        myStmt.setInt(11, selectedItem.getMax());
        myStmt.setString(12, selectedItem.getImage());
        myStmt.setString(13, selectedItem.getEquipment_group());
        myStmt.setString(14, selectedItem.getEquipment_id());
        myStmt.setString(15, selectedItem.getAdditional_notes());
        myStmt.setString(16, selectedItem.getDescription());
        myStmt.setString(17, selectedItem.getDepartment());
        myStmt.executeUpdate();
        saveStatusLbl.setText("New Item Saved");
        Path source = Paths.get(img);
        Path target = Paths.get("E:/Programming/workspace/Inventory Management System 4/src/" + dbState + "Images/", file.getName());
        try {
            //replace existing file using java nio package
            Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
        con.close();
        PauseTransition delay = new PauseTransition(Duration.seconds(2));
        delay.setOnFinished( e -> window.close() );
        delay.play();
        }catch(Exception exc){
            System.out.println(exc);
            exc.printStackTrace();
        }       
}

您绝对不应该将数据文件存储在类文件中。在捆绑应用程序的实际场景中,这无论如何都不起作用。

应避免在运行应用程序时修改应用程序的资源。

当然,您可以尝试通过玩弄ClassLoader来使其工作,例如使用 URLClassLoader ,但恕我直言,不将文件存储为资源要简单得多,而是将它们存储在不在类路径内的目录中并访问这些文件。可以使用以下命令从Path p获取 URL 字符串:

p.toUri().toURL().toExternalForm()

最新更新