util.ui
Class ComponentTools

java.lang.Object
  extended by util.ui.ComponentTools

public class ComponentTools
extends Object

Common tools for Swing gui components.

Version:
September 1, 2007
Author:
Damian Johnson (atagar1@gmail.com)

Constructor Summary
ComponentTools()
           
 
Method Summary
static GradientPaint applyThemedScheme(JComponent component, Color reference, boolean isPressed)
          Applies a highly customized visual scheme based on the reference color.
static void center(Window mover)
          Centers a window within the center of the screen.
static void center(Window mover, Component target)
          Moves a window to be concentric with another.
static JDialog makeDialog(Component parent, String title)
          Generates a modal dialog capable of having either a Frame or Dialog parent.
static JButton makeThemedButton(String text, Color reference)
          Provides a visually customized button utilizing the applyThemedScheme method that will update its theme accordingly when pressed.
static void setKeyBinding(JButton button, KeyStroke event)
          Binds a given keystroke to click the button when the button's in the focused window.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ComponentTools

public ComponentTools()
Method Detail

applyThemedScheme

public static GradientPaint applyThemedScheme(JComponent component,
                                              Color reference,
                                              boolean isPressed)
Applies a highly customized visual scheme based on the reference color. Background is of a gradient hue (similar to the Java 6 default ocean theme) and uses a raised bevel border. If designated as being pressed then this uses a lowered bevel border and the background darkens, unless the background is already very dark, in which case it will lighten. Unfortunately Swing doesn't directly support setting background gradients, requiring that the component's paint method is overridden. For instance, to apply this to a JButton:
JButton button = new JButton(text) { private static final long serialVersionUID = 0; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setPaint(applyThemedScheme(this, reference, getModel().isArmed())); g2.fillRect(0, 0, getWidth(), getHeight()); // Draws gradient background super.paintComponent(g); // Draws button content } }; button.setContentAreaFilled(false); // Disables default background

Parameters:
component - component to which custom foreground and border is applied, if null then these attributes aren't applied
reference - color on which background gradient and border are based
isPressed - determines if toggled scheme is applied for components that can be pressed
Returns:
component background gradient

makeThemedButton

public static JButton makeThemedButton(String text,
                                       Color reference)
Provides a visually customized button utilizing the applyThemedScheme method that will update its theme accordingly when pressed. Any content is anti-aliased.

Parameters:
text - message displayed by the button
reference - color on which background gradient and border are based
Returns:
button with a color scheme matching the provided color

center

public static void center(Window mover)
Centers a window within the center of the screen.

Parameters:
mover - window to be centered

center

public static void center(Window mover,
                          Component target)
Moves a window to be concentric with another.

Parameters:
mover - window to be centered
target - to be centered within

setKeyBinding

public static void setKeyBinding(JButton button,
                                 KeyStroke event)
Binds a given keystroke to click the button when the button's in the focused window.

Parameters:
button - button to be bound to keystroke
event - type of keyboard event that triggers the button

makeDialog

public static JDialog makeDialog(Component parent,
                                 String title)
Generates a modal dialog capable of having either a Frame or Dialog parent.

Parameters:
parent - the parent component of the dialog
title - title of dialog
Returns:
dialog with the specified parent