The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Three ways of doing this..

Posted by Kishori Sharan on April 03, 2001 at 11:31 AM

Hi
There are at least three ways to accomplish this.
1. Add a keylistener to your textarea and in keypressed event set the focus to next component and consume the key press event if Tab key is pressed.
2. Override the isManagingFocus method in JTextArea and then return false.
3. Override processKeyEvent method in JTextArea and then check if Tab key is pressed the don't do anything . For other key pressed just call the super event.
All three methods have been illustrated below.
Thanx
Kishori
/////////////////////////////////////////////////////////////////
/// Capturing the keyPressed event and consuming the event////
/////////////////////////////////////////////////////////////////
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;

public class FrameTest extends JFrame {
JTextArea ta = new JTextArea ( 5, 20 ) ;
JButton b = new JButton ( "Test" ) ;

public FrameTest ( ) {
Container cp = getContentPane ( ) ;
cp.add ( ta , "North" ) ;
cp.add ( b, "South" ) ;

ta.addKeyListener (
new KeyAdapter ( ) {
public void keyPressed ( KeyEvent e ) {
if ( e.getKeyCode() == KeyEvent.VK_TAB ) {
// Tansfer the focus to another componenet
FrameTest.this.ta.transferFocus ( ) ;
// Consume the event
e.consume() ;
}
}
}
);

}
}

////////////////////////////////////////////////////////////////////////
// Overriding isManagingFocus method of JTextArea
///////////////////////////////////////////////////////////////////////
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;

class CstTextArea extends JTextArea {
CstTextArea ( int r, int c ) {
super ( r, c ) ;
}

public boolean isManagingFocus ( ) {
return false ;
}
}

public class FrameTest extends JFrame {
CstTextArea ta = new CstTextArea ( 5, 20 ) ;
JButton b = new JButton ( "Test" ) ;

public FrameTest ( ) {
Container cp = getContentPane ( ) ;
cp.add ( ta , "North" ) ;
cp.add ( b, "South" ) ;
}


public static void main ( String[] args ) {
FrameTest ft = new FrameTest ( ) ;
ft.setBounds ( 20, 20 , 200, 200 );
ft.pack() ;
ft.show ( ) ;
}
}

////////////////////////////////////////////////////////////////////////
// Overriding processKeyEvent method of JTextArea
///////////////////////////////////////////////////////////////////////
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;

class CstTextArea extends JTextArea {
CstTextArea ( int r, int c ) {
super ( r, c ) ;
}

protected void processKeyEvent ( KeyEvent e ) {
if ( e.getKeyCode ( ) != KeyEvent.VK_TAB ) {
super.processKeyEvent ( e ) ;
}
}
}

public class FrameTest extends JFrame {
CstTextArea ta = new CstTextArea ( 5, 20 ) ;
JButton b = new JButton ( "Test" ) ;

public FrameTest ( ) {
Container cp = getContentPane ( ) ;
cp.add ( ta , "North" ) ;
cp.add ( b, "South" ) ;
}


public static void main ( String[] args ) {
FrameTest ft = new FrameTest ( ) ;
ft.setBounds ( 20, 20 , 200, 200 );
ft.pack() ;
ft.show ( ) ;
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us