FileDocCategorySizeDatePackage
ChainedConstructors.javaAPI DocExample3851Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.review

ChainedConstructors

public class ChainedConstructors extends JButton implements ActionListener
Demonstration of chained constructors.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.5 $

Fields Summary
Constructors Summary
public ChainedConstructors(Class dataType)
An illegal chained constructor showing attempt to use instance method.

param
dataType the dataType of the class.

		// this(buildClassText(dataType), dataType.getName(), this);
		// supress eclipse warnings.
		dataType.getClass();
	
public ChainedConstructors(String text)
A chained construtor showing use of embedded code.

param
text Display text for the button.

		this(text, new String("A button to show " + text), null);
	
public ChainedConstructors(Color color)
A chained constructor showing usage of an instance method.

param
color The default color to show.

		this(color.toString(), "", null);
	
public ChainedConstructors(String text, boolean showDate)
A chained constructor showing usage of a static method.

param
text Display text for the button.
param
showDate Whether to show the creation date in the button's tooltip.

		this(text, buildDateToolTip(text, showDate), null);
	
public ChainedConstructors(String text, String tooltip)
A chained consturctor showign a simple call.

param
text Display text for the button.
param
tooltip Tool Tip for the Button.

		this(text, tooltip, null);
	
public ChainedConstructors(String text, String tooltip, ActionListener listener)
The Primary constructor.

param
text Display text for the button.
param
tooltip Tool Tip for the Button.
param
listener Listener to the Button.

		setText(text);
		setToolTipText(tooltip);
		if (listener != null) {
			addActionListener(listener);
		}
	
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent event)
{@inheritDoc}

		// just for demo reasons.
	
protected java.lang.StringbuildClassText(java.lang.Class dataType)
Extract a short class name from the full name.

param
dataType The class to operate on.
return
the tool tip built.

		return dataType.getName()
		               .substring(dataType.getName().lastIndexOf('.") + 1);
	
private static java.lang.StringbuildDateToolTip(java.lang.String text, boolean showDate)
Helper method for date based tooltip construction.

param
text Display text for the button.
param
showDate Whether to show the creation date in the button's tooltip.
return
the constructed tooltip.

		final StringBuffer buf = new StringBuffer(250);
		buf.append("A Button to show ");
		buf.append(text);
		if (showDate) {
			buf.append(" created on ");
			DateFormat df = DateFormat.getInstance();
			buf.append(df.format(Calendar.getInstance().getTime()));
		}
		return buf.toString();