Constructs a tree of the components in a swing application
You can add this repository to your Java Project using JitPack.
import java.io.FileWriter;
import java.io.IOException
import com.github.luskjh.SwingTree;
/* We're using the automaton library (https://github.com/renatoathaydes/Automaton)
* for its GUI selectors
*/
import com.athaydes.automaton.Swinger;
...
/* Here we're using reflection to get the main class of the swing application */
Class<?> mainClass = Class.forName("package.then.classname");
Method method = mainClass.getDeclaredMethod("main", String[].class);
/* Start swing application */
String[] params = {};
method.invoke(null, (Object) params);
swinger = Swinger.forSwingWindow();
/* Give time for application to start */
swinger.pause(500);
Component frame = swinger.getAt("type:FrameClassName");
/* assemble tree */
SwingTree st = new SwingTree(frame);
/* Get JSON - currently the only output format supported */
JSONObject obj = st.toJSON();
/* Write to file */
try (FileWriter file = new FileWriter("/path/to/file.json")) {
file.write(obj.toJSONString());
} catch (IOException e) {
System.out.println("Error writing JSON to file");
}