HI,
I'm very new to java and have been learning about bound properties but have got stuck trying out a test program which keeps throwing Null Pointer Exceptions.
I have mamnaged to bind the text property but have problems with the foreground and font properties, I think this may be something to do with overiding methods incorrectly and have tried alternatives but still no luck.
The bean code with the properties is:-
[tt]
import java.awt.*;
import javax.swing.JPanel;
import java.beans.*;
import java.awt.font.*;
import java.awt.color.*;
public class GreenBox extends JPanel {
public GreenBox() {
setBackground(Color.white);
setForeground(Color.black);
setText("Help meeeeeeee!"
;
}
//Instance variables
private String text;
private java.awt.Font font;
private java.awt.Color foreground;
/*Create a property-change listener to manage the list of listeners
private transient PropertyChangeSupport changes =
new PropertyChangeSupport(this);
/*Create add/remove methods for the property-change listener
public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
super.addPropertyChangeListener(l);
changes.addPropertyChangeListener(l);
}
public synchronized void removePropertyChangeListener(PropertyChangeListener
l) {
super.removePropertyChangeListener(l);
changes.removePropertyChangeListener(l);
}
/**
* setText that notifies the listeners of changes
* via firePropertyChange() method
*/
public void setText(String text) {
String oldText = this.text;
this.text = text;
changes.firePropertyChange("text", oldText, text);
repaint();
}
//getText method
public String getText() {
return text;
}
/* public void setFont(java.awt.Font font) {
java.awt.Font oldFont = this.font;
this.font = font;
changes.firePropertyChange("font", oldFont, font);
repaint();
}
public java.awt.Font getFont() {
return font;
}
// Alternative Overide set Font in super class
public void setFont(java.awt.Font font) {
java.awt.Font oldFont = getFont();
super.setFont(font);
changes.firePropertyChange("font", oldFont, font);
}
public java.awt.Font getFont() {
return super.getFont();
}
public void setForeground(java.awt.Color foreground) {
java.awt.Color oldForeground = this.foreground;
this.foreground = foreground;
changes.firePropertyChange("foreground", oldForeground, foreground);
repaint();
}
public java.awt.Color getForeground() {
return foreground;
}*/
}
[/tt]
I've commented out the bits that are causing problems.
Any help would be appreciated as I'm going slowly mad trying to figure this out.
If you would like the code for the test harness please let me know.
thanks
I'm very new to java and have been learning about bound properties but have got stuck trying out a test program which keeps throwing Null Pointer Exceptions.
I have mamnaged to bind the text property but have problems with the foreground and font properties, I think this may be something to do with overiding methods incorrectly and have tried alternatives but still no luck.
The bean code with the properties is:-
[tt]
import java.awt.*;
import javax.swing.JPanel;
import java.beans.*;
import java.awt.font.*;
import java.awt.color.*;
public class GreenBox extends JPanel {
public GreenBox() {
setBackground(Color.white);
setForeground(Color.black);
setText("Help meeeeeeee!"
}
//Instance variables
private String text;
private java.awt.Font font;
private java.awt.Color foreground;
/*Create a property-change listener to manage the list of listeners
private transient PropertyChangeSupport changes =
new PropertyChangeSupport(this);
/*Create add/remove methods for the property-change listener
public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
super.addPropertyChangeListener(l);
changes.addPropertyChangeListener(l);
}
public synchronized void removePropertyChangeListener(PropertyChangeListener
l) {
super.removePropertyChangeListener(l);
changes.removePropertyChangeListener(l);
}
/**
* setText that notifies the listeners of changes
* via firePropertyChange() method
*/
public void setText(String text) {
String oldText = this.text;
this.text = text;
changes.firePropertyChange("text", oldText, text);
repaint();
}
//getText method
public String getText() {
return text;
}
/* public void setFont(java.awt.Font font) {
java.awt.Font oldFont = this.font;
this.font = font;
changes.firePropertyChange("font", oldFont, font);
repaint();
}
public java.awt.Font getFont() {
return font;
}
// Alternative Overide set Font in super class
public void setFont(java.awt.Font font) {
java.awt.Font oldFont = getFont();
super.setFont(font);
changes.firePropertyChange("font", oldFont, font);
}
public java.awt.Font getFont() {
return super.getFont();
}
public void setForeground(java.awt.Color foreground) {
java.awt.Color oldForeground = this.foreground;
this.foreground = foreground;
changes.firePropertyChange("foreground", oldForeground, foreground);
repaint();
}
public java.awt.Color getForeground() {
return foreground;
}*/
}
[/tt]
I've commented out the bits that are causing problems.
Any help would be appreciated as I'm going slowly mad trying to figure this out.
If you would like the code for the test harness please let me know.
thanks