HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Favorites Combo Box"
Vol. 6, Issue 8, p. 52

	


Listing 1

package com.chicagojava.awp.client.components.combobox;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


/*************************************************<br>
<code>FavoritesComboBox</code> is a combobox widget that visually displays two separate lists: 1) the entire list of selections and 2) a list of favorite (or more recently selected) choices. This combobox widget mimics the behavior of the "Font List" combobox in Microsoft Word.
*************************************************/
public class FavoritesComboBox extends JComboBox
{
  /***********************************************
Constructs the <code>FavoritesComboBox</code> and sets its associated model. @param aModel the combobox's model
  ***********************************************/
  public FavoritesComboBox(FavoritesComboBoxModel
    aModel)
  {
    super();
    setRenderer(createFavoritesComboBoxRenderer());
    addFocusListener(
      new FavoritesComboBoxFocusAdapter(aModel));
    setModel(aModel);
  }


  /***********************************************
Sets the maximum number of items the most recent favorites list can hold. @param aMaximumNumber the maximum number of items in the list
  ***********************************************/
  public void setMaximumNumberOfFavorites(int
    aMaximumNumber)
  {
    ((FavoritesComboBoxModel)
      getModel()).setMaximumNumberOfFavorites(
      aMaximumNumber);
  }


  /***********************************************
Returns the maximum number of items the most recent favorites list can hold. @return the maximum number of selections allowed in recent list
  ***********************************************/
  public int getMaximumNumberOfFavorites()
  {
    return ((FavoritesComboBoxModel)
      getModel()).getMaximumNumberOfFavorites();
  }


  /***********************************************
Returns the currently selected item. This method is overridden to invoke the <code> FavoritesComboBoxModel's getCurrentSelection() </code> method. @return the currently selected list object from the data model
  ***********************************************/
  public Object getSelectedItem()
  {
    return ((FavoritesComboBoxModel)
      getModel()).getCurrentSelection();
  }


  /***********************************************
A factory method that creates the default renderer to be used for the combobox. Subclasses should override this method in order to provide their own renderer. @return the default list cell renderer
  ***********************************************/
  protected ListCellRenderer
    createFavoritesComboBoxRenderer()
  {
    return new FavoritesListCellRenderer();
  }
}



Listing 2

 //a static inner class found in
//FavoritesRecentComboBoxModel
protected static class Wrapper
    implements Serializable
{
  private Object wrappee;


  public Wrapper(Object aWrappee)
  {
    this.wrappee = aWrappee;
  }


  public boolean extendedEquals( Object o )
  {
    return equals( o )
      || wrappee.equals( o )
      || (o instanceof Wrapper &&
      wrappee.equals( ((Wrapper)o).wrappee) );
  }


  public String toString()
  {
    return wrappee.toString();
  }
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.