Download the
MemoList.ziparchive. This contains the project folder MemoList
folders with two subfolders:
src/ source code for the project
setup/ database setup SQL files
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.
Swing 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 whereas the JComboBox only
keeps the "selected one" visible.
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 root < table-create.sql
mysql -u root < table-data.sql
The first two are these:
database.sql
create database if not exists memolist;
grant all on memolist.* to guest@localhost;
table-create.sql
use memolist;
drop table if exists memos;
create table memos (
id integer auto_increment not null,
subject varchar(50) not null,
content text not null,
creation datetime not null,
primary key(id),
fulltext(content)
) type=myisam
The table-data.sql file has this format:
use memolist;
insert into memos (subject,content,creation) values
( "Dialogs", "... a very long content string ...", now() ),
( "JLists and JComboBoxes", "... a very long content string ...", now() );
Note the usage of the MySQL function now() to give each entry the
creation timestamp value.
We see, the database is memolist and the table is memos
and so you can check the validity of the operations by running:
mysql -u guest memolist
mysql> show tables;
mysql> describe memos;
mysql> select id,subject,creation from memos;
mysql> quit
You should get positive results from these steps. If so,
go ahead with the application.
NetBeans Installation
To install the entire source,
move the MemoList project folder
into your NetBeansProjects directory. Create a project
from existing sources and add the MySQL library.
Step-by-step Contruction
Create the new Java Application project MemoList.
Add the MySQL library.
Create a JFrame Form as the class views.Frame.
Set the layout of Frame to be BorderLayout.
Set the minimum size to be [400,500] in Properties.
Drag a Split Pane container onto the Frame
Open the properties of the SplitPane (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).
public void setListModel(ListModel listmodel) {
list.setModel(listmodel);
}
public void addListSelectionListener(ListSelectionListener s) {
list.addListSelectionListener(s);
}
public void setText(String text) {
display.setText(text);
display.setCaretPosition(0);
}
//-------------------------------------
public void setListFont(Font font) {
list.setFont(font);
}
public void setListSelectionBackground(Color color) {
list.setSelectionBackground(color);
}
public void setListSelectionForeground(Color color) {
list.setSelectionForeground(color);
}
public void setListCellRenderer(ListCellRenderer rend) {
list.setCellRenderer(rend);
}
public void addHeader(java.awt.Component c) {
add(c, BorderLayout.PAGE_START);
}
Edit memolist.Main and create the usual starter code.
Do a test-run to make sure it looks correct.
package memolist;
import views.*;
public class Main {
private Frame frame = new Frame();
Main() {
frame.setVisible(true);
}
public static void main(String[] args) { new Main(); }
}
Add these classes and .properties files
(the Properties file generator can be found in the Other group).
models/Memo.java
package models;
import java.sql.Timestamp;
import java.text.DateFormat;
public class Memo {
public Integer id;
public String subject;
public String content;
public Timestamp creation;
public Memo(String subject, String content) {
this(null,subject,content,null);
}
Memo(Integer id, String subject, String content, Timestamp creation) {
this.id = id;
this.subject = subject;
this.content = content;
this.creation = creation;
}
@Override
public String toString() {
return DateFormat.getDateTimeInstance().format(creation) + " | " + subject;
}
}
models/Memos.java
package models;
import models.db.*;
import java.sql.*;
import java.util.*;
public class Memos {
private String table_name = getClass().getSimpleName().toLowerCase();
private Database db;
public Memos() throws Exception {
db = new Database();
}
public Memos(Properties props) throws Exception {
db = new Database(props);
}
public List<Memo> fetchAll() throws Exception {
Connection cx = db.connect();
String sql_op = "SELECT * FROM " + table_name;
Statement st = cx.createStatement();
ResultSet rs = st.executeQuery(sql_op);
List<Memo> L = new LinkedList<Memo>();
while (rs.next()) {
Integer id = rs.getInt("id");
String subject = rs.getString("subject");
String content = rs.getString("content");
Timestamp creation = rs.getTimestamp("creation");
L.add(new Memo(id, subject, content, creation));
}
return L;
}
}