• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextField focus problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think i too encountered a similar problem sometime ago.
i Think that this problem is specific to the JRE u r using.
Better use JRE1.3 or higher.
 
reply
    Bookmark Topic Watch Topic
  • New Topic