#Annotations

1 messages · Page 1 of 1 (latest)

spiral dock
#

I tryed to add all components which are marked with the Component Annotation, but the components not getting added

// Adds all components to the JPanel
    private void addComponents() throws InvocationTargetException, IllegalAccessException {
        for(Method method : getClass().getDeclaredMethods()){
            if(method.isAnnotationPresent(Component.class))
                add(method.invoke(this));
        }

        for (int x = 0; x < getComponents().length; x++)
            getComponents()[x].setFocusable(false);
    }

    // Single Player button
    @Component
    private JButton singlePlayer() {
        return singlePlayer;
    }

    // Multi Player button
    @Component
    private JButton multiPlayer() {
        return multiPlayer;
    }
marble shoalBOT
#

<@&987246487241105418> please have a look, thanks.

marble shoalBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

spiral dock
#

Is there any way to add the return value(swing components) due annotations?

spiral dock
#

Updated code, but still not work: ```Java
private void addComponents() throws InvocationTargetException, IllegalAccessException {
for (Method method : getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(Component.class))
if (method.getAnnotation(Component.class).component() == Components.JBUTTON)
add((JButton) method.invoke(this));
else if (method.getAnnotation(Component.class).component() == Components.JLABEL)
add((JLabel) method.invoke(this));
}

    for (int x = 0; x < getComponents().length; x++)
        getComponents()[x].setFocusable(false);
}

// Single Player button
@Component(component = Components.JBUTTON)
private JButton singlePlayer() {
    return singlePlayer;
}

// Multi Player button
@Component(component = Components.JBUTTON)
private JButton multiPlayer() {
    return multiPlayer;
}```
marble shoalBOT
#

Closed the thread.

spiral dock
#

T

spiral dock
#

Yes

torpid oar
#

can you show it ?

spiral dock
#

Cant really show it because i deleted it but it was: ```Java
@Target(...METHOD)
@Retention(Retention.RUNTIME)
public @interface Component{
Components component():
}
enum Components{
JBUTTON, JLABEL
}

#

im pretty sure the annotation itself isnt the problem

#

I also had no exceptions, they just did not got added to the panel

burnt lodge
#

Did you try put a breakpoint or print in add method/if statement

torpid oar
#

Did you try to debug it ?

#

where does it fail ?

#

does the isAnnotationPresent fail ?

#

or is it getAnnotation ?

#

or something else ?

#

also I struggle to see any benefit to using annotations

#

wouldn't it be way simpler in every way to just use method references ?

spiral dock
torpid oar
spiral dock
#

the panel is just empty without the added buttons

torpid oar
#

where does it fail ?

#

is it the if ?

#

the list.add ?

#

the view ?

#

what ?

spiral dock
spiral dock
torpid oar
spiral dock
#

i cant really test it because as i said i deleted it

torpid oar
#

then I can't help you

#

I can't help you with something if you can't even give more details than it doesnt work