Basic Focus question

M

M K

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;
}
 
B

ByteCoder

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;
}

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

M K

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.
 
B

ByteCoder

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."
 
M

M K

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.
 
A

Andrew Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top