Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How disable FocusTraversalPolicy with JDK1.3

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}

}



}
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think FocusTraversalPolicy was added in 1.4....
 
Kurt Boets
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kurt Boets
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic