5 Replies Latest reply: Dec 14, 2009 7:00 AM by 843798 RSS

    Exception when using "get" method in Field

    843798
      Hi,

      I have a function detailed below:

      private void associateActions() {
      Field[] fields = this.getClass().getDeclaredFields();
      for (Field field : fields) {
      if (field.getType().isInstance(new JButton())) {
      try {
      field.setAccessible(true);
      JButton obj = (JButton) field.get(new JButton());
      // additional functions to be added using the JButton object
      } catch (Exception e) {
      System.out.println(e);
      }
      }
      }
      }

      I have a selection of JButton objects within the class (all declared Private). When I run the code I get a java.lang.IllegalArgumentException for each JButton object (small selection shown below), caused by the line "JButton obj = (JButton) field.get(new JButton());"

      java.lang.IllegalArgumentException: Can not set javax.swing.JButton field GUI.GUIMainFrame.about to javax.swing.JButton
      java.lang.IllegalArgumentException: Can not set javax.swing.JButton field GUI.GUIMainFrame.cancelSubmitButton to javax.swing.JButton
      java.lang.IllegalArgumentException: Can not set javax.swing.JButton field GUI.GUIMainFrame.closeSubject to javax.swing.JButton

      I though this would work fine and am struggling to get to the bottom of the exception. Can anyone shed any light?

      Thanks,

      Chris.
        • 1. Re: Exception when using "get" method in Field
          843798
          sorry, the code above looks terrible. That should read
              private void associateActions() {
                  Field[] fields = this.getClass().getDeclaredFields();
                  for (Field field : fields) {
                      if (field.getType().isInstance(new JButton())) {
                          try {
                              field.setAccessible(true);
                              JButton obj = (JButton) field.get(new JButton());
                   //           addActionToItem(button);
                          } catch (Exception e) {
                              System.out.println(e);
                          }
                      }
                  }
              }
          • 2. Re: Exception when using "get" method in Field
            843798
            There's a bunch of pointlessness in here, and I'm not even sure what you're trying to do. You don't need to create an instance of JButton in order to test if a field has the type javax.swing.JButton. Just compare the class object to javax.swing.JButton.class
            if(field.getType().equals(JButton.class) {//etc}
            Secondly, you're creating another JButton, and trying to invoke the field you have against that. What do you think this line:
            JButton obj = (JButton) field.get(new JButton());
            is going to do? You have a field from your own class, of type JButton, and are now trying to treat it as if it were a field declared on JButton. JButtons are JButtons, but they don't have JButtons. I can't really work out what you're hoping to achieve here, but it looks like you need to re-visit a reflection tutorial. I'm not convinced a method called "associateActions" should even need reflection, to be honest.

            What are you actually trying to achieve here? I mean, what question did you ask yourself, that caused you to decide to use reflection?
            • 3. Re: Exception when using "get" method in Field
              843798
              Hi,

              What I am trying to do is call "setAction" on a number of JButton and JMenuItem so that components with the same functionality share an Action. I have the code to generate and assign the Actions to the right components based on an idiom in another method and was hoping not to have to call that method manually on each component. So I thought I could use Reflection to get all of the attributes, strip out the JButtons and JMenuItems and call the method on each one. The method I posted was the first test-case for building that method.

              Your response has highlighted the problem and the code now works - thank you. I was reading the "get" method as being passed a template type object similar to the toArray methods elsewhere. Lesson learned.

              If you have a better way to overcome the problem above I would be interested in hearing it. Thanks Chris.
                  private void associateActions() {
                      Field[] fields = this.getClass().getDeclaredFields();
                      for (Field field : fields) {
                          if (field.getType().equals(JButton.class)) {
                              try {
                                  field.setAccessible(true);
                                  JButton obj = (JButton) field.get(this);
                       //           addActionToItem(button);
                              } catch (Exception e) {
                                  System.out.println(e);
                              }
                          }
                      }
                  }
              • 4. Re: Exception when using "get" method in Field
                EJP
                That's OK but I would probably make a JButton[] array of all the JButton fields I wanted to do this action on. That way you have more control and less code.
                • 5. Re: Exception when using "get" method in Field
                  843798
                  ejp wrote:
                  That's OK but I would probably make a JButton[] array of all the JButton fields I wanted to do this action on. That way you have more control and less code.
                  That's a better approach than the reflection solution. Another approach might be, rather than instantiating each button yourself, hiding that instantiation behind a factory method that instantiates JButtons, and adds the action.