How to switch focus using from one JTextField to another ??
Hi there,
I was wondering if anyone can help me. I have two JTextField and I
want to switch focus between one and the other so I can write different
words inside.
for instance here I wrote code for a button that when pressed writes
zero inside a JTextField:
void zeroButton_mouseClicked(MouseEvent e) {
boolean b,c = false;
if(tableNoTextField.hasFocus())
{
b = true;
System.out.println(b);
inputTableNo = inputTableNo + "0";
System.out.println(inputTableNo);
tableNoTextField.setText(inputTableNo);
}
if(noOfGuestField.hasFocus())
{
c = true;
System.out.println(c);
guestNumber = guestNumber + "0";
System.out.println(guestNumber);
noOfGuestField.setText(guestNumber);
}
}
Here I tried to switch the focus between one JTextField to another but
without success:
boolean x = false;
boolean y = false;
void tableNoTextField_mouseClicked(MouseEvent e) {
if(tableNoTextField.hasFocus())
{
x = true;
System.out.println(x);
tableNoTextField.requestFocus();
System.out.println("tableNoTextField has focus");
System.out.println(y);
}
}
void noOfGuestField_mouseClicked(MouseEvent e) {
if(noOfGuestField.hasFocus())
{
y = true;
System.out.println(y);
noOfGuestField.requestFocus();
System.out.println("noOfGuestField has focus");
System.out.println(x);
}
}
Can anyone tell me how to switch the focus from one component to
another please?