Create applications using ObjectDb and JPA in NetBeans

February 19, 2011

Hibernate, JPA

«»

Step 6: Code Events of the Controls

After coding our own methods, we can code the events for each control.

Get the design view of the frmCity JFrame and right click on it avoiding any controls. style=’mso-spacerun:yes’> Select Events, Window and windowOpened[formWindowOpened] to get the code block where we can write for all what we should do when the window is opened. We call the methods needed to connect to database, to fill cmbCountry with list of countries, to fill the lstCity with cities and to prepare the form to a selectable state like follows.

1
2
3
4
5
6
private void formWindowOpened(java.awt.event.WindowEvent evt) {     
	connectDatabase();
	fillCountries();
	fillCities();
	setToSelect();
}

As describe previously, get the btnAddNewCountryActionPerformed event and include this code needed to add a new country to the database.

1
2
3
4
5
6
7
8
9
private void btnAddNewCountryActionPerformed(java.awt.event.ActionEvent evt) {    
	Country country = new Country();
	country.setName(txtCountry.getText());
	em.getTransaction().begin();
	em.persist(country);
	em.getTransaction().commit();
	fillCountries();
	txtCountry.setText(null);
}

Write click on lstCity, select Events, ListSelection and valueChanged to get the lstCityValueChanged.

1
2
3
private void lstCityValueChanged(javax.swing.event.ListSelectionEvent evt) {
	displayCityDetails();
}

btnAddwill prepare the form to add a new City. It will clear the existing entries, selecting a city is disabled and can select to save or cancel after entering the data. The following code will achieve that functionality.

1
2
3
4
5
6
7
8
9
10
private void lstCityValueChanged(javax.swing.event.ListSelectionEvent evt) {
	displayCityDetails();
}
 
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {  
	clearCityDetails(); 
	lstCity.clearSelection(); 
	setToEdit(); 
	txtCity.requestFocus(); 
}

btnEdit will prepare the form to edit a selected country. When the user is editing a city, selecting a city is disabled, but can change the details as necessary. The following code inside btnEditActionPerformed is achieving that.

1
2
3
4
5
6
7
8
9
10
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {    
	if (lstCity.getSelectedValue() == null) {
		JOptionPane.showMessageDialog(null, "Select one to edit");
		lstCity.requestFocus();
	} else {
		setToEdit();
		txtCity.requestFocus();
		txtCity.selectAll();
	}
}

Code under the ActionPerformed of btnDelete will remove the selected item from the database. Removing an object makes a change to the database, hence we need to get the EntityTransaction instance to begin with. The object is marked to be removed when
em.remove is called and as the commit method of the EntityTransaction instance is called, the object is
physically erased from the database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
	City city = (City) lstCity.getSelectedValue();
	if (city == null) {
		JOptionPane.showMessageDialog(null, "Select one to delete");
		lstCity.requestFocus();
		return;
	} else {
		em.getTransaction().begin();
		em.remove(city);
		em.getTransaction().commit();
	}
	fillCities();
	clearCityDetails();
	setToSelect
	lstCity.requestFocus();
}

btnSave will save a new Object if the user had previously clicked the “Add” button. If the “Edit” button was clicked previously, it will update the changes done to the selected object. When the Add button is clicked, the selection in the lstCity is cleared. In that case, lstCity.getSelectedValue() returns a null value. In that case it creates a new instance of city and assign the user entered values. Now we are going to save the newly created object. As it changes the database, we must get EntityTransaction instance of the em EntityManager and call the begin method. When we call the persist method of the EntityManager instance, it marks the object to be saved to the database. No physical change occurs until we call the commit method of the EntityTransaction, which save the object in the database. Clicking the Edit button do not clear the selection of the lstCity and lstCity.getSelectedValue() will return a city object. In that case, new attributes entered by the user is done to that object. We get the EntityTransaction and call the begin method. As em is the very same EntityManager which retrieved the city object we are going to update, we do not have to order to save it explicitly. Just calling the commit method of the EntityTransaction will update the object in the database with new values. The id value can NOT be updated like this and it will throw an exception. If we call the persist method of another EntityManager, this very same object would have consider as a new object, and the update will not occur as expected. We must use the commit method related to the very same EntityManager which we used to retrieve it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {   
	City city = (City) lstCity.getSelectedValue();
	if (city == null) {
		city = new City();
		city.setCountry((Country) cmbCountry.getSelectedItem());
		city.setName(txtCity.getText());
		em.getTransaction().begin();
		em.persist(city);
		em.getTransaction().commit();
	} else {
		city.setCountry((Country) cmbCountry.getSelectedItem());
		city.setName(txtCity.getText());
		em.getTransaction().begin();
		em.getTransaction().commit();
	}
	fillCities();
	setToSelect();
	lstCity.requestFocus();
}

btnCancel just clears the entered values and make the application able to select cities.

1
2
3
4
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {  
        clearCityDetails();
        setToSelect();
    }

When the window is closing, we have to close the EntityManager. As there is only one window, the same event can be considered as closing the whole application. We can therefore close the EntityManagerFactory in the window closing event.

1
2
3
4
private void formWindowClosing(java.awt.event.WindowEvent evt) {
        em.close();
        emf.close();
    }

Step 7: Code Run the Program

We have completed all the coding. NetBeans also has created some codes for us when the JFrame and controls are added to application. If you run the application, you will get all the functionality.
If there is any error, first check whether you have add the support for ObjectDB as described above.

You can download the source code either with or without the libraries from this article.

email

«»

Comments

comments

,