Update JTextField on property change?

Swing Designer allows you to quickly create the frames, panels, dialogs, applets and other UI elements that comprise Java Swing applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Update JTextField on property change?

Postby edbeaty » Mon Oct 04, 2010 12:50 pm

Hello all. I am new to using the Instantiations bindings framework, and have a question. I have bound a JTextField to a bean's property, so changes to the JTextField's text are causing the bean's set() method to be called, no problem. My difficulty is in trying to get the JTextField to update its text when the bound bean's property is changed elsewhere.

In the example code, pressing the button calls the bean's setName() method, firing a propertyChangeEvent from the bean, but the JTextField doesn't respond. My guess is that nobody's listening to the fired event, and I'm not sure who should (the JTextField? Or some component of the Binding apparatus?)

Any help you can provide would be greatly appreciated.
Thanks,
Ed

Code: Select all
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import javax.swing.JButton;

public class BindingTest extends JPanel {

    protected JTextField textField;   
    protected Model model = new Model();
   
    public BindingTest() {
       
        JButton button = new JButton("New button");
        add(button);
       
        textField = new JTextField();
        add(textField);
        textField.setColumns(10);
        initDataBindings();
       
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
               
                model.setName("foo");
               
            }           
        });
    }

   
    protected void initDataBindings() {
        BeanProperty<Model, String> modelBeanProperty = BeanProperty.create("name");
        BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
        AutoBinding<Model, String, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, model, modelBeanProperty, textField, jTextFieldBeanProperty);
        autoBinding.bind();
    }
   
    public static class Model {
       
        protected PropertyChangeSupport support = new PropertyChangeSupport(this);
       
        protected String name;

        public String getName() {
            return name;
        }

        public void setName(String newName) {
            String oldName = this.name;
            this.name = newName;
            System.out.println("setName called: " + newName);
            support.firePropertyChange(new PropertyChangeEvent(this, "name", oldName, newName));
        }
       
        public void addPropertyChangeListener(PropertyChangeListener l){
            support.addPropertyChangeListener(l);
        }
    }
   
   
    public static void main(String[] args){
       
        BindingTest view = new BindingTest();       
        JFrame frame = new JFrame();
        frame.getContentPane().add(view, BorderLayout.CENTER);
        frame.setMinimumSize(new Dimension(500,500));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    }
}
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby edbeaty » Mon Oct 04, 2010 9:39 pm

Never mind, I figured it out. Changing the bean to extend PropertyChangeSupport allows the JTextField to respond to the bean's PropertyChangeEvents.
-Ed
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby asablin » Tue Oct 05, 2010 12:29 am

asablin
Moderator
 
Posts: 19
Joined: Thu Sep 23, 2010 3:07 am

Re: Update JTextField on property change?

Postby edbeaty » Tue Oct 05, 2010 6:32 am

Thanks asablin. Here's the project link from the article: http://code.google.com/p/javabuilders/
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby Eric Clayberg » Tue Oct 05, 2010 7:15 am

edbeaty wrote:I am new to using the Instantiations bindings framework

BTW, we should point out that it isn't the "Instantiations bindings framework". We are simply offering tooling support for the existing Swing data binding framework (JSR 295)...warts and all.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Update JTextField on property change?

Postby edbeaty » Tue Oct 05, 2010 8:01 am

Thanks, Eric. One more question; I'd like to use the @Bindable annotation that asablin pointed out, but I can't seem to find the correct library in the javabuilders code. Is there a different package I should be looking in?
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby Eric Clayberg » Tue Oct 05, 2010 8:24 am

Sorry. No idea.

In general, we can help with Swing Designer specific questions in this forum. If you need help with general Swing issues (such as how to best use the JSR 295 framework), you would be much better off asking those questions in one of the general Swing newsgroups/forums available.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Update JTextField on property change?

Postby edbeaty » Tue Oct 05, 2010 8:45 am

No problem; I'll try those forums.
I just want to add that I'm really pleased with Instantiations! It's making our GUI creation much simpler than before.
Thanks,
Ed
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby Eric Clayberg » Tue Oct 05, 2010 8:53 am

edbeaty wrote:I just want to add that I'm really pleased with Instantiations!

...and now Google ;-)
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Update JTextField on property change?

Postby kseader » Thu Oct 21, 2010 6:26 am

Ed,
I am experiencing the same issues as you were earlier. When you say that you changed the bean to extend PropertyChangeSupport are you talking about the model class? If so, what did you pass the constructor as the source object? Any help would be greatly appreciated.

Thanks,
Kirk
kseader
 
Posts: 2
Joined: Wed Oct 20, 2010 12:13 pm

Re: Update JTextField on property change?

Postby edbeaty » Fri Oct 22, 2010 8:24 am

Hi, Kirk. Yes, I changed the model class to extend PropertyChangeSupport, and it seemed to work. I defined my model as an inner class to the view, something like:

Code: Select all
public class View extends JPanel implements PropertyChangeListener{
   
   protected Model model = new Model();
   
   public View(){      
   }
      
   public class Model extends PropertyChangeSupport{
      public Model() {
         super(View.this);
      }
      
      //model getters and setters
   }
}



I'm sure this is five ways to non-standard, but it worked as a first pass at getting the bindings to work.
Hope this helps,
Ed
edbeaty
 
Posts: 6
Joined: Mon Oct 04, 2010 11:00 am

Re: Update JTextField on property change?

Postby kseader » Fri Oct 22, 2010 11:25 am

Great, that helps.
Thanks Ed. Much appreciated.

Kirk
kseader
 
Posts: 2
Joined: Wed Oct 20, 2010 12:13 pm

Re: Update JTextField on property change?

Postby Eric Clayberg » Sat Oct 23, 2010 4:20 am

Thanks for the suggestions, Ed. Those should be helpful to lots of folks.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to Swing Designer

Who is online

Users browsing this forum: No registered users and 1 guest

cron