FocusTraversalPolicy

Hello,

I made a little program that use the FocusTraversalPolicy but i can't run it because i get an error.

can someone help me and give an answer why i get this error.

My program:

import java.awt.*;

public class Movebetween extends Frame {

TextField [] textin=new TextField[4];

TextField t1,t2,t3,t4;

static ControlFoc newPolicy;

public Movebetween (String caption)

{

newPolicy=new ControlFoc();

Panel p=new Panel();

for (int i=0;i < textin.length;i++)

{

textin=new TextField(30);

}

p.setLayout(new FlowLayout());

for (int i=0;i < textin.length;i++)

{

p.add(textin);

}

add(p);

pack();

p.setFocusTraversalPolicy(newPolicy);

setVisible(true);

}

public static void main (String[] args)

{

Movebetween b=new Movebetween("Checking Focus");

}

}

class ControlFoc extends FocusTraversalPolicy

{

public Component getComponentAfter (Container c,Component aComponent)

{

if (aComponent.equals(textin[0]))

return textin[2];

}

public Component getComponentBefore (Container c,Component t2)

{

return t2;

}

public Component getDefaultComponent (Container focusCycleRoot)

{

return textin[0];

}

public Component getFirstComponent(Container focusCycleRoot) {

return focusCycleRoot;

}

/*public Component getInitialComponent (Container focusCycleRoot)

{

return t2;

}*/

public Component getLastComponent (Container focusCycleRoot)

{

return textin[3];

}

}

the problem in the rountine:getComponentAfter,getDefaultComponent,getLastComponent

The error seems to be realted to that that i didn't define the array textin of the textfield ,and that not clear to me ,because i define it.

thanks in advance.

[1972 byte] By [ofr_QAJAVAa] at [2007-10-2 10:50:05]
# 1

1) Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code so the code is readable.

2) You don't tell us what the error is and your code isn't executable so we can't guess what the problem is.

3) You are posting this question in the Swing forum, but you are using a AWT components (TextField, Frame).

4) For a working example in a Swing application read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html]How to Use the Focus Subsystem[/url].

camickra at 2007-7-13 3:07:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hey,

Yes,you are right i wrong in the topic that i put my question.

Anyway if you can answer me for the question i will be very grateful about that.

On paragraph 2 you are right my program is not executable, i get an error that textin cannot be resolved.

it's seems to be that some variable that i declared as i mentioned in the previous mail is not defined so good.(it's the array of TextField).

thanks

ofr_QAJAVAa at 2007-7-13 3:07:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

Panel p=new Panel();

for (int i=0;i < textin.length;i++) {

////////////////////////////////////////////////////////////

//You have forgotten to reference

//which part of the array you want

// textin[i] = new TextField(30);

////////////////////////////////////////////////////////////

textin=new TextField(30);

}

This is what I see wrong. Hopefully it helps.

NewOldGuya at 2007-7-13 3:07:47 > top of Java-index,Desktop,Core GUI APIs...
# 4

OK.

that's an error number two but after a couple of hours i found out that my main problem is related to that the class that extends from FocusTraversalPolicy

need to be declared in the main class of the program and then it identify the array textin.

Maybe it's will help to someone that will encounter in this problem.

I know that's a tiny problem of attention but maybe someone will need it.

Anyway thanks for yours help.

ofr_QAJAVAa at 2007-7-13 3:07:47 > top of Java-index,Desktop,Core GUI APIs...