For Programmers: Free Programming Magazines  


Home > Archive > Java Beans > August 2005 > Updating PropertyEditor









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Updating PropertyEditor
J Fuzzy

2005-08-07, 9:01 am

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(PropertyChange
Listener pcl){
support.addPropertyChangeListener(pcl);
}

public void removePropertyChangeListener(PropertyCha
ngeListener pcl){
support.removePropertyChangeListener(pcl);
}
}


Thomas Hawtin

2005-08-07, 5:01 pm

J Fuzzy wrote:
>
> 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.
>
> [...]
>
> 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(){
> public void itemStateChanged(ItemEvent e){
> if(e.getStateChange() == ItemEvent.SELECTED){
> setCheckOn(true);
> support.firePropertyChange("", null, null);
> }else{
> setCheckOn(false);
> 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);
>
> public void addPropertyChangeListener(PropertyChange
Listener pcl){
> support.addPropertyChangeListener(pcl);
> }
>
> public void removePropertyChangeListener(PropertyCha
ngeListener pcl){
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com