Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes check if setEditable is defined as a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "check if setEditable is defined as a method" Watch "check if setEditable is defined as a method" New topic
Author

check if setEditable is defined as a method

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 245
How to check if the setEditable method is defined as a method for a class such as JComponent?
e.g.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If it isn't defined as a method, the compiler will tell you so.

Edit: Wait a minute... perhaps you actually meant you want to check if the JComponent object is actually an object of a subclass which does have a setEditable() method? In that case you should probably choose the actual subclass you're interested in (say JTable for example) and then use the "instanceof" operator:


Or perhaps you should reconsider that design entirely. When beginners write code like that, it's often because they haven't figured out to keep references to components that they want to work with. If you don't keep track of those references, you are reduced to grubbing through a pile of components to try to get the references back. It's possible your question arises from that sort of programming.
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
what about idea use reflection? something like this:



but.. really Paul Clapham - great answer!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Reflection is certainly an answer to that question, too. But usually you would use reflection if you were writing a general-purpose framework, like for example an IDE or a GUI designer. If you find yourself using reflection for an application with a specific purpose (like a calculator, for example) then there's a good chance you shouldn't be going there.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You ought to be looking in the API, and you find this, or this, so you can work out from those methods whether your class does or does not have a setEditable method.
You can also look in the API index under S, but there will be many thousands of lines for you to look at. Here, ctrl-F is your friend
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: check if setEditable is defined as a method