labs / tiddlers / content / labs / lab03 / _Labs_03_Displaying the
@Mark George Mark George on 20 Jul 2021 1 KB WIP lab 3

Now we need to make the ProductViewer display the products that have been saved in the DAO.

  1. In the source view, add a field to ProductViewer that is an instance of your DAO class.

  2. We need to get the product objects out of the DAO and put them into the JList.

    Since Swing components use the Model-View-Controller mechanism, we need to create a model (not to be confused with modal --- they are two entirely different concepts) that will hold the data and then tell the list component about the model.

    The default model classes in Swing are outdated and clunky, so we have written one that is a bit easier to work with:

    1. Create a new class named SimpleListModel. It should go in a package named helpers

    2. Open the following URL in a web browser:

    3. Copy and paste the code into your SimpleListModel class.

  3. Refer to the Swing/List Components (JList and JComboBox)/Displaying Objects in a List/Displaying%20Objects%20in%20a%20List) section in the reference.

    Add a SimpleListModel field to your ProductViewer dialog (step 1 from the reference). This model is for holding the products that will be displayed so name it productsModel.

    Call the method on the DAO object that gets all of the products. Store the result in a variable.

    Update the contents of the model field using the list of products that you just retrieved from the DAO (step 2 in the reference).

    Tell the JList component about the model (step 3).

  4. Test the system again. You should now be able to add some products using the product editor dialog and then see them in the product viewer dialog.

    If you are not happy with how the products are being displayed then you can change the toString() method in the product domain class.

  5. You can close the issue for this feature now.