Download the
SwingNotes.zip archive.
For expediency you can install the project from existing
sources, but it is recommended to go through the step-by-step
approach to better appreciate the constructions.
Create a new Library in NetBeans
The SwingNotes project requires JAR files available in NetBeans. For convenience,
I have made a new group of JAR files which we will install as a Library in
NetBeans.
First download the zipped directory of JAR files:
Extract it as the folder HibernateSearch into the location of your choice.
Some suggestions are (create these folders if necessary, or make other choice):
Create the HibernateSearch library in NetBeans as follows:
Go to Tools Libraries
and click the New Library button (bottom, left).
In the Library Name, type HibernateSearch.
Click OK.
With the Classpath tab selected, click the Add Jar/Folder button
and navigate to the location of the HibernateSearch folder.
Select all the JAR files within the HibernateSearch folder
and again click the Add Jar/Folder button.
Back in the Library Manager, click OK.
JList Component
Many of the complex swing components employ one or more models
which control a portion of the total behavior of the component.
The two primary models are the
data and selection models.
The data model usually governs how to control the contents of the component
whereas the selection model governs the behavior of selected elements.
Java provides default data and
selection models
which are often
sufficient for most purposes,
but allows a developer to create other non-standard models.
This separation of component and its
models gives the component an extra degree of generality and flexibility.
Both
JList and JComboBox objects are used to maintain
lists of objects. The difference
is that the JList keeps a certain number
visible in a scroll area
whereas the JComboBox only
keeps the "selected one" visible in a drop-down fashion.
In the case of a JList, we use a model object defined
in the controller like this
DefaultListModel listmodel = new DefaultListModel();
This model is associated to the JList
component, say, with name list,
via the setModel operation by an interface function:
public void setListModel(ListModel lm) {
list.setModel(lm);
}
In the controller, these are typical operations:
listmodel.addElement( some_element ); // add an element
listmodel.clear(); // remove all elts
The JList object itself
is responsible for determining the selection as
well as generating an event when the selection
changes. Use these operations for determining selection:
list.getSelectedValue()
list.getSelectedIndex();
Typical to our MVC approach, if we need to know the selected
value without a selection change taking place, we would
write an interface function:
public Object getListSelectedValue() {
return list.getSelectedValue();
}
Most importantly a ListSelectionListener is used
for activation when the selection is changed. Again, we will
create an interface function to the container which employs
the list
public void addListSelectionListener(ListSelectionListener lsl) {
list.addListSelectionListener(lsl);
}
The controller then writes a handler operation:
view_container.addListSelectionListener(
new ListSelectionListener() {
public void itemStateChanged(ItemEvent evt) {
...
}
}
);
This function is called when a list selection is changed,
both on deselection of the previous element
with mouse down and selection of the new element with mouse up;
the rationale is that this behavior is needed for various
drag-and-drop operations.
A special event-based getValueIsAdjusting()
function is usually used to filter out the deselection event:
if (evt.getValueIsAdjusting()) return;
Database setup
The setup folder contains MySQL scripts which
can be used to set up the database, table and provide initial
population of data. Open a shell, using the cd shell command,
navigate to the
setup folder, then run these commands:
mysql -u root < database.sql
mysql -u guest swingnotes < table.sql
mysql -u guest swingnotes < table-data.sql
The first two are these:
database.sql
create database if not exists swingnotes;
grant all on swingnotes.* to guest@localhost;
table.sql
DROP TABLE IF EXISTS notes;
CREATE TABLE notes (
id INTEGER AUTO_INCREMENT,
subject VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP NOT NULL,
PRIMARY KEY(id)
);
The table-data.sql file has this format:
insert into notes (subject,content) values
( "ORMs", "... a long content string ..." ),
( "NetBeans Designer", "... a long content string ..." ),
( "MVC event handling", "... a long content string ..." );
Note the usage of the MySQL TIMESTAMP field, which usually automatically
updates the time whenever the record is modified (unless the TIMESTAMP
field itself is reset). Strangely enough,
Hibernate does not do achieve this type of modification except when the
inital record is created.
function now() to give each entry the
creation timestamp value.
You can check the validity of the operations by running:
mysql -u guest swingnotes
mysql> show tables;
mysql> describe notes;
mysql> select id,subject,created from notes;
mysql> quit
NetBeans Installation
To install the entire source at once.
move the SwingNotes project folder
into your NetBeansProjects directory.
create a Java project from existing sources.
add the MySQL and the new HibernateSearch (note Hibernate) libraries.
Step-by-step Contruction
Create the new Java Application project SwingNotes.
Add the MySQL and HibernateSearch libraries.
Create a JFrame Form as the class views.Frame.
Set the layout of Frame to be BorderLayout.
Drag a Split Pane container onto the Frame.
Edit its properties (use the Inspector) and make
the orientation be VERTICAL_SPLIT
Drag a List into the top part
Drag a TextArea into the bottom part
Change the variable name of the List to be list.
Change the variable name of the TextArea to be display.
Edit list properties.
Remove the default value of the
model property: [item1,item2,…]
(remove all values).
In the list properties, set the selectionMode to SINGLE.