How to create Java bean component : Java Beans « Language Basics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Language Basics » Java BeansScreenshots 
How to create Java bean component
How to create Java bean component

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import javax.swing.event.SwingPropertyChangeSupport;

public class BakedBean extends JComponent implements Externalizable {
  // Property names
  public static final String BEAN_VALUE = "Value";

  public static final String BEAN_COLOR = "Color";

  // Properties
  private Font beanFont; // simple

  private Dimension beanDimension; // simple

  private int beanValue; // bound

  private Color beanColor; // constrained

  private String text; // change

  // Manages all PropertyChangeListeners
  protected SwingPropertyChangeSupport propertySupporter = new SwingPropertyChangeSupport(
      this);

  // Manages all VetoableChangeListeners
  protected VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);

  protected transient ChangeEvent changeEvent = null;

  protected EventListenerList listenerList = new EventListenerList();

  public BakedBean() {
    beanFont = new Font("SanSerif", Font.BOLD | Font.ITALIC, 12);
    beanDimension = new Dimension(150100);
    beanValue = 0;
    beanColor = Color.black;
    text = "BakedBean #";
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(beanColor);
    g.setFont(beanFont);
    g.drawString(text + beanValue, 3030);
  }

  public void setBeanFont(Font font) {
    beanFont = font;
  }

  public Font getBeanFont() {
    return beanFont;
  }

  public void setBeanValue(int newValue) {
    int oldValue = beanValue;
    beanValue = newValue;

    // Notify all PropertyChangeListeners
    propertySupporter.firePropertyChange(BEAN_VALUE, new Integer(oldValue),
        new Integer(newValue));
  }

  public int getBeanValue() {
    return beanValue;
  }

  public void setBeanColor(Color newColorthrows PropertyVetoException {
    Color oldColor = beanColor;

    vetoableChangeSupport.fireVetoableChange(BEAN_COLOR, oldColor, newColor);

    beanColor = newColor;
    propertySupporter.firePropertyChange(BEAN_COLOR, oldColor, newColor);
  }

  public Color getBeanColor() {
    return beanColor;
  }

  public void setBeanString(String newString) {
    text = newString;

    // Notify all ChangeListeners
    fireStateChanged();
  }

  public String getBeanString() {
    return text;
  }

  public void setPreferredSize(Dimension dim) {
    beanDimension = dim;
  }

  public Dimension getPreferredSize() {
    return beanDimension;
  }

  public void setMinimumSize(Dimension dim) {
    beanDimension = dim;
  }

  public Dimension getMinimumSize() {
    return beanDimension;
  }

  public void addPropertyChangeListener(PropertyChangeListener l) {
    propertySupporter.addPropertyChangeListener(l);
  }

  public void removePropertyChangeListener(PropertyChangeListener l) {
    propertySupporter.removePropertyChangeListener(l);
  }

  public void addVetoableChangeListener(VetoableChangeListener l) {
    vetoableChangeSupport.addVetoableChangeListener(l);
  }

  public void removeVetoableChangeListener(VetoableChangeListener l) {
    vetoableChangeSupport.removeVetoableChangeListener(l);
  }

  // Remember that EventListenerList is an array of
  // key/value pairs.
  // key = XXListener class reference
  // value = XXListener instance
  public void addChangeListener(ChangeListener l) {
    listenerList.add(ChangeListener.class, l);
  }

  public void removeChangeListener(ChangeListener l) {
    listenerList.remove(ChangeListener.class, l);
  }

  // EventListenerList dispatching code.
  protected void fireStateChanged() {
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length - 2; i >= 0; i -= 2) {
      if (listeners[i== ChangeListener.class) {
        if (changeEvent == null)
          changeEvent = new ChangeEvent(this);
        ((ChangeListenerlisteners[i + 1]).stateChanged(changeEvent);
      }
    }
  }

  public void writeExternal(ObjectOutput outthrows IOException {
    out.writeObject(beanFont);
    out.writeObject(beanDimension);
    out.writeInt(beanValue);
    out.writeObject(beanColor);
    out.writeObject(text);
  }

  public void readExternal(ObjectInput inthrows IOException,
      ClassNotFoundException {
    setBeanFont((Fontin.readObject());
    setPreferredSize((Dimensionin.readObject());
    // Use preferred size for minimum size..
    setMinimumSize(getPreferredSize());
    setBeanValue(in.readInt());
    try {
      setBeanColor((Colorin.readObject());
    catch (PropertyVetoException pve) {
      System.out.println("Color change vetoed..");
    }
    setBeanString((Stringin.readObject());
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("BakedBean");
    frame.getContentPane().add(new BakedBean());
    frame.setVisible(true);
    frame.pack();
  }
}


           
       
Related examples in the same category
1. Simple Java bean containerSimple Java bean container
ww_w__.__ja_v__a___2_s.___c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.