0

I am trying to get the latest version (last typed words) in a string.

After I do that, I show an optionsPane and ask the user to confirm the string. If not the desired one, then I ask the user to reenter.

Why am I not getting anything (blank) if the user reenters in the field? Is the focusListener not updating it after the first time?

txtWhatIsYour = new JTextField();
txtWhatIsYour.setHorizontalAlignment(SwingConstants.CENTER);
txtWhatIsYour.setBackground(SystemColor.info);
txtWhatIsYour.setBounds(150, 75, 175, 25);
frame.getContentPane().add(txtWhatIsYour);
txtWhatIsYour.setColumns(10);               
txtWhatIsYour.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {}

public void focusLost(FocusEvent e) {
            firstName = txtWhatIsYour.getText();}});
il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Suggest a size for a `JTextField` by specifying columns in the constructor. – Andrew Thompson Dec 24 '17 at 11:08
  • BTW *"..if the user reenters in the field?"* the code shown, would do nothing. Why would you expect it to do anything **but** nothing given `public void focusGained(FocusEvent e) {}`? – Andrew Thompson Dec 24 '17 at 14:16
  • I want the user to finish up typing and then I want to collect those words in a string. Am I not supposed to use FocusLost for that? @AndrewThompson –  Dec 24 '17 at 20:04
  • I took 'reenters' to mean entering the field again. Where is the MCVE / SSCCE? – Andrew Thompson Dec 25 '17 at 03:04
  • 1
    You may also want a [`DocumentListener`](https://stackoverflow.com/search?tab=votes&q=DocumentListener). – trashgod Dec 25 '17 at 16:48

0 Answers0