Subpages: 1. Combo Boxes: JComboBox
2. Basic JComboBox example
3. Custom model and renderer
4. Comboboxes with memory
5. Custom editing
9.4 Comboboxes with memory
In some situations it is desirable to use editable combo boxes which keep a historical list of choices for future reuse. This conveniently allows the user to select a previous choice rather than typing identical text. A typical example of an editable combo box with memory can be found in find/replace dialogs in many modern applications. Another example, familiar to almost every modern computer user, is provided in many Internet browsers which use an editable URL combo box with history mechanism. These combo boxes accumulate typed addresses so the user can easily return to any previously visited site by selecting it from the drop-down list instead of manually typing it in again.
The following example shows how to create a simple browser application using an editable combo box with memory. It uses the serialization mechanism to save data between program sessions, and the JEditorPane component (described in more detail in chapters 11 and 19) to display non-editable HTML files.

Figure 9.3 JComboBox with memory of previously visited URLs.
<<file figure9-3.gif>>
The Code: Browser.java
see \Chapter9\3
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Browser extends JFrame
{
protected JEditorPane m_browser;
protected MemComboBox m_locator;
protected AnimatedLabel m_runner;
public Browser() {
super("HTML Browser [ComboBox with Memory]");
setSize(500, 300);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(new JLabel("Address"));
p.add(Box.createRigidArea(new Dimension(10, 1)));
m_locator = new MemComboBox();
m_locator.load("addresses.dat");
BrowserListener lst = new BrowserListener();
m_locator.addActionListener(lst);
p.add(m_locator);
p.add(Box.createRigidArea(new Dimension(10, 1)));
m_runner = new AnimatedLabel("clock", 8);
p.add(m_runner);
getContentPane().add(p, BorderLayout.NORTH);
m_browser = new JEditorPane();
m_browser.setEditable(false);
m_browser.addHyperlinkListener(lst);
JScrollPane sp = new JScrollPane();
sp.getViewport().add(m_browser);
getContentPane().add(sp, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
m_locator.save("addresses.dat");
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
m_locator.grabFocus();
}
class BrowserListener implements ActionListener, HyperlinkListener
{
public void actionPerformed(ActionEvent evt) {
String sUrl = (String)m_locator.getSelectedItem();
if (sUrl == null || sUrl.length() == 0 ||
m_runner.getRunning())
return;
BrowserLoader loader = new BrowserLoader(sUrl);
loader.start();
}
public void hyperlinkUpdate(HyperlinkEvent e) {
URL url = e.getURL();
if (url == null || m_runner.getRunning())
return;
BrowserLoader loader = new BrowserLoader(url.toString());
loader.start();
}
}
class BrowserLoader extends Thread
{
protected String m_sUrl;
public BrowserLoader(String sUrl) { m_sUrl = sUrl; }
public void run() {
setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
m_runner.setRunning(true);
try {
URL source = new URL(m_sUrl);
m_browser.setPage(source);
m_locator.add(m_sUrl);
}
catch (Exception e) {
JOptionPane.showMessageDialog(Browser.this,
"Error: "+e.toString(),
"Warning", JOptionPane.WARNING_MESSAGE);
}
m_runner.setRunning(false);
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public static void main(String argv[]) { new Browser(); }
}
class MemComboBox extends JComboBox
{
public static final int MAX_MEM_LEN = 30;
public MemComboBox() {
super();
setEditable(true);
}
public void add(String item) {
removeItem(item);
insertItemAt(item, 0);
setSelectedItem(item);
if (getItemCount() > MAX_MEM_LEN)
removeItemAt(getItemCount()-1);
}
public void load(String fName) {
try {
if (getItemCount() > 0)
removeAllItems();
File f = new File(fName);
if (!f.exists())
return;
FileInputStream fStream =
new FileInputStream(f);
ObjectInput stream =
new ObjectInputStream(fStream);
Object obj = stream.readObject();
if (obj instanceof ComboBoxModel)
setModel((ComboBoxModel)obj);
stream.close();
fStream.close();
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Serialization error: "+e.toString());
}
}
public void save(String fName) {
try {
FileOutputStream fStream =
new FileOutputStream(fName);
ObjectOutput stream =
new ObjectOutputStream(fStream);
stream.writeObject(getModel());
stream.flush();
stream.close();
fStream.close();
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Serialization error: "+e.toString());
}
}
}
class AnimatedLabel extends JLabel implements Runnable
{
protected Icon[] m_icons;
protected int m_index = 0;
protected boolean m_isRunning;
public AnimatedLabel(String gifName, int numGifs) {
m_icons = new Icon[numGifs];
for (int k=0; k<numGifs; k++)
m_icons[k] = new ImageIcon(gifName+k+".gif");
setIcon(m_icons[0]);
Thread tr = new Thread(this);
tr.setPriority(Thread.MAX_PRIORITY);
tr.start();
}
public void setRunning(boolean isRunning) {
m_isRunning = isRunning;
}
public boolean getRunning() { return m_isRunning; }
public void run() {
while(true) {
if (m_isRunning) {
m_index++;
if (m_index >= m_icons.length)
m_index = 0;
setIcon(m_icons[m_index]);
Graphics g = getGraphics();
m_icons[m_index].paintIcon(this, g, 0, 0);
}
else {
if (m_index > 0) {
m_index = 0;
setIcon(m_icons[0]);
}
}
try { Thread.sleep(500); } catch(Exception ex) {}
}
}
}
Understanding the Code
Class Browser
This class extends JFrame to implement the frame container for our browser. Instance variables:
JEditorPane m_browser: text component to parse and render HTML files.
MemComboBox m_locator: combo box to enter/select URL address.
AnimatedLabel m_runner: traditional animated icon alive while the browser is requesting a URL.
The constructor creates the custom combo box, m_locator, and an associated explanatory label. Then it creates the m_runner icon and places all three components in the northern region of our frame's content pane. JEditorPane m_browser is created and placed in a JScrollPane to provide scrolling capabilities. This is then added to the center of the content pane.
Note that the WindowListener, as used in many previous examples to close the frame and terminate execution, receives an additional function: it invokes our custom save() method (see below) on our custom combo box component before destroying the frame. This saves the list of visited URLs entered as a file called "addresses.dat" in the current running directory.
Class Browser.BrowserListener
This inner class implements both the ActionListener and HyperlinkListener interfaces to manage navigation to HTML pages. The actionPerformed() method is invoked when the user selects a new item in the combo box . It verifies that the selection is valid and the browser is not currently running (i.e. requesting a URL). If these checks are passed it then creates and starts a new BrowserLoader instance (see below) for the specified address.
Method hyperlinkUpdate() is invoked when the user clicks a hyperlink in the currently loaded web page. This method also determines the selected URL address and starts a new BrowserLoader to load it.
Class Browser.BrowserLoader
This inner class extends Thread to load web pages into the JEditorPane component. It takes a URL address parameter in the constructor and stores it in a instance variable. The run() method sets the mouse cursor to hourglass (Cursor.WAIT_CURSOR) and starts the animated icon to indicate that the browser is busy.
The core functionality of this thread is enclosed in its try/catch block. If an exception occurs during processing of the requested URL, it is displayed in simple dialog message box (we will learn discuss JOptionPane in chapter 14).
The actual job of retrieving, parsing, and rendering the web page is hidden in a single call to the setPage() method. So why do we need to create this separate thread instead of making that simple call, say, in BrowserListener? The reason is, as we discussed in chapter 2, by creating separate threads to do potentially time-consuming operations we avoid clogging up the event-dispatching thread.
Class MemComboBox
This class extends JComboBox to add a historical mechanism for this component. The constructor creates an underlying JComboBox component and sets its editable property to true.
The add() method adds a new text string to the beginning of the list. If this item is already present in the list, it is removed from the old position. If the resulting list is longer than the pre-defined maximum length then the last item in the list is truncated.
Method load() loads a previously stored ComboBoxModel from file "addresses.dat" using the serialization mechanism. The significant portion of this method reads an object from an ObjectInputStream and sets it as the ComboBoxModel. Note that any possible exceptions are only printed to the standard output and purposefully do not distract the user (since this serialization mechanism should be considered an optional feature).
Similarly, the save() method serializes our combo box's ComboBoxModel. Any possible exceptions are, again, printed to standard output and do not distract the user.
Class AnimatedLabel
Surprisingly, Swing does not provide any special support for animated components, so we have to create our own component for this purpose. This provides us with an interesting example of using threads in Java.
Note: Animated GIFs are fully supported by ImageIcon (see chapter 5) but we want complete control over each animated frame here.
AnimatedLabel extends JLabel and implements the Runnable interface. Instance variables:
Icon[] m_icons: an array of images to be used for animation.
int m_index: index of the current image.
boolean m_isRunning: flag indicating whether the animation is running.
The constructor takes a common name of a series of GIF files containing images for animation, and the number of those files. These images are loaded and stored into an array. When all images are loaded a thread with maximum priority is created and started to run this Runnable instance.
The setRunning() and getRunning() methods simply manage the m_isRunning flag.
In the run() method we cyclically increment the m_index variable and draw an image from the m_icons array with the corresponding index, exactly as you would expect from an animated image. This is done only when the m_isRunning flag is set to true. Otherwise, the image with index 0 is displayed. After an image is painted, AnimatedLabel yields control to other threads and sleeps for 500 ms.
The interesting thing about this component is that it runs in parallel with other threads which do not necessary yield control explicitly. In our case the concurrent BrowserLoader thread spends the main part of its time inside the setPage() method, and our animated icon runs in a separate thread signaling to the user that something is going on. This is made possible because this animated component is running in the thread with the maximum priority. Of course, we should use such thread priority with caution. In our case it is appropriate since our thread consumes only a small amount of the processor's time and does yield control to the lesser-priority threads (when it sleeps).
Note: As a good exercise try using threads with normal priority or Swing's Timer component in this example. You will find that this doesn't work as expected: the animated icon does not show any animation while the browser is running.
Running the Code
Figure 9.3 shows the Browser application displaying a web page. Note that the animated icon comes to life when the browser requests a URL. Also note how the combo box is populated with URL addresses as we navigate to different web pages. Now quit the application and re-start it. Note that our addresses have been saved and restored (by serializing the combo box model, as discussed above).
Note: HTML rendering functionality is not yet matured. Do not be surprised if your favorite web page looks signigicantly different in our Swing-based browser. As a matter of fact even the JavaSoft home page throws several exceptions while being displayed in this Swing component. (These exceptions occur outside our code, during the JEditorPane rendering--this is why they are not caught and handled by our code.)
UI Guideline : Usage of a Memory Combobox
The example given here is a good usage for such a device. However, a memory combobox will not always be appropriate. Remember the advice that usability of an unsorted comboboxes tends to degrade rapidly as the number of items grows. Therefore, it is sensible to deploy this technique where the likelihood of more than say 20 entries is very small. The browser example is good because it is unlikely that a user would type more than 20 URLs in a single web surfing session.
Where you have a domain problem which is likely to need a larger number of memory items but you still want to use a memory combobox, consider adding a sorting algorithm, so that rather than most recent first, you sort into a meaningful index such as alphabetical order. This will improve usability and mean that you could easily populate the list up to 2 or 3 hundred items.



RSS feed Java FAQ News