VasilM

nothingfancy

Jan 14th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  
  6.     String colorNames[] = {"Red", "Green", "Blue"};
  7.     Color colors[] = {Color.RED, Color.GREEN, Color.BLUE};
  8.  
  9.     private void cbxColorActionPerformed(java.awt.event.ActionEvent evt) {                                        
  10.     // TODO add your handling code here:
  11.           circle1.setColor(colors[cbxColor.getSelectedIndex()]);
  12.     }
  13.  
  14.     public BoundPropertyDemoForm() {
  15.         initComponents();
  16.         // Регистрация на слушатели на събитията за компонентите
  17.         circle1.addPropertyChangeListener(square1);
  18.         square1.addPropertyChangeListener(triangle1);
  19.         square1.addPropertyChangeListener(newBean1);
  20.         newBean1.addPropertyChangeListener(newBean2);
  21.     }  
  22.  
  23.  */
  24. package my.boundproperty;
  25.  
  26. import java.awt.Canvas;
  27. import java.awt.Color;
  28. import java.awt.Dimension;
  29. import java.awt.Graphics;
  30. import java.beans.*;
  31.  
  32. /**
  33.  *
  34.  * @author lmn7
  35.  */
  36. public class NewBean extends Canvas implements PropertyChangeListener {
  37.  
  38.     private Color color;
  39.  
  40.     private final PropertyChangeSupport propertySupport;
  41.  
  42.     public NewBean() {
  43.         setSize(50, 50);
  44.         color = Color.CYAN;
  45.         propertySupport = new PropertyChangeSupport(this);
  46.     }
  47.  
  48.     public Color getColor() {
  49.         return this.color;
  50.     }
  51.  
  52.     @Override
  53.     public void addPropertyChangeListener(PropertyChangeListener listener) {
  54.         propertySupport.addPropertyChangeListener(listener);
  55.     }
  56.  
  57.     @Override
  58.     public void removePropertyChangeListener(PropertyChangeListener listener) {
  59.         propertySupport.removePropertyChangeListener(listener);
  60.     }
  61.  
  62.     @Override
  63.     public void propertyChange(PropertyChangeEvent evt) {
  64.         setColor((Color) evt.getNewValue());
  65.     }
  66.  
  67.     public void setColor(Color color) {
  68.         Color oldValue = this.color;
  69.         this.color = color;
  70.         propertySupport.firePropertyChange("color", oldValue, color);
  71.         repaint();
  72.     }
  73.  
  74.     public void paint(Graphics g) {
  75.         Dimension d = getSize();
  76.         g.setColor(color);
  77.         g.fillOval(0, 0, d.width, d.height - 1);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment