|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JPanel | +--FontSelectionPanel
A component which allows a user to select a font. Here is a code sample
demonstrating its use:
Copyright (C) 2000 Greg Merrill (
greghmerrill@yahoo.com).
Distributed under the terms of the GNU General Public License (version 2).
For details on the GNU GPL, please visit
http://www.gnu.org/copyleft/gpl.html.
To find out more about this and other free software by Greg Merrill,
please visit http://gregmerrill.imagineis.com
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FontSelectionPanelDemo {
public static void main (String[] args) {
final JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
final FontSelectionPanel fontSelectionPanel = new FontSelectionPanel(
new Font("Times New Roman", Font.BOLD+Font.ITALIC, 14)
);
panel.add(fontSelectionPanel, BorderLayout.CENTER);
JButton button = new JButton("OK");
button.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent e) {
try {
JOptionPane.showMessageDialog(
frame,
"Selected font is: " + fontSelectionPanel.getSelectedFont(),
"Selected Font",
JOptionPane.INFORMATION_MESSAGE
);
}
catch (FontSelectionPanel.InvalidFontException ife) {
JOptionPane.showMessageDialog(
frame,
"You have not selected a valid font",
"Invalid Font",
JOptionPane.ERROR_MESSAGE
);
}
}
});
panel.add(button, BorderLayout.SOUTH);
frame.setContentPane(panel);
frame.addWindowListener(new WindowAdapter () {
public void windowClosing (WindowEvent e) { System.exit(0); }
});
frame.pack();
frame.show();
}
}
Version History:
Added methods
setSelectedFont(java.awt.Font)
,
setSelectedFontFamily(String)
,
setSelectedFontStyle(int)
,
setSelectedFontSize(int)
GridBag fill strategy modified slightly to improve resizing behavior
Changed sample code to use BorderLayout
Added version history to javadocs
Inner Class Summary | |
static interface |
FontSelectionPanel.InvalidFontException
Indicates that an invalid font is currently specified |
static interface |
FontSelectionPanel.InvalidFontSizeException
Indicates that an invalid font size is currently specified |
static interface |
FontSelectionPanel.NoFontFamilySelectedException
Indicates that no font family is currently selected |
static interface |
FontSelectionPanel.NoFontSizeSpecifiedException
Indicates that no font size is currently specified |
static interface |
FontSelectionPanel.NoFontStyleSelectedException
Indicates that no font style is currently selected |
static class |
FontSelectionPanel.PhraseCanvas
Component for displaying a "phrase" (a brief, one or two word String) using a particular font & a particular color. |
Inner classes inherited from class javax.swing.JComponent |
javax.swing.JComponent.AccessibleJComponent |
Fields inherited from class javax.swing.JComponent |
TOOL_TIP_TEXT_KEY,
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 |
Constructor Summary | |
FontSelectionPanel()
Like FontSelectionPanel(java.awt.Font) , except an initialFont of
null will be used. |
|
FontSelectionPanel(java.awt.Font initialFont)
Like FontSelectionPanel(java.awt.Font, String[], int[]) , except that
a default list of styles
({"Plain", "Bold", "Italic", "Bold Italic"} ) and font
sizes ({8, 9, 10, 12, 14} ) will be used. |
|
FontSelectionPanel(java.awt.Font initialFont,
java.lang.String[] styleDisplayNames,
int[] predefinedSizes)
Construct a new FontSelectionPanel whose family, style & size widget selections are set according to the supplied initial Font. |
Method Summary | |
void |
addObserver(java.util.Observer o)
Adds an Observer to this FontSelectionPanel; the supplied Observer will have its update() method called any time the Font currently specified in the FontSelectionPanel changes. |
void |
deleteObserver(java.util.Observer o)
Removes an Observer from this FontSelectionPanel. |
java.awt.Font |
getSelectedFont()
Returns the currently selected font. |
java.lang.String |
getSelectedFontFamily()
Returns the currently selected font family |
int |
getSelectedFontSize()
Returns the currently selected font size. |
int |
getSelectedFontStyle()
Returns the currently selected font style. |
void |
setSelectedFont(java.awt.Font font)
Changes the currently selected font by assigning all widget values to match the family/style/size values of the supplied font |
void |
setSelectedFontFamily(java.lang.String family)
Sets the currently selected font family. |
void |
setSelectedFontSize(int size)
Sets the currently selected font size. |
void |
setSelectedFontStyle(int style)
Sets the currently selected font style. |
Methods inherited from class javax.swing.JPanel |
getAccessibleContext,
getUIClassID,
updateUI |
Methods inherited from class javax.swing.JComponent |
addAncestorListener,
addNotify,
addPropertyChangeListener,
addPropertyChangeListener,
addVetoableChangeListener,
computeVisibleRect,
contains,
createToolTip,
firePropertyChange,
firePropertyChange,
firePropertyChange,
firePropertyChange,
firePropertyChange,
firePropertyChange,
firePropertyChange,
firePropertyChange,
getActionForKeyStroke,
getAlignmentX,
getAlignmentY,
getAutoscrolls,
getBorder,
getBounds,
getClientProperty,
getConditionForKeyStroke,
getDebugGraphicsOptions,
getGraphics,
getHeight,
getInsets,
getInsets,
getLocation,
getMaximumSize,
getMinimumSize,
getNextFocusableComponent,
getPreferredSize,
getRegisteredKeyStrokes,
getRootPane,
getSize,
getToolTipLocation,
getToolTipText,
getToolTipText,
getTopLevelAncestor,
getVisibleRect,
getWidth,
getX,
getY,
grabFocus,
hasFocus,
isDoubleBuffered,
isFocusCycleRoot,
isFocusTraversable,
isLightweightComponent,
isManagingFocus,
isOpaque,
isOptimizedDrawingEnabled,
isPaintingTile,
isRequestFocusEnabled,
isValidateRoot,
paint,
paintImmediately,
paintImmediately,
print,
printAll,
putClientProperty,
registerKeyboardAction,
registerKeyboardAction,
removeAncestorListener,
removeNotify,
removePropertyChangeListener,
removePropertyChangeListener,
removeVetoableChangeListener,
repaint,
repaint,
requestDefaultFocus,
requestFocus,
resetKeyboardActions,
reshape,
revalidate,
scrollRectToVisible,
setAlignmentX,
setAlignmentY,
setAutoscrolls,
setBackground,
setBorder,
setDebugGraphicsOptions,
setDoubleBuffered,
setEnabled,
setFont,
setForeground,
setMaximumSize,
setMinimumSize,
setNextFocusableComponent,
setOpaque,
setPreferredSize,
setRequestFocusEnabled,
setToolTipText,
setVisible,
unregisterKeyboardAction,
update |
Methods inherited from class java.awt.Container |
add,
add,
add,
add,
add,
addContainerListener,
countComponents,
deliverEvent,
doLayout,
findComponentAt,
findComponentAt,
getComponent,
getComponentAt,
getComponentAt,
getComponentCount,
getComponents,
getLayout,
insets,
invalidate,
isAncestorOf,
layout,
list,
list,
locate,
minimumSize,
paintComponents,
preferredSize,
printComponents,
remove,
remove,
removeAll,
removeContainerListener,
setCursor,
setLayout,
validate |
Methods inherited from class java.awt.Component |
action,
add,
addComponentListener,
addFocusListener,
addInputMethodListener,
addKeyListener,
addMouseListener,
addMouseMotionListener,
bounds,
checkImage,
checkImage,
contains,
createImage,
createImage,
disable,
dispatchEvent,
enable,
enable,
enableInputMethods,
getBackground,
getBounds,
getColorModel,
getComponentOrientation,
getCursor,
getDropTarget,
getFont,
getFontMetrics,
getForeground,
getInputContext,
getInputMethodRequests,
getLocale,
getLocation,
getLocationOnScreen,
getName,
getParent,
getPeer,
getSize,
getToolkit,
getTreeLock,
gotFocus,
handleEvent,
hide,
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,
remove,
removeComponentListener,
removeFocusListener,
removeInputMethodListener,
removeKeyListener,
removeMouseListener,
removeMouseMotionListener,
repaint,
repaint,
repaint,
resize,
resize,
setBounds,
setBounds,
setComponentOrientation,
setDropTarget,
setLocale,
setLocation,
setLocation,
setName,
setSize,
setSize,
show,
show,
size,
toString,
transferFocus |
Methods inherited from class java.lang.Object |
equals,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Constructor Detail |
public FontSelectionPanel()
FontSelectionPanel(java.awt.Font)
, except an initialFont of
null
will be used.public FontSelectionPanel(java.awt.Font initialFont)
FontSelectionPanel(java.awt.Font, String[], int[])
, except that
a default list of styles
({"Plain", "Bold", "Italic", "Bold Italic"}
) and font
sizes ({8, 9, 10, 12, 14}
) will be used.initialFont
- see
FontSelectionPanel(java.awt.Font, String[], int[])
public FontSelectionPanel(java.awt.Font initialFont, java.lang.String[] styleDisplayNames, int[] predefinedSizes)
initialFont
- the newly constructed FontSelectionPanel's family,
style, and size widgets will be set according to this value. This value
may be null, in which case an initial font will be automatically created.
This auto-created font will have a family, style, and size corresponding
to the first avaiable value in the widget form family, style, and size
respectively.styleDisplayNames
- must contain exactly four members. The members
of this array represent the following styles, in order: Font.PLAIN,
Font.BOLD, Font.ITALIC, and Font.BOLD+Font.ITALICpredefinedSizes
- must contain one or more predefined font sizes which
will be available to the user as a convenience for populating the font
size text field; all values must be greater than 0.Method Detail |
public void addObserver(java.util.Observer o)
o
- observer to be addedObserver
public void deleteObserver(java.util.Observer o)
o
- Observer to be removedObserver
public java.lang.String getSelectedFontFamily() throws FontSelectionPanel.NoFontFamilySelectedException
public int getSelectedFontStyle() throws FontSelectionPanel.NoFontStyleSelectedException
Font
public int getSelectedFontSize() throws FontSelectionPanel.NoFontSizeSpecifiedException, FontSelectionPanel.InvalidFontSizeException
public java.awt.Font getSelectedFont() throws FontSelectionPanel.InvalidFontException
FontSelectionPanel.InvalidFontException
,
FontSelectionPanel.NoFontFamilySelectedException
,
FontSelectionPanel.NoFontStyleSelectedException
,
FontSelectionPanel.NoFontSizeSpecifiedException
,
or FontSelectionPanel.InvalidFontSizeException
public void setSelectedFont(java.awt.Font font)
font
- font whose values should be used to set widgetspublic void setSelectedFontFamily(java.lang.String family)
family
- family to which selection should changepublic void setSelectedFontStyle(int style)
style
- style to which selection should changepublic void setSelectedFontSize(int size)
size
- size to which selection should change
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |