views:

94

answers:

5

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

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

+1  A: 

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.

Peter Tillemans
+1  A: 

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.

Bozhidar Batsov
A: 

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.

tweber
After taking a quick look at groovy i don't see exactly which features would benefit me. Would it be any different than using reflection to look an the class and building the gui accordingly?
KarlsFriend
You can, of course, implement your application as well without groovy. I recommended it because both tasks (writing a simple GUI and using reflection) are greatly simplified using the groovy language. The choice was made because of the assumption that you are not constrained by a existing codebase.
tweber
A: 

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.

McDowell
+1  A: 

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();
    }
}
stacker
This is sort of what I meant. Of course it's more a proof of concept than a library. I think the concept could be extended to handle a lot of cases. This Question has been arround a long time, so i guess the library i was looking for has yet to be build.
KarlsFriend