Results 1 to 6 of 6

Thread: how to bind a property to a JLabel?

  1. #1

    Default how to bind a property to a JLabel?

    Hi,

    maybe someone can point me to some doc/ or example code...?

    I want to do the following:
    My databean has a property (which is not readonly, i.e. it has a
    getter and a setter). Now, when building a form, I want to bind this
    property to a JLabel fileld, so it is visible just as information, but can't
    be edited and doesn't look like a disabled input field. Is there any
    "standard" way of achieving this?

    thanks,
    Hermann Vosseler

  2. #2
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Hi and welcome.

    I don't think there's a binder in RCP that handles JLabels, but you could easily make one yourself.

    I've made a binding that binds String properties to a label:

    Code:
    public class StringLabelBinding extends CustomBinding
    {
    
        private JTextField label;
    
        public StringLabelBinding(FormModel formModel, String formPropertyPath)
        {
            this(new JTextField(), formModel, formPropertyPath);
        }
    
        public StringLabelBinding(JTextField label, FormModel formModel, String formPropertyPath)
        {
        	super(formModel, formPropertyPath, String.class);
            this.label = label;
        }
    
        @Override
        protected void valueModelChanged(Object newValue)
        {
            label.setText((String) newValue);
    
        }
    
        @Override
        protected JComponent doBindControl()
        {
            return label;
        }
    
        @Override
        protected void enabledChanged()
        {
            label.setEnabled(isEnabled());
    
        }
    
        @Override
        protected void readOnlyChanged()
        {
            label.setEditable(!isReadOnly());
        }
        
        @Override
        protected boolean isReadOnly()
        {
            return true;
        }
    
    }
    The binder is equally simple:

    Code:
    public class StringLabelBinder implements Binder
    {
        @Override
        public Binding bind(FormModel formModel, String formPropertyPath, Map context)
        {
            return new StringLabelBinding(formModel, formPropertyPath);
        }
    
        @Override
        public Binding bind(JComponent control, FormModel formModel, String formPropertyPath, Map context)
        {
            if (control instanceof JTextField)
            {
                return new StringLabelBinding((JTextField) control, formModel, formPropertyPath);
            }
            else
            {
                throw new IllegalArgumentException("This binding's control must be a JTextField");
            }
        }
    
    }
    Have fun!

  3. #3
    Join Date
    Jun 2005
    Posts
    28

    Default how to bind a property to a JLabel?

    You can use:
    Code:
    getBindingFactory().createBoundLabel(formProperty);
    This is already available in SwingBindingFactory.

    Regards,
    Prashant

  4. #4
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    Ok, sorry, you're correct. However, this'll only work for String. If you want some more flexibility, you'll need to create a binder yourself. Say you want a label that shows the i18n value of an enum, you can easily adapt my binder to show enums.

    With even more customisation, you could do something like:

    A converter that you need to implement:

    Code:
    public interface StringConverter
    {
        public String convertToString(Object toConvert);
    }


    And hand it to the slightly altered binder and binding:

    Code:
    public class StringLabelBinder implements Binder
    {
        private StringConverter converter;
    
        public void setConverter(StringConverter converter) { this.converter = converter; }
    
        public StringConverter getConverter() 
        {
            if(converter == null)
            {
                converter = new Converter() { public String convertToString(Object object) { return object.toString(); } }
            }
            return converter;
        }
      
        @Override
        public Binding bind(FormModel formModel, String formPropertyPath, Map context)
        {
            return new StringLabelBinding(formModel, formPropertyPath, converter);
        }
    
        @Override
        public Binding bind(JComponent control, FormModel formModel, String formPropertyPath, Map context)
        {
            if (control instanceof JTextField)
            {
                return new StringLabelBinding((JTextField) control, formModel, formPropertyPath, converter);
            }
            else
            {
                throw new IllegalArgumentException("This binding's control must be a JTextField");
            }
        }
    }
    Code:
    public class StringLabelBinding extends CustomBinding
    {
    
        private JTextField label;
        private StringConverter converter;
    
        public StringLabelBinding(FormModel formModel, String formPropertyPath, StringConverter converter)
        {
            this(new JTextField(), formModel, formPropertyPath, converter);
        }
    
        public StringLabelBinding(JTextField label, FormModel formModel, String formPropertyPath, StringConverter converter)
        {
        	super(formModel, formPropertyPath, String.class);
            this.label = label;
            this.converter = converter;
        }
    
        @Override
        protected void valueModelChanged(Object newValue)
        {
            label.setText(converter.convertToString(newValue));
    
        }
    
        @Override
        protected JComponent doBindControl()
        {
            return label;
        }
    
        @Override
        protected void enabledChanged()
        {
            label.setEnabled(isEnabled());
    
        }
    
        @Override
        protected void readOnlyChanged()
        {
            label.setEditable(!isReadOnly());
        }
        
        @Override
        protected boolean isReadOnly()
        {
            return true;
        }
    
    }
    So now you have a flexible solution to show any object type in a label. You only need to implement the String converter and use that in the Binder.

    But again, for Strings it's overkill, prashantbhat answer will do nicely.

  5. #5

    Default

    oh thanks... this did the trick

    also thanks to LievenDoclo for posting the flexible sollution; it may come in handy for another problem I'll probably have to solve next...

    and btw, Spring RichClient rocks!
    Hermann

  6. #6
    Join Date
    Mar 2007
    Location
    Oudenaarde
    Posts
    294

    Default

    and btw, Spring RichClient rocks!
    Hermann
    Tell me something I don't already know

    By the way, a small correction, the above binder will show a non-editable jtextfield, not a label. With a label, you're not able to select to content, with the above binder, it's possible to do so.
    Last edited by LievenDoclo; May 6th, 2008 at 08:09 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •