0

I want a Generic Method, however I'm not sure if it's ideal for this scenario, nor am I very much familiar with how Generic's work (if someone could link me a good tutorial or article, I'd very much appreciate it.)

However, I wanted to create a method that handles initialization of JComponents, and if the array of JComponents was All JRadioButtons, to be sent to a different method.

public void initializeComponent(JComponent...components)
{
     if(components[0] instanceof JRadioButton)
         initializeJRadioButtons(components[]);
}

However, this will only check whether the first one is a JRadioButton, I feel Generics may handle this better, but is there a way to check if all components are JRadioButtons without looping?

Such as if someone does this.

JRadioButton[] radioButtons = new JRadioButton[2];
...
initializeComponent(radioButtons);
7
  • Maybe you could make the task easier by looping over components, then do instanceof test and delegate to initializeRadioButton (no plural) for each component? Do you need to initialize arrays of components at once? I don't see how generics would help you do that. Commented Mar 3, 2014 at 9:42
  • @SubhrajyotiMajumder That won't work. Commented Mar 3, 2014 at 9:42
  • may be if all of the entry are not JRadioButton Commented Mar 3, 2014 at 9:44
  • Any particular reason not to use loops? Commented Mar 3, 2014 at 9:44
  • 1
    @Otanan Yes, figured that out now, too. So you have special methods for initilizing radio buttons, and this and that other stuff, but you want this as an entry method for all those? Why? Also, have you tried just overloading? init(Foo... foos), init(Bar... bars), etc.? Commented Mar 3, 2014 at 9:47

4 Answers 4

3

No, you can't check them without looping over them. Consider any process that would do it for you - at some point it would still have to check them one by one as they're distinct objects.

However, what you could do use use method overloading. You could have a method with the signature of

public void initializeComponent(JRadioButton...components)

You then know that every one of those components will be a radio button, hence can skip any checking.

You can then have the method you've already got in your question which may have to check each component to do the correct initialisation.

public void initializeComponent(JComponent...components)
Sign up to request clarification or add additional context in comments.

3 Comments

This is definitely what I might end up using, however, would List<JRadioButton> radioButtons = new ArrayList<>(components); work? Or would I get an exception thrown for trying to add a non JRadioButton to the list if the components array is holding a non-JRadioButton?
You'll get a syntax error because you're components variable type is not JRadioButton or a subtype of it (rather a supertype). It'll fail at compile time.
Thank you! I can't believe I didn't think of this!
1

I don't see what the problem of looping over the components is:

boolean isRadioButtons = true;
for (Component c : components) {
    if (!(c instanceof JRadioButton)) {
        isRadioButtons = false;
        break;
    }
}

Comments

1

Since you just want to check whether the array that is passed is actually a JRadioButton[] array or not, you can use the following code. Although this is a hack, that does the job:

public static void initializeComponent(JComponent... components) throws ClassNotFoundException {
    // Get the Class instance for an array of type JRadioButton.
    Class<?> clazz = Class.forName("[Ljavax.swing.JRadioButton;");

    if (clazz.isInstance(components)) { 
        System.out.println(true);
        initializeJRadioButtons((JRadioButton[]) components);
    }
}

The method uses the encoding for the JRadioButton array to get the Class instance for that type of array. See Class#forName() for more details on this.

If the components is an instance of JRadioButton[], then you'll get true as result. Remember, this is just a hack, as I already said. IMO, overloading is the best option for you.

Comments

0

No, there is not a built-in way to check if all entries in an array are of a specific sub type type.

Loop it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.