Article View: borland.public.jbuilder.java.swing
Article #11634Problems with field traversal of Swing components
From: Scott Lavender
Date: Mon, 03 May 1999 00:00
Date: Mon, 03 May 1999 00:00
157 lines
5572 bytes
5572 bytes
To All,
Attached is a test class that I created to demonstrate the problems that we
have been investigating regarding field traversal of Swing components. It
seems that when a JTable is contained with a JScrollPane, the user must use
the TAB key twice in order to get the JTable into focus. I discovered that if
the JTable is placed into a JPanel and then the JPanel is placed into the
JScrollPane, the user only has to hit the TAB key once.
// Beginning of TabTestPanel Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import com.indcomp.foundation.*;
public class TabTestPanel extends JPanel
{
JScrollPane scrollPane = null;
JTextField field = new JTextField( 10 );
JTable table = null;
JPanel innerPanel = null;
// FocusListener for use by all components
FocusListener focusListener = new FocusEventHandler();
/** * Creates a new instance. */ public TabTestPanel() { // Setup a
JTable TableModel dataModel = new AbstractTableModel() { public int
getColumnCount() { return 5; } public int getRowCount() { return 4;} public
Object getValueAt(int row, int col) { return new String( row + ", " + col );
} };
table = new JTable( dataModel );
/*
* Add JTable to JScrollPane
*
* Test 1: This test consists of a JTable contained in a ScrollPane.
*
* Test 2: This test consists of a JTable contained in a JPanel
* and then added to a ScrollPane.
*
* Test 3: This test code will print the parent hierarchy from the
* JTree upwards.
*
* NOTE: For the following tests, comment out comment marks
* for the test to perform. Then uncomment the comment marks for
* the test to disable.
*/
// Test 1: ScrollPane->JTable
// DOES NOT WORK!!!
///*
scrollPane = new JScrollPane( table );
//*/
// Test 2: ScrollPane->JPanel->JTable
// WORKS!!!
/*
innerPanel = new JPanel();
innerPanel.add( table );
innerPanel.addFocusListener( focusListener );
scrollPane = new JScrollPane( innerPanel );
*/
// Assign FocusListener to all components
field.addFocusListener( focusListener );
scrollPane.addFocusListener( focusListener );
scrollPane.getViewport().addFocusListener( focusListener );
table.addFocusListener( focusListener );
// Add JTextField and JScrollPane to JPanel
add( field );
add( scrollPane );
// Test 3: List parents of JTable
/*
System.out.println( "\nParents of JTable..." );
printParentOfJComponent( table );
*/
}
private void printParentOfJComponent( JComponent jComponent )
{
if( jComponent == null )
return;
Component parent = jComponent.getParent();
if( (parent != null) && (parent instanceof JComponent) )
{
System.out.println( "\t" + parent.getClass().getName() );
printParentOfJComponent( (JComponent)parent );
}
}
//
// Begin - Inner classes
//
/**
* Class to handle focus events.
*/
private class FocusEventHandler extends FocusAdapter
{
/**
* Invoked when a component gains the keyboard focus.
*/
public void focusGained(FocusEvent e)
{
System.out.println( "\nTabTestPanel:FocusEventHandler.focusGained
for " + ((JComponent)e.getComponent()).getClass().getName() );
}
/** * Invoked when a component loses the keyboard focus. */ public void
focusLost(FocusEvent e) { System.out.println(
"TabTestPanel:FocusEventHandler.focusLost for " +
((JComponent)e.getComponent()).getClass().getName() ); }
}
//
// End - Inner classes
//
/**
* Test code.
*
* @param args String array of command line arguments.
*/
public static void main( String[] args )
{
JFrame frame = new JFrame( "Test" );
frame.getContentPane().add( new TabTestPanel() );
frame.pack();
frame.setVisible( true );
}
}
// End of TabTestPanel Class
If anyone has some ideas about this problem, please let me know.
----------------------------------
Scott Lavender
ICC/GR Software
100 Deerfield Point
12725 Morris Road, Suite 300
Alpharetta, GA 30004
----------------------------------
Voice: 770-610-4166
Fax: 770-475-5126
Email: lavenders@genrad.com
----------------------------------
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Technical Details
Message-ID:
<7gk4i7$pq5$1@nnrp1.dejanews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,c3da7e46006cd7c9,start X-Google-Attributes: gid111d45,public From: Scott Lavender <scott.lavender@att.net> Subject: Problems with field traversal of Swing components Date: 1999/05/03 Message-ID: <7gk4i7$pq5$1@nnrp1.dejanews.com> X-Deja-AN: 473473571 X-Http-Proxy: 1.0 x9.dejanews.com:80 (Squid/1.1.22) for client 198.182.182.73 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Mon May 03 12:24:42 1999 GMT Newsgroups: borland.public.jbuilder.java.swing X-Http-User-Agent: Mozilla/4.5 [en] (WinNT; I)