By Tony Bevis on Monday, 23 October 2023
Category: Java

Java programming course: 17.3 Modifying AdministratorFrame

Modifying AdministratorFrame

The frame now needs to show AnimalPanel inside a new tab of the JTabbedPane:

At this point you can run the application to ensure it appears as below:

You can double-click the Animals node on the left to expand it, and then click the Pen sub-nodes that appear to see the animals in that pen. To make all the nodes expanded when you start the application add the following method inside AnimalTree:


Now call expandNodes() from the constructor:

You will note that after entering some details and clicking the Save button the added animal is not appearing in the list on the left. This will be addressed in the next section.

Updating AnimalTree automatically

For the AnimalTree object to get notified whenever a change to the collection of animal objects held by the administrator changes it can implement the AnimalListener interface, so you will need to import virtualzoo.core.event:

If you click the glyph that appears to the left of the class declaration statement above, you can click the option to implement all abstract methods. The following code will appear:

To handle the animalCreated() method first, insert the following code:


The getTreeNode() method loops through each of the nodes under the root node until it finds the one in the argument, which it then returns:


You now need to wire up the AnimalTree object which is referenced in AnimalPanel so that it listens to events. In AnimalPanel import virtualzoo.core and define a new instance variable to reference the ZooAdministrator object:

Now change the constructor to obtain the ZooAdministrator object and call its addAnimalListener() method:

You can now run the application, enter, and save one (or several) animals, and you should find the tree automatically updates itself.

Modifying existing animal details

Now that you have completed the code necessary to add new animals you need to enable their details to be changed. What you will do is detect whenever the user selects one of the names in AnimalTree and pass that selected Animal object to AnimalEditor so that it transitions into "change" mode.

The JTree class (of which AnimalTree is a subclass) supports the fact that client objects may wish to listen to selections of nodes in the tree. All client objects need to do is implement the TreeSelectionListener interface and provide the appropriate code in its required valueChanged() method. Then you can pass the client object as an argument to the addTreeSelectionListener() method of JTree. You will now code these steps.

Firstly, change the class header of AnimalPanel to implement TreeSelectionListener (you will also need to import the java.swing, javax.swing.event and javax.swing.tree packages):

Now you need to code the only method required by the interface:


You need to ensure you listen to the tree selection events so invoke the registration in the constructor:

In AnimalTree you need to ensure the tree is updated:

Next lesson: 17.4 Removing an existing animal

Related Posts

Leave Comments