微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaFX我的主菜单上有2个类似的TableView,如果我先搜索它,实际上并不会删除某些内容

如何解决JavaFX我的主菜单上有2个类似的TableView,如果我先搜索它,实际上并不会删除某些内容

嘿,我先感谢您提供任何帮助,我有一个菜单,您可以在其中搜索添加删除修改两个不同的TableView。充满ObservableList的代码可以完美地工作,但是具有ObservableList的代码却不能正常工作,即使我复制并粘贴了代码并相应地将Part for Product切换了出去。如果使用“产品”,则如果我提前搜索它,实际上并不会删除该行。只会将其从搜索列表中删除(如果我返回完整列表,则会显示备份。)

这是我的主菜单控制器:

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import model.InHouse;
import model.Inventory;
import model.Part;
import model.Product;

/**
 * FXML Controller class
 *
 * @author ajw51
 */
public class MainMenuController implements Initializable {

    Stage stage;
    Parent scene;

    @FXML
    private TableView<Part> partsTableView;

    @FXML
    private TableColumn<Part,Integer> partIdCol;

    @FXML
    private TableColumn<Part,String> partNameCol;

    @FXML
    private TableColumn<Part,Integer> partInvCol;

    @FXML
    private TableColumn<Part,Double> partPriceCol;

    @FXML
    private TableView<Product> productsTableView;

    @FXML
    private TableColumn<Product,Integer> productIdCol;

    @FXML
    private TableColumn<Product,String> productNameCol;

    @FXML
    private TableColumn<Product,Integer> productInvCol;

    @FXML
    private TableColumn<Product,Double> productPriceCol;

    @FXML
    private TextField partSearchTxt;

    @FXML
    private TextField productSearchTxt;

    @FXML
    void onActionAddPart(ActionEvent event) throws IOException {
        stage = (Stage) ((Button) event.getSource()).getScene().getwindow();
        scene = FXMLLoader.load(getClass().getResource("/view/AddPartInHouseMenu.fxml"));
        stage.setScene(new Scene(scene));
        stage.show();
    }

    @FXML
    void onActionAddProduct(ActionEvent event) throws IOException {
        stage = (Stage) ((Button) event.getSource()).getScene().getwindow();
        scene = FXMLLoader.load(getClass().getResource("/view/AddProductMenu.fxml"));
        stage.setScene(new Scene(scene));
        stage.show();
    }

    @FXML
    void onActionDeletePart(ActionEvent event) {
        partsTableView.getItems().removeAll(partsTableView.getSelectionModel().getSelectedItem());
    }

    @FXML
    void onActionDeleteProduct(ActionEvent event) {
        productsTableView.getItems().removeAll(productsTableView.getSelectionModel().getSelectedItem());
    }

    @FXML
    void onActionExit(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    void onActionModifyPart(ActionEvent event) throws IOException {
        Part partSelected = partsTableView.getSelectionModel().getSelectedItem();
        if (partSelected instanceof InHouse) {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/view/ModifyPartInHouseMenu.fxml"));
            loader.load();
            
            ModifyPartInHouseMenuController MPIHController = loader.getController();
            MPIHController.sendPart(partSelected);
            
            stage = (Stage) ((Button) event.getSource()).getScene().getwindow();
            Parent scene = loader.getRoot();
            stage.setScene(new Scene(scene));
            stage.show();
        } else {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/view/ModifyPartOutsourcedMenu.fxml"));
            loader.load();
            
            ModifyPartOutsourcedMenuController MPOController = loader.getController();
            MPOController.sendPart(partSelected);
            
            stage = (Stage) ((Button) event.getSource()).getScene().getwindow();
            Parent scene = loader.getRoot();
            stage.setScene(new Scene(scene));
            stage.show();
        }
    }

    @FXML
    void onActionModifytProduct(ActionEvent event) throws IOException {
        stage = (Stage) ((Button) event.getSource()).getScene().getwindow();
        scene = FXMLLoader.load(getClass().getResource("/view/ModifyProductMenu.fxml"));
        stage.setScene(new Scene(scene));
        stage.show();
    }

    @FXML
    void onActionSearchParts(ActionEvent event) throws IOException {
        String q = partSearchTxt.getText();
        partsTableView.setItems(Inventory.lookupPart(q));
    }

    @FXML
    void onActionSearchProducts(ActionEvent event) throws IOException{
        String q = productSearchTxt.getText();
        productsTableView.setItems(Inventory.lookupProduct(q));
    }

    @Override
    public void initialize(URL url,ResourceBundle rb) {
        // Todo

        partsTableView.setItems(Inventory.getAllParts());

        partIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));
        partNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        partInvCol.setCellValueFactory(new PropertyValueFactory<>("stock"));
        partPriceCol.setCellValueFactory(new PropertyValueFactory<>("price"));

