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
 

"Keeping Tabs on Your Components"
Vol. 2, Issue 2 P. 62

	

Listing 1.

//*************************************************************

//* handleEvent                                                              *
//*************************************************************

	/**
	* Event handler
	* @param event The event
	*/
	public boolean
	handleEvent( Event event )
	{
		//	Handle tabs nicely...
		if ( event.id == Event.KEY_PRESS && event.key == 9 )
		{
			if ( event.target instanceof Component )
			{
				if ( !event.shiftDown() )
					focusForward();
				else
					focusBackward();
			}

			//	I handled it...
			return( true );
		}

		//	I don't want this...
		return( super.handleEvent( event ) );
	}

Listing 2.

//*************************************************************
//* focusForward                                                             *
//*************************************************************

	/**
	* Sends the focus to the next component
	* @param c The current component
	*/
	public synchronized void
	focusForward()
	{
		//	Go from our current position and find next widget...
		currentPos = findNextComponent( currentPos );

		//	Now set the focus...
		getComponent( currentPos ).requestFocus();

		if ( getComponent( currentPos ) instanceof TextComponent )
			( ( TextComponent )getComponent( currentPos ) ).selectAll();
	}

Listing 3.

//*************************************************************
//* findNextComponent                                                        *
//*************************************************************

	protected int
	findNextComponent( int startPos )
	{
		int			i = startPos + 1;

		do
		{
			//	Check for a wrap...
			if ( i >= countComponents() )
				i = 0;

			Component c = getComponent( i );

			//	Skip anything disabled or labels...
			if ( c instanceof Label || !c.isEnabled() )
			{
				i++;
				continue;
			}

			//	I am the one!
			return( i );
		}
		while ( i != startPos );

		//	Wow, no more!
		return( 0 );
	}
 

 

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.