I have all of my program working except the following buttons (add, modify, delete, save, search) which I have the buttons, but turned off in my program so it will still compile/run until I get the button functionality fixed. To be honest, I have no clue on what code needs to make them work as required. I am strictly a beginner and it has taken many many sleepless nights just to get to this point. Any help would be very much appreciated. :) The following is what I need to accomplish, followed by my section of code (as not to overwhelm you with my whole program):

Modify the Inventory Program to include an Add button, a Delete button, and a Modify
button on the GUI. These buttons should allow the user to perform the corresponding
actions on the item name, the number of units in stock, and the price of each unit. An
item added to the inventory should have an item number one more than the previous last
item.
• Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory
by the product name. If the product is not found, the GUI should display an appropriate
message. If the product is found, the GUI should display that product’s information in the
GUI.

// I NEED HELP WITH THIS SECTION. HAVE NO CLUE ON WHAT CODE WILL MAKE BUTTONS WORK AS REQUESTED

//  private JButton addButton = new JButton(addAction);


//private Action lastAction  = new AbstractAction("Add") {
//  public void actionPerformed(ActionEvent evt) {


//index = ProductInventory.getSize() - 1;


//      repaint();
//      }
//  };
//  private JButton modifyButton = new JButton(modifyAction);


//  private Action lastAction  = new AbstractAction("Modify") {
//  public void actionPerformed(ActionEvent evt) {


//  index = ProductInventory.getSize() - 1;


//  repaint();
//  }
//  };
//private JButton deleteButton = new JButton(deleteAction);


//private Action lastAction  = new AbstractAction("Delete") {
//  public void actionPerformed(ActionEvent evt) {


//  index = ProductInventory.getSize() - 1;


//  repaint();
//  }
//};
// private JButton saveButton = new JButton(saveAction);


// private Action lastAction  = new AbstractAction("Save") {
//  public void actionPerformed(ActionEvent evt) {


//  index = ProductInventory.getSize() - 1;


//  repaint();
//  }
//};
//private JButton searchButton = new JButton(searchAction);


//private Action lastAction  = new AbstractAction("Search") {
//  public void actionPerformed(ActionEvent evt) {


//  index = ProductInventory.getSize() - 1;


//  repaint();
//  }
//};

Dani AI

Generated

Plan: keep a single model (for example an ArrayList<Product> or the existing ProductInventory class), a currentIndex pointing at the product shown in the GUI, and small helper methods loadFields(int index), clearFields(), and validateFields() that perform parsing/validation. Each button should update the model (add/modify/delete), update currentIndex as appropriate, then call loadFields(currentIndex) and enable the Save button.

Add / Modify / Delete / Search (action logic in plain steps)

  • Add: validate name, parse units and price. Compute new id as inventory.size() + 1 (or use a sequence that survives deletes). Create a Product, add it to the inventory, set currentIndex = inventory.size() - 1, refresh UI.
  • Modify: ensure currentIndex >= 0, validate inputs, update the Product object at currentIndex, refresh UI.
  • Delete: confirm via JOptionPane, remove the product at currentIndex. If you want contiguous IDs, reassign ids in a short loop; otherwise keep ids unique. Then set currentIndex to a nearby valid index (e.g., max(0, currentIndex-1)) and refresh.
  • Search: iterate the inventory using equalsIgnoreCase on the product name. If found set currentIndex and call loadFields(currentIndex). If not found show a message.

Saving / file creation

  • Create the directory with new File("C:\\data").mkdirs() and handle the boolean result. Use ObjectOutputStream (or write CSV) to persist the collection. Product and the collection must be Serializable if using object streams. Example pattern:
File dir = new File("C:\\data");
if (!dir.exists() && !dir.mkdirs()) {
    // show error: cannot create directory
}
File out = new File(dir, "inventory.dat");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(out))) {
    oos.writeObject(productList); // productList must be Serializable
}

Troubleshooting notes

  • Catch NumberFormatException when parsing numbers and show a clear message. Catch IOException for save and report the exception message. Remember C:\data requires Windows and write permission; for portability use a user directory. See the File.mkdirs() behavior and ObjectOutputStream usage in the Java docs for details: File.mkdirs and ObjectOutputStream.

Context: was correct that you need real handlers behind the buttons; and indicated similar example threads that can be used as complementary references when wiring the actual ActionListener/Action code.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

So, you have the buttons, now you need to write the code behind the buttons that actually does the useful stuff don't ya?

You should check answer to your first post where I provided a link to other post with same problem as yours. Based on that you would get some ideas what to do with it

Hey Catgirl. Search on my name.
Some of the guys helped me work through this exact assignment some months back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.