*
The moose likes Swing / AWT / SWT and the fly likes How disable FocusTraversalPolicy with JDK1.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How disable FocusTraversalPolicy with JDK1.3" Watch "How disable FocusTraversalPolicy with JDK1.3" New topic
Author

How disable FocusTraversalPolicy with JDK1.3

Kurt Boets
Greenhorn

Joined: Oct 19, 2004
Posts: 14
Hello,
I am using the FocusTraversalPolicy class. It works fine with JDK1.4.
But with JDK1.3, I receive a NoSuchClassException.
Is there a way to catch this error ?

I put my code here; the problem occur on this call
frame.setFocusTraversalPolicy(myFocus);

CODE:

import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.util.Arrays;
import java.util.List;

import be.smalsmvm.RneReat.commons.Version;

/**
*
*
* @author kbo
*/
public class LoginBoxTest {
private Button b1;
private Button b2;
private Button b3;
private Label l_name;
private Label l_firstname;
private Label l_adres;
private Label l_telefoonnummer;

private TextField name;
private TextField firstname;
private TextField adres;
private TextField telefoonnummer;
private Frame frame;
private Panel panel;
private boolean isJDK1_4 =false;
MyFocus myFocus;

public LoginBoxTest(){
frame = new Frame("login");
l_name = new Label("Name:");
l_firstname = new Label("Firstname:");
l_telefoonnummer = new Label("telefoonnummer:");
l_adres = new Label("adres:");
panel = new Panel();
b1 = new Button("button1");
b2 = new Button("button2");
b3 = new Button("button3");
name = new TextField();
firstname = new TextField();
adres = new TextField();
telefoonnummer = new TextField();

String javaVersion=System.getProperty("java.version");
//check if it is jdk1.4 or higher
isJDK1_4 = Version.isJava14() || Version.isJava15();

}

public void LaunchApplication(){
frame.setLayout(new GridLayout(4, 3));
name.setColumns(15);
firstname.setColumns(15);
adres.setColumns(15);
telefoonnummer.setColumns(15);

frame.add(l_name);
frame.add(name);
b1.setSize(15,10);
frame.add(b1);

frame.add(l_firstname);
frame.add(firstname);
frame.add(b2);

frame.add(l_adres);
frame.add(adres);
frame.add(b3);

frame.add(l_telefoonnummer);
frame.add(telefoonnummer);

frame.pack();

//Here the focus Traversel policy class is added to the frame
if ( isJDK1_4 )
this.initTraversal();


frame.setSize(400, 200);
frame.setVisible(true);

}


private void initTraversal(){
try{
myFocus = new MyFocus();
frame.setFocusTraversalPolicy(myFocus);

}catch(NoClassDefFoundError e){
e.printStackTrace();
}
}

public Component [] getComponentOrder(){
Component order [] = new Component[]{name, firstname, adres, telefoonnummer,
b1, b2, b3};

return order;
}



class MyFocus extends FocusTraversalPolicy {

private List list;

public MyFocus(){
list = Arrays.asList(getComponentOrder());
}


public Component getDefaultComponent(Container focusCycleRoot) {
return getComponentOrder()[0];

}

public Component getFirstComponent(Container focusCycleRoot) {
return getComponentOrder()[0];
}

public Component getLastComponent(Container focusCycleRoot) {
return getComponentOrder()[getComponentOrder().length-1];

}

public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
int index = list.indexOf(aComponent);
return getComponentOrder()[(index + 1) % getComponentOrder().length];


}

public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
int index = list.indexOf(aComponent);
return getComponentOrder()[(index - 1 + getComponentOrder().length) % getComponentOrder().length];

}


}


public static void main(String [] args){
try{
System.out.println("1");
LoginBoxTest box = new LoginBoxTest();
System.out.println("2");
box.LaunchApplication();
System.out.println("3");
}catch(RuntimeException thr){
System.out.println("in de runtm");
System.out.println(thr.getStackTrace());
}catch(NoClassDefFoundError thr){
System.out.println(thr.getCause());
}catch(Throwable thr){
thr.printStackTrace();

}

}



}
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
I think FocusTraversalPolicy was added in 1.4....
Kurt Boets
Greenhorn

Joined: Oct 19, 2004
Posts: 14
I know.
But at this moment, I don't know which JDK the client uses.
And if it is 1.3 of less, It won't compile. (throws a NoDefClassFound)
Is there a way to catch it, so that this code is always save to use ?

Thx,
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Can you even catch a compile time Exception?

You'll probably have to see what classes FocusTraversalPolicy deprecated and use them in place of it.


Grails Consultant
http://dangertree.net
Kurt Boets
Greenhorn

Joined: Oct 19, 2004
Posts: 14
I found the solution:
Check it with reflexion.

I just added this code :
Method methode = frame.getClass().getMethod("setFocusTraversalPolicy", new Class []{FocusTraversalPolicy.class} );
methode.invoke(frame, new Object [] {new MyFocus(getComponentOrder())});
And it works for JDK 1.3 + 1.4

greetings,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How disable FocusTraversalPolicy with JDK1.3
 
Similar Threads
setNextFocusableComponent()
Focus traversal issue
mouseEntered method
NextFocusableComponent -> FocusTraversalPolicy
Tab order on swing components