Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Basic Focus question

Thread view: 
M K - 20 May 2005 18:50 GMT
I have a JFrame that has several text boxes.  When the user presses the save
button i check to see if the text boxes have data.
I want to be able to put the cursor on the text box thats blank.

Heres my code w/o the focus

String textD = txtDetails.getText().trim();
if (textD.equals(""))
{
       JOptionPane.showMessageDialog(null, "Please enter details", "Data
Error",JOptionPane.INFORMATION_MESSAGE);
       // need set focus here
       return;
}
ByteCoder - 20 May 2005 19:02 GMT
> I have a JFrame that has several text boxes.  When the user presses
> the save button i check to see if the text boxes have data.
[quoted text clipped - 11 lines]
>         return;
> }

The JTextField inherits the "requestFocus" method from the
javax.swing.JComponent Class.

Signature

-------------
- ByteCoder -           ...I see stupid people
-------------
                  Curiosity *Skilled* the cat

M K - 20 May 2005 19:20 GMT
I added the following:
txtDetails.requestFocus(true);

I had to press the TAB key 1x to get it to go to that field.  So it did not
work.  I have tried several other types of setFocusXXXXX and no change.

> > I have a JFrame that has several text boxes.  When the user presses
> > the save button i check to see if the text boxes have data.
[quoted text clipped - 14 lines]
> The JTextField inherits the "requestFocus" method from the
> javax.swing.JComponent Class.
ByteCoder - 20 May 2005 19:42 GMT
> I added the following:
> txtDetails.requestFocus(true);
>
> I had to press the TAB key 1x to get it to go to that field.  So it
> did not work.  I have tried several other types of setFocusXXXXX and
> no change.
[...]

The requestFocus(boolean temporary) method says:
"JComponent overrides requestFocus solely to make the method public so
that UI implementations can cause temporary focus changes. This method
is not meant for general use, instead developers are urged to call the
noarg requestFocus  or requestFocusInWindow methods. If the JComponent
has an InputVerifierassociated with it, the InputVerifier will be
messaged."

Signature

-------------
- ByteCoder -           ...I see stupid people
-------------
                  Curiosity *Skilled* the cat

Andrew Thompson - 20 May 2005 19:23 GMT
> String textD = txtDetails.getText().trim();
> if (textD.equals(""))
> {
>         JOptionPane.showMessageDialog(null, "Please enter details", "Data
> Error",JOptionPane.INFORMATION_MESSAGE);
>         // need set focus here

         txtDetails.requestFocus();

>         return;
> }

The JavaDocs are very useful for this sort of thing.
<http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html>

A better group for GUI questions is..
<http://www.physci.org/codes/javafaq.jsp#cljg>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

M K - 20 May 2005 19:56 GMT
Andrew,
   I always start with the docs.  I don't need pointer to docs I need help
please

I have tried requestFocus and requestFocusInWindow and no change.

> > String textD = txtDetails.getText().trim();
> > if (textD.equals(""))
[quoted text clipped - 13 lines]
> A better group for GUI questions is..
> <http://www.physci.org/codes/javafaq.jsp#cljg>
Andrew Thompson - 21 May 2005 02:37 GMT
> Andrew,
>     I always start with the docs.  I don't need pointer to docs..

I apparently require a crystal ball...  

> I have tried requestFocus and requestFocusInWindow and no change.

..to know that you tried calling both Component.requestFocus()
and Component.requestFocus(true).

> ..I need help please

OK..  here is the quick example I whipped up to check my facts.

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestFocus
 extends JPanel implements ActionListener {

 JTextField[] tf;

 public TestFocus() {
   tf = new JTextField[3];
   tf[0] = new JTextField("Blah!");
   tf[1] = new JTextField();
   tf[2]= new JTextField("Tah!");

   setLayout( new GridLayout() );
   add(tf[0]);
   add(tf[1]);
   add(tf[2]);

   JButton b = new JButton("Focus");
   b.addActionListener(this);
   add(b);
 }

 public void actionPerformed(ActionEvent ae) {
   for (int ii=0; ii<tf.length; ii++) {
     String text = tf[ii].getText().trim();
     if (text.equals("")) {
       JOptionPane.showMessageDialog(tf[ii],
       "Please enter details", "Data Error",
       JOptionPane.WARNING_MESSAGE );
       // need set focus here
       tf[ii].requestFocus();
       return;
     }
   }
 }

 public static void main(String[] args) {
   JFrame frame = new JFrame("Test Focus");
   frame.setDefaultCloseOperation(
     JFrame.DISPOSE_ON_CLOSE );

   frame.getContentPane().add( new TestFocus() );
   frame.pack();
   frame.setVisible(true);
 }
}
</sscce>

This code works to set the focus to the second text field for me.
Does it work for you?  Yes?

In that case, the answer is to change that code, one line at a time,
to your code.  Then, when it breaks, that is the bit you are doing wrong.

If that fails to produce your solution, try making an SSCCE*, as I did.

* <http://www.physci.org/codes/sscce.jsp>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.