com.lamatek.swingextras
Class JFontChooser

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--com.lamatek.swingextras.JFontChooser
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener, java.awt.image.ImageObserver, javax.swing.event.ListSelectionListener, java.awt.MenuContainer, java.io.Serializable

public class JFontChooser
extends javax.swing.JComponent
implements java.awt.event.ActionListener, javax.swing.event.ListSelectionListener

A simple FontChooser dialog that implements similar functionality to the JFileChooser, JOptionPane and JColorChooser components provided with Swing.

Upon initialization, the JFontChooser polls the system for all available fonts, initializes the various JList components to the values of the default font and provides a preview of the font. As options are changed/selected the preview is updated to display the current font.

JFileChooser can either be created and added to a GUI as a typical JComponent or it can display a JDialog using the showDialog method (just like the JFileChooser component). Objects that extend JFontChooser should use the acceptSelection and cancelSelection methods to determine either an accept or cancel selection.

Example:

JFontChooser chooser = new JFontChooser(new Font("Arial", Font.BOLD, 12));
if (chooser.showDialog(this, "Choose a font...") == JFontChooser.ACCEPT_OPTION) {
  Font f = chooser.getSelectedFont();
  // Process font here...
}

See Also:
Serialized Form

Inner classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Field Summary
static int ACCEPT_OPTION
          Value returned by showDialog upon a selected font.
static int CANCEL_OPTION
          Value returned by showDialog upon a cancel.
static int ERROR_OPTION
          Value returned by showDialog upon an error.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JFontChooser(java.awt.Font font)
          Constructs a new JFontChooser component initialized to the supplied font object.
 
Method Summary
 void acceptSelection()
          This method is called when the user presses the okay button, selecting the currently displayed font.
 void actionPerformed(java.awt.event.ActionEvent e)
          Processes action events from the okay and cancel buttons as well as the current size TextField.
 void cancelSelection()
          This method is called when the user presses the cancel button.
 java.awt.Font getSelectedFont()
          Returns the currently selected font.
 int showDialog(java.awt.Component parent, java.lang.String title)
          Pops up a Font chooser dialog with the supplied title, centered about the component parent.
 void valueChanged(javax.swing.event.ListSelectionEvent e)
          Processes events received from the various JList objects.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getUIClassID, getVerifyInputWhenFocusTarget, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, hide, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ERROR_OPTION

public static final int ERROR_OPTION
Value returned by showDialog upon an error.

ACCEPT_OPTION

public static final int ACCEPT_OPTION
Value returned by showDialog upon a selected font.

CANCEL_OPTION

public static final int CANCEL_OPTION
Value returned by showDialog upon a cancel.
Constructor Detail

JFontChooser

public JFontChooser(java.awt.Font font)
Constructs a new JFontChooser component initialized to the supplied font object.
See Also:
showDialog(Component, String)
Method Detail

getSelectedFont

public java.awt.Font getSelectedFont()
Returns the currently selected font. Typically called after receipt of an ACCEPT_OPTION (using the showDialog option) or from within the approveSelection method (using the component option).
Returns:
java.awt.Font A font class that represents the currently selected font.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Processes action events from the okay and cancel buttons as well as the current size TextField.
Specified by:
actionPerformed in interface java.awt.event.ActionListener

valueChanged

public void valueChanged(javax.swing.event.ListSelectionEvent e)
Processes events received from the various JList objects.
Specified by:
valueChanged in interface javax.swing.event.ListSelectionListener

showDialog

public int showDialog(java.awt.Component parent,
                      java.lang.String title)
Pops up a Font chooser dialog with the supplied title, centered about the component parent.
Returns:
int An integer that equates to the static variables ERROR_OPTION, ACCEPT_OPTION or CANCEL_OPTION.

acceptSelection

public void acceptSelection()
This method is called when the user presses the okay button, selecting the currently displayed font. Children of JFontChooser should override this method to process this event.

cancelSelection

public void cancelSelection()
This method is called when the user presses the cancel button. Children of JFontChooser should override this method to process this event.