0

I am making an inventory system that has the ability of saving the data in .txt format. The GUI has a delete button that gives one the ability of deleting a selected row in a jTable but fails to do so in the .txt file that the credentials were saved before the user deleted the selected row. The save button has successfully been able to replicate data from the JTable to the .txt file. Below is the save button and delete button codes respectively:-

//save button
        JButton btnNewButton = new JButton("Save");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Clicked");
                ProductObject prouct = new ProductObject(textField.getText(), textField_1.getText(),
                        Double.valueOf(textField_2.getText()), textField_3.getText());
                products.add(prouct);
                String id = textField.getText();
                String Name =  textField_1.getText();
                String Quality = textField_2.getText();
                String Cost = textField_3.getText();
                populateTable();
            try {
                FileWriter Writer = new FileWriter("Product.txt", true);
                Writer.write(""+id+""+Name+""+Quality+""+Cost);
                Writer.write(System.getProperty("line.separator"));
                Writer.close();
                JOptionPane.showMessageDialog(null, "success");
            }
            catch(Exception r) {
                JOptionPane.showMessageDialog(null, "Error");
            }
            }
        });

//Delete Button
JButton btnNewButton_7 = new JButton("Delete");
        btnNewButton_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int i = table.getSelectedRow();
                tableModel.removeRow(i);
            }
        });
        btnNewButton_7.setBounds(10, 405, 114, 23);
        contentPane.add(btnNewButton_7);

What additional code do I need to add to make the delete button to have the ability of reading the .txt file and make the necessary adjustments?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Muema
  • 57
  • 1
  • 5
  • 2
    Why not override the .txt file, after deleting, with the actual data ? Refactor the code block of `FileWriter Writer = ...` into a private method where you always write the complete tableModel (`products`)to the file and call from both ActionListener. – PeterMmm Jan 24 '22 at 20:05
  • [How to delete a cell from jTable by pressing DELETE key?](https://stackoverflow.com/questions/12985911/how-to-delete-a-cell-from-jtable-by-pressing-delete-key/12985981#12985981); [Which of this delete JTable row methods is better?](https://stackoverflow.com/questions/18666470/which-of-this-delete-jtable-row-methods-is-better/18666960#18666960); [Add Jbutton to each row of a Jtable](https://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) – MadProgrammer Jan 24 '22 at 21:29
  • 1
    This is one of those moments you realise the importance of "data models". All the data you want to manage should be represent as (at a minimum) an object (preferably described by an `interface`). This makes it infinitely easier to manage. You could then have a "data management service" which was responsible for loading and saving the data, which was independent and decoupled from the rest of your code. You'd then make use of a custom `TableModel` (extended from `AbstractTableModel`) which could manage accept these "data" objects. – MadProgrammer Jan 24 '22 at 21:32
  • 1
    Deleting a row(s) from a file is simply a matter of re-writing the file, excluding the data you don't want, which is why my previous comment becomes so important. This might also be a good time to investigate things like XML, JSON and even CSV, which provide a common structure to the data – MadProgrammer Jan 24 '22 at 21:33

0 Answers0