I'm looking for a way to build UI using XML and swing. I have a code in XML and looking for a way to get UI out of it using swing. I tried below Java code but im not sure if this will help me display frame and other components.
Please help.
Below is the XML code which has JFrame and other things I want in my UI. I want to call this file from a java code so that I can display what all I wrote in XML.
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_161" class="java.beans.XMLDecoder">
<object class="javax.swing.JFrame">
<void property="size">
<object class="java.awt.Dimension">
<int>208</int>
<int>87</int>
</object>
</void>
<void property="contentPane">
<void method="add">
<object id="JLabel0" class="javax.swing.JLabel">
<void property="text">
<string>Hello1</string>
</void>
</object>
</void>
<void method="add">
<object id="JButton0" class="javax.swing.JButton">
<string>Hello2</string>
</object>
</void>
<void property="layout">
<object class="java.awt.BorderLayout">
<void method="addLayoutComponent">
<object idref="JButton0"/>
<string>South</string>
</void>
<void method="addLayoutComponent">
<object idref="JLabel0"/>
<string>Center</string>
</void>
</object>
</void>
</void>
<void property="name">
<string>frame0</string>
</void>
<void property="title">
<string>Save/Load View</string>
</void>
<void property="visible">
<boolean>true</boolean>
</void>
</object>
</java>
Below is the Java code:
import javax.swing.*;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MasterScreen{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("MasterXML.xml"));
System.out.println("Siri");
doc.getDocumentElement ().normalize ();
System.out.println("Siriri");
}
catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
}
}
