Skip to content

New feature for SDK: AbstractControl + Button #518

@capdevon

Description

@capdevon

I opened this Issue to keep track of forum discussions to add the ability to create in custom AbstractControls, functions that can be executed by button clicks.

https://hub.jmonkeyengine.org/t/new-feature-for-sdk-abstractcontrol-buttonproperty/46927

It requires the creation of a dedicated library, for example jme-sdk-devtools, to be published on the MAVEN repository. In this way, the library can be used by users in their Maven or Gradle projects to add graphical functions to AbstractControls and write their own editors with the SDK.

Here is an example:

  1. ButtonProperty
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ButtonProperty {

    String name() default "";
    
    String tooltip() default "";
}

  1. MyControl
public class MyControl extends AbstractControl {

    @Override
    protected void controlUpdate(float tpf) {
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
    
    @ButtonProperty(name="Click Me", tooltip="sayHello")
    public void sayHello() {
        System.out.println("Hello: " + MyControl.class.getSimpleName());
    }

}
  1. UIBuilder

import java.lang.reflect.Method;
import org.apache.commons.lang3.reflect.MethodUtils;
import com.jme3.scene.control.Control;

import javax.swing.*;

...

    public static JPanel buildPanel(Control control) {

        JPanel container = new JPanel(false);
        container.setLayout(new MigLayout("fillx"));

        ...
        
        List<Method> methods = MethodUtils.getMethodsListWithAnnotation(control.getClass(), ButtonProperty.class);
        for (Method method : methods) {
            ButtonProperty bp = method.getAnnotation(ButtonProperty.class);
            JButton button = new JButton(bp.name());
            button.setToolTipText(bp.tooltip());
            button.addActionListener(e ->  {
                try {
                    method.invoke(control);
                } catch (ReflectiveOperationException ex) {
                    ex.printStackTrace();
                }
            }));
            
            container.add(new JLabel(""), "align righ");
            container.add(button, "wrap, pushx, growx");
        }

        return container;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions