labs / tiddlers / content / labs / lab04 / _Labs_04_Deleting

Add a feature that allows a user to select a product in the product viewer and then delete that product:

  1. Add an issue for this feature if you haven't already done so.

  2. We need to fix the type of object that the JList on your ProductViewer dialog is expecting. By default NetBeans assumes it will hold strings which is not the case since we will be storing products in the JList.

    There is a section in the reference (Swing/List Components (JList and JComboBox)/Fixing the JList & JComboBox Type Problem/Fixing%20the%20JList%20&%20JComboBox%20Type%20Problem)) that explains how to fix this problem. Follow the instructions on that page to make the JList use your product domain class instead of strings.

  3. Add a button to the ProductViewer dialog for deleting a product (if you haven't already done so).

    Refer to section of the project specification for the lo-fi mock-up of the dialog to see where the delete button fits into things.

  4. Add code to the delete button's handler that:

    • Gets the selected product from the JList.

      Refer to the reference section (Swing/List Components (JList and JComboBox)/Retrieving the Value Selected by the User).

    • Calls the removeProduct method on the DAO field passing the selected product.

    • Updates the JLists's model object. If we don't do this then the user interface will continue to show the deleted product. The model is the SimpleListModel field that is declared at the top of the class.

      Call the updateItems method on the model passing in the updated collection of products (use the DAO method that gets the products).

  5. Test your system (by running it). You should now see that when you select a product and press the delete button, the product is removed.

  6. Finally you should add a confirmation dialog to ensure the user doesn't accidentally delete a product.

    There is a section in the reference (Swing/Message Dialogs (JOptionPane))) that describes how to use confirmation dialogs.

  7. What happens if the user clicks delete without selecting a product?

    The JList component has an isSelectionEmpty method that you can use to prevent crashes here.

    Don't bug the user unnecessarily with message boxes here. Simply preventing the system from crashing is sufficient. The user will quickly recognise their mistake and throwing up a message box every time the user makes a minor and easily recognisable mistake is a great way of training them to close message boxes without reading them.