Results 1 to 3 of 3
Thread: Dynamic cast
- 08-17-2010, 05:48 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Dynamic cast
So, my problem is that I am trying to disable all fields that starts with "_". I don't know how to make proper cast. I hope you guys could help me.;)
Java Code:import java.lang.reflect.Field; import javax.swing.JButton; import javax.swing.JTextField; public class test{ private JButton _test1; private JButton _test2; private JButton test3; private JTextField _test3; public static void main(String [] args){ new test(); } public test(){ setAllEnabled(true); } private void setAllEnabled(boolean set){ Field [] fields = this.getClass().getDeclaredFields(); for(Field f:fields){ if(f.getName().startsWith("_")){ //so...I don't know how to do that type of casting } } } }
-
Why not simply put all the components you wish disabled in a collection such as an ArrayList and then iterate through the collection and disable each component it contains? In my opinion using reflection to do what you're trying to do doesn't pass the smell test.
- 08-17-2010, 06:21 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Need Help with Class Cast Exception
By soxfan714 in forum New To JavaReplies: 2Last Post: 11-03-2011, 04:33 PM -
Cast int to char
By Chasingxsuns in forum New To JavaReplies: 1Last Post: 02-05-2010, 06:27 AM -
Cannot cast JNLPAppletLauncher to itself
By kingofearth in forum Java AppletsReplies: 0Last Post: 01-19-2010, 07:37 PM -
Cast From Image to String
By Deepa in forum New To JavaReplies: 4Last Post: 03-03-2009, 02:07 PM -
How can I cast Object as an int
By romina in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks