0

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 ();
     }
}
}
2
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. Commented Mar 14, 2018 at 11:22
  • Im not able to build a UI in swing using XML. Can you help me out with this? Commented Mar 14, 2018 at 11:31

1 Answer 1

2

Your XML file was obviously produced from a JFrame object by using java.beans.XMLEncoder.
Therefore, decoding this XML file to produce the original object is very simple. Just use java.beans.XMLDecoder like this:

public class Main {

    public static void main(String[] args) throws IOException {
        XMLDecoder xmlDecoder = new XMLDecoder(new FileInputStream("MasterXML.xml"));
        Object frame = xmlDecoder.readObject();
        xmlDecoder.close();
    }
}

You don't need to bother with low-level XML-parsing like you did with DocumentBuilder-

When running the above code this JFrame will show up:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@sirisandela Welcome, good to hear this. You may want to accept the answer if it was helpful. See also here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.