Hey I appreciate any help in advance, I have a main menu where you can search, add, delete, and modify two different TableViews. The one filled with the ObservableList works perfectly but the one with the ObservableList doesn't work the same even though I copy and pasted the code and switched out Part for Product respectfully. With the Products it doesn't actually delete a row if I search for it in advance. It only deletes it from the search list (item shows back up if I go back to the full list).

Here is my main menu controller:

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"));

    }

}

Here is my Inventory class with my methods:

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;
    }

}

and here is my main if you need it for some reason :

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, 5, 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, "Bucket", 5.99, 5, 2, 200);
        Product product2 = new Product(2, "Hammer", 8.99, 11, 7, 70);
        Product product3 = new Product(3, "Wrench", 17.99, 10, 5, 40);
        Product product4 = new Product(4, "Shovel", 20.99, 18, 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!
0

There are 0 best solutions below