constrained values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    constrained values

    Code:
    public void setValue(T newValue) throws PropertyVetoException {
    
          T old = value;
          //veto.fireVetoableChange(value,old,newValue);
          veto.fireVetoableChange("value",
                  new  ? (old),
                  new  ? (newValue));
    
          value = newValue;
          changes.firePropertyChange(name, old, value);
        }
    what shd be there where ? are
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    What is the method siganture of fireVetoableCha nge?

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      oll3i-

      Please do not double-post - you have already started a thread on this subject.

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        i dont know what u mean but here is teh code


        Code:
        package zad12;
        
        
        
        
        import java.beans.*;
        import java.io.*;
        import java.util.*;
        import java.lang.reflect.*;
        
        abstract class AbstractBean implements Serializable {
        
          protected PropertyChangeSupport changes = new PropertyChangeSupport(this);
          protected VetoableChangeSupport veto = new VetoableChangeSupport(this);
        
          public void addPropertyChangeListener(PropertyChangeListener pcl) {
        	  changes.addPropertyChangeListener(pcl);
          }
        
          public void removePropertyChangeListener(PropertyChangeListener pcl) {
        	  changes.addPropertyChangeListener(pcl);
          }
        
          class BoundedProperty<T>  implements PropertyChangeListener {
            private String name;
            private T value;
          
            private Object chgHandlerObject;
            private Method changeHandler;
        
            public BoundedProperty(String name) {
              this.name = name;
            }
        
            public T getValue() { return value; }
        
            public void setValue(T newValue) throws PropertyVetoException {
        
              T old = value;
              //veto.fireVetoableChange(value,old,newValue);
              veto.fireVetoableChange("value",
                      new ?(old),
                      new ?(newValue));
        
              value = newValue;
              changes.firePropertyChange(name, old, value);
            }
            
            
           
            
            
            
            public void propertyChange(PropertyChangeEvent e) {
              if (!e.getPropertyName().equals(name)) return;
              if (changeHandler == null) return;
              try {
                changeHandler.invoke(chgHandlerObject);
              } catch(Exception exc) {
                  exc.printStackTrace();
              }
            }
        
            public void setChangeHandler(Object handl, String mname) {
              try {
                Method m = handl.getClass().getDeclaredMethod(mname);
                chgHandlerObject = handl;
                changeHandler = m;
               changes.addPropertyChangeListener(this);
              } catch(Exception exc) {
                  exc.printStackTrace();
                  return;
              }
            }
        
            public void setChangeHandler(Object ohandler) {
              try {
                Method m = ohandler.getClass().getDeclaredMethod(name+"Change");
                chgHandlerObject = ohandler;
                changeHandler = m;
              } catch(Exception exc) {
                  exc.printStackTrace();
                  return;
              }
              changes.addPropertyChangeListener(this);
            }
        
            public void removeChangeHandler() {
              changeHandler = null;
              chgHandlerObject = null;
              changes.removePropertyChangeListener(this);
            }
          }
        
        
        }

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          i just started learning java sorry for so many questions i ask and thank u for ur patience

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            I don't need to look at your code, I am interested in what the API is for fireVetoableCha nge().

            There should be either a java document or a java source file that says something like
            Code:
            fireVetoableChange(String aString, Something aSomething, OtherSomething aOtherSomething)
            What does the API say that is? Or what does the java source say that it is?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by oll3i
              Code:
              public void setValue(T newValue) throws PropertyVetoException {
               
              T old = value;
              //veto.fireVetoableChange(value,old,newValue);
              veto.fireVetoableChange("value",
              new ? (old),
              new ? (newValue));
               
              value = newValue;
              changes.firePropertyChange(name, old, value);
              }
              what shd be there where ? are
              Any class whose constructor takes a T object as argument.

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                thank u that worked :]

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  i get an error in bean.text.setVa lue(napis.getOb ject());
                  it says
                  Exception in thread "main" java.lang.Error : Unresolved compilation problems:
                  The method setValue(Box) in the type AbstractBean.Bo undedProperty<B ox> is not applicable for the arguments (String)
                  The method setValue(Box) in the type AbstractBean.Bo undedProperty<B ox> is not applicable for the arguments (String)

                  at zad12.Extprops. <init>(Extprops .java)
                  at zad12.Extprops. main(Extprops.j ava)

                  but setValue takes T as argument and napis.getObject () returns T

                  Code:
                  package zad12;
                  
                  import static java.awt.Color.BLACK;
                  
                  import java.awt.Color;
                  import java.awt.event.ActionEvent;
                  import java.awt.event.ActionListener;
                  import java.beans.PropertyVetoException;
                  
                  import javax.swing.JButton;
                  import javax.swing.JFrame;
                  
                  public class Extprops extends JFrame {
                  
                  	  JButton b = new JButton();
                  	  TestBean bean = new TestBean();
                  	 
                  	  Box <String> napis = new Box <String> ("JOE BLACK");
                  	  Color c = new Color(0, 0, 0);
                  	  public Extprops() throws PropertyVetoException {
                     >>> here is the error >>> bean.text.setValue(napis.getObject());
                  	    bean.color.setValue(c);
                  
                  	    b.setText(napis.getObject());
                  	    b.setForeground(bean.color.getValue());
                  
                  	    b.addActionListener( new ActionListener() {
                  	         public void actionPerformed(ActionEvent e) {
                  	           try {
                   >>> here is the error >>> bean.text.setValue(napis.getObject());
                  			} catch (PropertyVetoException e1) {
                  				// TODO Auto-generated catch block
                  				e1.printStackTrace();
                  			}
                  	           try {
                  				bean.color.setValue(bean.color.getValue().brighter());
                  			} catch (PropertyVetoException e1) {
                  				// TODO Auto-generated catch block
                  				e1.printStackTrace();
                  			}
                  	         }
                  	      });
                  	    bean.color.setChangeHandler(this);
                  	    bean.text.setChangeHandler(this);
                  
                  	    add(b);
                  	    setSize(200,200);
                  	    setVisible(true);
                  	  }
                  
                  	  public void colorChange() {
                  	    b.setForeground(bean.color.getValue().brighter().brighter());
                  	  }
                  
                  	  public void textChange() {
                  	    b.setText(napis.getObject());
                  	  }
                  
                  
                  
                  	  public static void main(String[] args)  throws PropertyVetoException{
                  	     new Extprops();
                  	  }
                  
                  
                  	}

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #10
                    i made it all work ;]

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by oll3i
                      i made it all work ;]

                      Good for you .

                      Comment

                      Working...