2

I have this code:

this.trigger = new Trigger();
this.presentationModel = new PresentationModel(this.personBean, this.trigger);
final ValueModel firstNameAdapter = presentationModel.getBufferedModel("firstName");
final JTextField firstNameTextField = BasicComponentFactory.createTextField(firstNameAdapter);

and

firstNameTextField.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            trigger.triggerCommit();
        }
    });

So when I push the enter button on the JTextField, I expect the value in my ValueModel class to be the same as the value in my JTextField. This doesn't happen unless I click outside the JTextField, then back inside the JTextField, and then push enter. If I just type in the text and hit enter, the ValueModel does not get the updated value. I am stuck on this problem, can anybody help?

BTW, I used this link for figuring out JGoodies in the first place: JGoodies Tutorial

user7291698
  • 1,972
  • 2
  • 15
  • 30
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113

3 Answers3

0

I hope I am understanding your question correctly.

You need to get the text in the text field and set it in the ValueModel.

firstNameTextField.addActionListener(new ActionListener()      
{         
    @Override         
    public void actionPerformed(ActionEvent e)          
    {
       //this get the text from the text field
       String firstName = firstNameTextField.getText();

       //now write your code to set the firstname into the ValueModel


       trigger.triggerCommit();
    }     
 }); 
Andy
  • 8,841
  • 8
  • 45
  • 68
  • I could manually call the getter on the JTextField and the setter on the ValueModel, but that doesn't work so well if I am using ints, longs, or formatted data in my JTextField. There has to be a way to programmatically force the data from the JTextField into the ValueModel. – smuggledPancakes Jan 18 '11 at 17:47
  • Well, if you were using ints, longs, or other formatted date you would just have to parse it. – Andy Jan 18 '11 at 17:53
  • True, I could make it work, but this can't be what the authors of JGoodies intended. I did send an email to them about this problem. – smuggledPancakes Jan 18 '11 at 18:23
  • Sorry, I'm not an expert on JGoodies, so after reading more about it I understand what your saying. – Andy Jan 18 '11 at 18:39
0

I looked through the JGoodies API (should have done this sooner) and found an unexpected static call, Bindings.commitImmediately()

If I call this method before my call to trigger.triggerCommit(), everything works as expected :)

smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
0

Create a text field that commits on each key typed instead of when focus is lost:

BasicComponentFactory.createTextField(firstNameAdapter, false);

Also, you should consider architecting your program to not use buffered models. I find that they make things more complicated and tricky, and think I saw Karsten Lentzsch recommending not to use them as well in a mailing list.

The most useful way for me to learn JGoodies was to look at the tutorial code for the JGoodies binding and validation libraries.

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57