File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

check if setEditable is defined as a method

albert kao
Ranch Hand

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

Joined: Oct 14, 2005
Posts: 19973

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: 46
what about idea use reflection? something like this:

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

Joined: Oct 14, 2005
Posts: 19973

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

Joined: Oct 13, 2005
Posts: 46375
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
I agree. Here's the link:
subject: check if setEditable is defined as a method
It's not a secret anymore!