Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / August 2005

Tip: Looking for answers? Try searching our database.

Updating PropertyEditor

Thread view: 
J Fuzzy - 07 Aug 2005 08:03 GMT
Hello all:

I wrote a simple bean that has one boolean property called checkOn.

The bean consists of a Checkbox.

The checkbox changes the property from true to false and back again.

Everythings works great, except the property editor doesn't get updated.

Any ideas on updating the editor?

Here the code:

-----------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.beans.*;

public class TempCheck extends Panel{

   private boolean checkOn;
   private Checkbox checkbox;

   public TempCheck(){
       setLayout(new BorderLayout());
       checkbox = new Checkbox("Hello", true);
       add(checkbox, BorderLayout.CENTER);
       checkOn = true;

       checkbox.addItemListener(
           new ItemListener(){
               public void itemStateChanged(ItemEvent e){
                   if(e.getStateChange() == ItemEvent.SELECTED){
                       setCheckOn(true);
                       support.firePropertyChange("", null, null);
                   }else{
                       setCheckOn(false);
                       support.firePropertyChange("", null, null);
                   }
               } // end itemStateChanged
           } // end ItemListener
       ); // end checkbox.addItemListener
   } // end constructor

   public boolean getCheckOn(){ return checkOn; }

   public void setCheckOn( boolean c){
       this.checkOn = c;
       System.out.println("checkOn = " + checkOn);
       checkbox.setState(checkOn);
   }

   public Dimension getMinimumSize(){ return new Dimension(50, 20);}
   public Dimension getPreferredSize(){ return getMinimumSize();}

   private PropertyChangeSupport support = new PropertyChangeSupport(this);

   public void addPropertyChangeListener(PropertyChangeListener pcl){
       support.addPropertyChangeListener(pcl);
   }

   public void removePropertyChangeListener(PropertyChangeListener pcl){
       support.removePropertyChangeListener(pcl);
   }
}
Thomas Hawtin - 07 Aug 2005 15:07 GMT
> I wrote a simple bean that has one boolean property called checkOn.
>
[quoted text clipped - 7 lines]
>
> public class TempCheck extends Panel{

What's wrong with Swing (in this context)? It's not 1997 any more.

>         setLayout(new BorderLayout());
>         checkbox = new Checkbox("Hello", true);
>         add(checkbox, BorderLayout.CENTER);
>         checkOn = true;

There is a problem here is that you have duplicated state, which more
often than not causes bugs. You seem to be okay here, but it is more by
luck than judgement that you have both values initialised to the same
value, and should keep updated together (unless there are any exceptions).

>         checkbox.addItemListener(
>             new ItemListener(){
[quoted text clipped - 6 lines]
>                         support.firePropertyChange("", null, null);
>                     }

You should fill in the correct values here...

>                 } // end itemStateChanged
>             } // end ItemListener
>         ); // end checkbox.addItemListener
>     } // end constructor

Do these comments help?

>     public boolean getCheckOn(){ return checkOn; }

Conventionally boolean properties getters start with is. Also On is kind
of redundant for boolean properties. Something like isChecked would be
better.

>     private PropertyChangeSupport support = new PropertyChangeSupport(this);
>
[quoted text clipped - 5 lines]
>         support.removePropertyChangeListener(pcl);
>     }

You are overriding Component methods here. No need to have support (but
do make sure you fire on the correct object). You will prevent all the
other properties of your component working, which may foul the internal
workings.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.