Java Reference
In-Depth Information
• HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY
• HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY
• ICON_CHANGED_PROPERTY
• MARGIN_CHANGED_PROPERTY
• MNEMONIC_CHANGED_PROPERTY
• MODEL_CHANGED_PROPERTY
• PRESSED_ICON_CHANGED_PROPERTY
• ROLLOVER_ENABLED_CHANGED_PROPERTY
• ROLLOVER_ICON_CHANGED_PROPERTY
• ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY
• SELECTED_ICON_CHANGED_PROPERTY
• TEXT_CHANGED_PROPERTY
• VERTICAL_ALIGNMENT_CHANGED_PROPERTY
• VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
Therefore, instead of hard-coding specific text strings, you can create a
PropertyChangeListener that uses these constants, as shown in Listing 4-5.
Listing 4-5. Base PropertyChangeListener for AbstractButton
import javax.swing.*;
import java.beans.*;
public class AbstractButtonPropertyChangeListener
implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (e.getPropertyName().equals(AbstractButton.TEXT_CHANGED_PROPERTY)) {
String newText = (String) e.getNewValue();
String oldText = (String) e.getOldValue();
System.out.println(oldText + " changed to " + newText);
} else if (e.getPropertyName().equals(AbstractButton.ICON_CHANGED_PROPERTY)) {
Icon icon = (Icon) e.getNewValue();
if (icon instanceof ImageIcon) {
System.out.println("New icon is an image");
}
}
}
}
Search WWH ::




Custom Search