Results 1 to 5 of 5
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default struggling with properties

    When c is a class that extends a JPanel that functions as a drawing
    surface which holds (Chinese) shapes and bc is a class which contains
    a couple of shape buttons I could do the following:
    c.addPropertyChangeListener(bc);


    Next I could do as well (to action process the buttons):
    bc.setDrawingArea(c);
    And here is the method at the bc button panel class:
    Java Code:
    public void setDrawingArea(GraphicOps graphicOps) {
            this.graphicOps = graphicOps;
    }
    Now it gets a little more complicated whereas graphicOps is an interface:

    Java Code:
    public interface GraphicOps {
        public void setColor(Color color);  // Set drawing color
        public void setShape(int shape);    // Set drawing shape
        public void clearCanvas();          // Clear drawing area
        public void removeLast();           // Remove last item
        public void printCanvas();          // Print everything
        public int getShapeCount();         // Get number of shapes
    
        // Constants
        public static final int DRAWN_LINE          = 0;
        public static final int DRAWN_RECT          = 1;
        public static final int DRAWN_ROUND_RECT    = 2;
        public static final int FILLED_RECT         = 3;
        public static final int FILLED_ROUND_RECT   = 4;
        public static final int DRAWN_OVAL          = 5;
        public static final int FILLED_OVAL         = 6;
    
        // Property for count of graphic shapes
        public static final String SHAPE_PROPERTY = "shape_property";
    }

    My question is: how come that getPropertyName() ="shape_property"
    (See also string declaration of the interface) I never used the set property
    name method (setPropertyName()) and the method (see first text block, above)
    addPropertyChangeListener is the one that has one arg only!

    Java Code:
    // PropertyChangeListener Interface
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(GraphicOps.SHAPE_PROPERTY)) {
                int count = ((Integer)evt.getNewValue()).intValue();
                boolean state = (count > 0); // is the shape count
    
                clearButton.setEnabled(state);
                undoButton.setEnabled(state);
                printButton.setEnabled(state);
            }
        }
    Last edited by willemjav; 11-16-2013 at 11:38 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: struggling with properties

    evt.getPropertyName().equals(GraphicOps.SHAPE_PROP ERTY) would be true if something, somewhere, called firePropertyChange(...) with a property name of GraphicOps.SHAPE_PROPERTY on the PropertyChangeSupport that this listener is registered to. If you didn't specify the event name when you added the listener, then it will be called for all events fired on that PropertyChangeSupport.

    BTW, c and bc are horrible names for classes. See the link in my sig.
    Get in the habit of using standard Java naming conventions!

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: struggling with properties

    You are right, because it works out like you say:
    GraphicOps.SHAPE_PROPERTY is a String constant that holds "shape_property".
    And .equals works out to be truth when evt.getPropertyName() = "shape_property", which is the case in that application!

    The point is kjkrum, I'd like to understand how works this really because I did not store that string into the property!
    Who did that and why is that there, what is the hidden mechanics behind it?
    Remember the listener got installed like this: c.addPropertyChangeListener(bc);
    and I did not use the other (overload) method: c.addPropertyChangeListener("shape_property",gp);
    which, by to way also works great!

    Concerning the names of classes you are also right, but the code example is not mine,
    it is of Kim Topley the author of the JFC book (a good text book of learning java in depth).

    So the final question is how did the string endup presenting the canvas property, a JPanel
    and a Component which fires property (more) events!
    Last edited by willemjav; 11-17-2013 at 09:01 AM.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: struggling with properties

    The only "hidden mechanics" are in the code you haven't shown us. Are you asking how the PropertyChangeEvent got its property name? To answer that, you will have to find out what is calling PropertyChangeSupport#firePropertyChange(...).
    Get in the habit of using standard Java naming conventions!

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Re: struggling with properties

    The only place that string is mentioned is at the above interface, line 18:
    // Property for count of graphic shapes
    public static final String SHAPE_PROPERTY = "shape_property";
    No mention in the rest of the code (8 classes!)

    "To answer that, you will have to find out what is calling PropertyChangeSupport#firePropertyChange(...)."

    I will look into that one, thanks

Similar Threads

  1. Struggling need help
    By acbar in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 06:00 AM
  2. Struggling to import JAR
    By Mr ChriZ in forum Android
    Replies: 0
    Last Post: 01-12-2012, 11:53 AM
  3. Struggling with this code
    By Shimless12 in forum New To Java
    Replies: 5
    Last Post: 07-14-2011, 07:06 PM
  4. Struggling with enum!
    By XmisterIS in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 01:23 PM
  5. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 06:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •