Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 9. (The basics) Combo Boxes. Easy for reading, Click here!

Custom Search
Swing Chapter 9. (The basics) Combo Boxes. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 4/5 



Previous Page Previous Page (3/5) - Next Page (5/5) Next Page
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.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy