Add a feature that allows a user to select a product in the product viewer and then delete that product:
Add an issue for this feature if you haven't already done so.
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
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.
Add a button to the
ProductViewer dialog for deleting a product (if you haven't already done so).
Refer to section 188.8.131.52 of the project specification for the lo-fi mock-up of the dialog to see where the delete button fits into things.
Add code to the delete button's handler that:
Gets the selected product from the
Refer to the reference section (Swing/List Components (JList and JComboBox)/Retrieving the Value Selected by the User).
removeProduct method on the DAO field passing the selected product.
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.
updateItems method on the model passing in the updated collection of products (use the DAO method that gets the products).
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.
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.
What happens if the user clicks delete without selecting a product?
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.