GUI from class definition

Is there a lib that can build a simple graphical interface looking at the signature of a class or method?

Example:

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

To

gui

Thus, when the button is pressed, the method is called and the result is displayed.

Do you know any examples of anything like this in Java or other languages?

thanks

+6
java reflection user-interface
source share
7 answers

I have coded a very simple example that shows how this can be achieved using multiple-line reflection code and a little wobble. Perhaps init () should return the number of methods found for use in the GridLayout, then it will 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(); } } 
+1
source share

Actually there is Java Management Extensions or JMX . But this is a heavy heavy hammer for the example that you give here. But it provides methods and properties in a standard way, and you can get them using tools such as web interfaces, JConsole, remote procedure call, etc.

It is used to manage and manage application servers, etc.

+1
source share

I don’t know about the existence of such a library, but it will be easy for you to create it - suppose you just need 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 JPanel, using FlowLayout and some reflection magic.

+1
source share

I wrote a thesis on this topic. We use it at the university as a user interface for our simulation software. And we are still working on it (as part of my doctoral dissertation). It is planned to release it as an open source project this year. Although optimized for technical modeling, it can be used in other areas. If you're interested, check out the VRL-Introduction

+1
source share

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

 new ReflectionUI().openObjectFrame(new MyMath(), null, null); 
+1
source share

If you know or want to try groovy , it would be very easy to build such a gui. You simply combine SwingBuilder for the actual GUI with Groovy MetaClass to open the methods of your class. You can also use the Groovy console to add some dynamic features.

0
source share

If you want to write your code as JavaBeans , several available testing tools may be available. There is much more to the JavaBeans specification than to getters and seters.

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

0
source share

All Articles