Hello All,
I've been encountering this focus problem in
Applets, I have a JPanel and have a few JTextfields embedded in it .
I have a few validations to be executed for a JTextfield and if encountering any error in it I show a JOptionPane with the error
or warning message in it.
After display and I click the OK, button I have to set the focus back to the same Jtextfield, I have used the functions
setRequestFocusEnabled() and requestFocus() but the focus goes to the browser ( though the cursor is available in the JTextfield)
and when i press the back space key takes me to the previous screen. I tried selecting the text in the JTextField by using selectAll(),
but of no use, the text gets selected but the focus is still in the browser, and the focusGained() display also works.
I have pasted the code below. Can anyone suggest me where I have gone wrong or what needs to be done in addition.
public class TextHandler implements FocusListener
{
boolean bfocusflag = true;
public void focusGained(FocusEvent fe)
{
Object source = fe.getSource();
if ((source == txtEthernetIPAdress))
{
System.out.println(" Focus in ip address");
/*
((JTextField)source).requestFocus();
((JTextField)source).grabFocus();*/
}
}
public void focusLost(FocusEvent fe)
{
boolean bsuccess=true;
Object source = fe.getSource();
int icountToken = 0;
if ((!fe.isTemporary()) && (source == txtEthernetIPAdress) && (bfocusflag))
{
String s= txtEthernetIPAdress.getText();
String sPart="";
try
{
StringTokenizer sIPAddrToken = new StringTokenizer(s, ".");
if (sIPAddrToken.countTokens() == 4)
{
//Get the address after removing the dot
while (sIPAddrToken.hasMoreTokens())
{
sPart = sIPAddrToken.nextToken();
try
{
int i = Integer.parseInt(sPart);
if ((i > 0) && (i < 256))
{
icountToken++;
bsuccess=true;
bfocusflag = true;
}
else
{
bsuccess=false;
break;
}
}
catch(Exception e)
{
bsuccess=false;
bfocusflag = false;
}
} //end while
}
else
{
bfocusflag = false;
SmErr.dspErrorMsg(sFrom,ComParms.FORMATERROR);
System.out.println(((JTextField)source).isVisible());
((JTextField)source).setRequestFocusEnabled(true);
((JTextField)source).setVisible(true);
System.out.println(((JTextField)source).isVisible());
((JTextField)source).requestFocus();
((JTextField)source).selectAll();
((JTextField)source).setEnabled(true);
bfocusflag = true;
} //end if
}
catch(Exception e)
{
bfocusflag = false;
}
}
else
{
bfocusflag = false;
}//end of if
if ((icountToken == 4) && (bsuccess))
{
Text_response(((JTextField)source).getName(),((JTextField)source).getText());
}
if (!bsuccess)
{
bfocusflag = false;
// A function throwing a Joptionpane for the corresponding error type
SmErr.dspErrorMsg(sFrom,ComParms.FORMATERROR);
System.out.println(((JTextField)source).isVisible());
((JTextField)source).setVisible(true);
System.out.println(((JTextField)source).isVisible());
((JTextField)source).setRequestFocusEnabled(true);
((JTextField)source).requestFocus();
((JTextField)source).selectAll();
((JTextField)source).setEnabled(true);
bfocusflag = true;
}
}// end of method
}//end of innner class
Thanks in Advance
Usha Ranganathan