        productsTableView.setItems(Inventory.getAllProducts());

        productIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));
        productNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        productInvCol.setCellValueFactory(new PropertyValueFactory<>("stock"));
        productPriceCol.setCellValueFactory(new PropertyValueFactory<>("price"));

    }

}

这是我的清单类和方法

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author ajw51
 */
public class Inventory {

    private static ObservableList<Part> allParts = FXCollections.observableArrayList();
    private static ObservableList<Product> allProducts = FXCollections.observableArrayList();

    public static void addPart(Part newPart) {
        allParts.add(newPart);
    }

    public static void addProduct(Product newProduct) {
        allProducts.add(newProduct);
    }

    public static Part lookupPart(int partId) {
        for (Part part : getAllParts()) {
            if (part.getId() == partId) {
                return part;
            }
        }
        return null;
    }

    public static Product lookupProduct(int productId) {
        for (Product product : getAllProducts()) {
            if (product.getId() == productId) {
                return product;
            }
        }
        return null;
    }
    public static ObservableList<Part> lookupPart(String partName) {
        ObservableList<Part> allFilteredParts = FXCollections.observableArrayList();

        for (Part part : getAllParts()) {
            if (part.getName().contains(partName)) {
                allFilteredParts.add(part);
            }
        }
        if (allFilteredParts.isEmpty()) {
            return getAllParts();
        }

        return allFilteredParts;
    }

    public static ObservableList<Product> lookupProduct(String productName) {
        ObservableList<Product> allFilteredProducts = FXCollections.observableArrayList();

        for (Product product : getAllProducts()) {
            if (product.getName().contains(productName)) {
                allFilteredProducts.add(product);
            }
        }
        if (allFilteredProducts.isEmpty()) {
            return getAllProducts();
        }

        return allFilteredProducts;
    }

    public static void updatePart(int index,Part selectedPart) {

        getAllParts().set(index,selectedPart);

    }

    public static void updateProduct(int index,Product selectedProduct) {
        
        getAllProducts().set(index,selectedProduct);
    
    }
    public static boolean deletePart(Part selectedPart) {
        
        for (Part part : getAllParts()) {
            if (part.getId() == selectedPart.getId()) {
                return getAllParts().remove(part);
            }
        }
        return false;
    }

    public static boolean deleteProduct(Product selectedProduct) {
        
        for (Product product : getAllProducts()) {
            if (product.getId() == selectedProduct.getId()) {
                return getAllProducts().remove(product);
            }
        }
        return false;
    }
    
    public static ObservableList<Part> getAllParts() {
        return allParts;
    }

    public static ObservableList<Product> getAllProducts() {
        return allProducts;
    }

}

这是我的主要建议,如果您出于某些原因需要它:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.InHouse;
import model.Inventory;
import model.Outsourced;
import model.Product;

/**
 *
 * @author ajw51
 */
public class InventorySystem extends Application{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Todo code application logic here
        
        InHouse inhouse1 = new InHouse(1,"Bucket",5.99,5,2,200,8451);
        InHouse inhouse2 = new InHouse(3,"Wrench",17.99,10,40,3201);
        
        Outsourced outsource1 = new Outsourced(2,"Hammer",8.99,11,7,70,"Wisconsin Tools");
        Outsourced outsource2 = new Outsourced(4,"Shovel",20.99,18,4,78,"Hardware Co");
        
        Product product1 = new Product(1,200);
        Product product2 = new Product(2,70);
        Product product3 = new Product(3,40);
        Product product4 = new Product(4,78);
        
        Inventory.addPart(inhouse1);
        Inventory.addPart(outsource1);
        Inventory.addPart(inhouse2);
        Inventory.addPart(outsource2);
        
        Inventory.addProduct(product1);
        Inventory.addProduct(product2);
        Inventory.addProduct(product3);
        Inventory.addProduct(product4);
        
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/view/MainMenu.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Inventory System");
        stage.show();
    }
    ```
Thank You!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。