开发者

GUI from class definition

开发者 https://www.devze.com 2023-01-04 08:53 出处:网络
Is there a lib that can build a simple GUI by looking at a class or method signature? For Example开发者_开发知识库:

Is there a lib that can build a simple GUI by looking at a class or method signature?

For Example开发者_开发知识库:

class MyMath{
    public static double sqrt(double d){
         //impl
    }
}       

To

GUI from class definition

So that when the button is clicked the method is invoked and the result is shown.

Do you know any examples of something like that in Java or other languages?

thanks


I coded a very basic example, which shows how this can be achieved using a few lines reflection code and little swing. Maybe init() should return the number of methods found for use in the GridLayout, then it would be more dynamic.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MathPanel extends JFrame {

    public static double sqrt(double d) {
        return Math.sqrt(d);
    }

    public static double sin(double d) {
        return Math.sin(d);
    }

    public static double cos(double d) {
        return Math.cos(d);
    }

    class Item implements ActionListener {
        JTextField result = new JTextField();
        JTextField param = new JTextField();
        JButton button;
        Method m;
        public Item(JFrame frame,String name, Method m) {
            button = new JButton(name);
            button.addActionListener(this);
            this.m = m;
            frame.getContentPane().add(param);
            frame.getContentPane().add(button);
            frame.getContentPane().add(result);
        }

        public void actionPerformed(ActionEvent e) {
            String cmd  = e.getActionCommand();
            Item item = map.get(cmd);
            try {
                double dbl = Double.parseDouble(item.param.getText());
                Object invoke = item.m.invoke(this, dbl);
                item.result.setText("" + invoke );
            } 
            catch (Exception x) {
                x.printStackTrace();
            }
        }
    }

    HashMap<String,Item>map = new HashMap<String, Item>(); 


    public MathPanel() {
        setLayout(new GridLayout(3,3));
        setTitle("Test");
        setSize(400, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void init() {
        try {
            Method[] allMethods = MathPanel.class.getDeclaredMethods();
            for (Method m : allMethods) {
                String mname = m.getName();
                Class<?> returnType = m.getReturnType();
                if ( returnType.getName().equals("double")) {
                    Item item = new Item(this,mname,m);
                    map.put(mname,item);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MathPanel mp = new MathPanel();
        mp.init();
    }
}


Actually there is in the form of Java Management Extensions or JMX. But it is a very heavy hammer for the kind of example you are giving here. But it exposes methods and properties in a standard way and you can get to them using tools like web interfaces, JConsole, remote procedure call and so forth.

It is used to instrument and manage application servers and the like.


I'm not a aware of the existence of such a library, but it will be fairly easy for you to create one - a assume you simple want a text field for each argument, a button to execute the method with the selected argument and a label/field to show the result. Something like this can be achieved with a JPanel using FlowLayout and a bit of reflection magic.


I wrote my diploma thesis about exactly this topic. We use it at university as UI for our simulation software. And we still work on it (as part of my PhD thesis). It is planned to release it as open source project this year. Although optimized for the domain of technical simulation, it can be used in other areas as well. If you are interested, have a look at VRL-Introduction


There is the ReflectionUI library that seems to do exactly what you want:

new ReflectionUI().openObjectFrame(new MyMath(), null, null);


If you happen to know or want to try the groovy language it'd be very easy to build such a gui. You'd simply combine the SwingBuilder for the actual GUI with Groovy's MetaClass to discover the methods of your class. You could also take advantage of the groovy console to add some dynamic features.


If you're willing to write your code as JavaBeans, there may be a few lightweight test tools available. There's much more to the JavaBeans spec than getters and setters.

BeanBox seems to have disappeared from the JDK since I last used it (in 1997), but look under the Related Links > Products and Technologies section of this link.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号