Results 1 to 3 of 3

Thread: Dynamic cast

  1. #1
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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
                }
            }
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Well, I tought of that way too. Then I remembered what I was doing in PHP and tried to do it that way but obviously I failed ^^. Seems like I am going to do it with ArrayList. Thanks!

    Vizualni

Similar Threads

  1. Need Help with Class Cast Exception
    By soxfan714 in forum New To Java
    Replies: 2
    Last Post: 11-03-2011, 05:33 PM
  2. Cast int to char
    By Chasingxsuns in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 07:27 AM
  3. Cannot cast JNLPAppletLauncher to itself
    By kingofearth in forum Java Applets
    Replies: 0
    Last Post: 01-19-2010, 08:37 PM
  4. Cast From Image to String
    By Deepa in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 03:07 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 12:20 PM

Posting Permissions

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