Results 1 to 4 of 4
  1. #1
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default getComponentType.

    I'm running the following code in my MouseDragged method:

    Java Code:
    selection.setBounds((int) Math.min(anchor.x, e.getX()),
    					(int) Math.min(anchor.y, e.getY()),
    					(int) Math.abs(e.getX() - anchor.x),
    					(int) Math.abs(e.getY() - anchor.y));
    
    			for (Component c : this.getComponents()) {
    				boolean inBox = selection.intersects(c.getBounds());
    
    				PlaceIcon pi = (PlaceIcon) c;
    				pi.setSelected(inBox);
    this worked fine when I only had components of type PlaceIcon but now I have added components of another type and obviously cant cast all components to type PlaceIcon.
    I need to either specify which type of components getComponents() gets OR check that c is of type PlaceIcon before casting it.

    Any advice on this.




    Problem is solved, I've just built a set of placeIcons as i go and and just loop thorough those.
    Last edited by Crakrjack; 11-05-2013 at 01:37 AM. Reason: problem solved

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: getComponentType.

    Check out the instanceof operator.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,758
    Rep Power
    5

    Default Re: getComponentType.

    I find the OPs own solution a lot cleaner though ;) But indeed when you do not have the luxury to maintain a secondary collection, instanceof is a necessary evil. Either that or using Class.isAssignableFrom().

    A third option which I have not to long ago started to really appreciate for specific situations (frameworky situations) is to actually use annotations to mark certain objects you may want to pick out of a large pile. I mean it is as simple as creating an annotation:

    Java Code:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface GetThis {
    }
    Slapping it on an object

    Java Code:
    @GetThis
    public class MyOwnButton extends JButton {
      // bla
    }
    and then you can use the isAnnotationPresent() method of Class to filter out the classes with this annotation:

    Java Code:
    for (Component c : this.getComponents()) {
    
      if(c.getClass().isAnnotationPresent(GetThis.class)){
        // bla
      }
    }
    I wouldn't use it for this particular case as the OPs own solution is still a lot simpler and better, but it is worth to mention it anyway IMO :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,417
    Rep Power
    5

    Default Re: getComponentType.

    Another possibility is to go back to the instanceof solution. Simply create an interface of the methods common to all of your created classes and have those
    classes implement the interface. Then you only need a single instanceof check for the interface type. And then cast to that type.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Posting Permissions

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