0

I have two classes here. The first one is one that writes data to an XML File, and the second is one that uses JFrame, and uses an action handler on the button to collect the data from the three text fields.

My two problems:

1) The GUI is blank - It's been a while since I played around with JFrame, so bear with me.

2) I need to extract the 3 strings from the text boxes, and insert them into my insertNewEntry() function. The attribute will just be an integer that I'll increment, and the element and document will be "rootElement", and "doc", respectively.

Thanks very much for the help!

import java.io.File;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.swing.*;

import java.awt.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

@SuppressWarnings("unused")
public class createXML extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String [] args) throws ParserConfigurationException, TransformerException{

        String address = "/home/leo/workspace/Test/Files/src/xmlOutput";
        Scanner s = new Scanner(System.in);
        int ID = 0;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element rootElement = doc.createElement("Company");
        doc.appendChild(rootElement);

        GUI gui = new GUI();

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(500, 300);
        gui.setVisible(true);


    //  insertNewEntry(rootElement, doc, ID, firstName, lastName, salary);


        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();

        DOMSource source = new DOMSource(doc);
        StreamResult sr = new StreamResult(new File(address));

        t.transform(source, sr);

        System.out.println("File created.");

    }

    private static void insertNewEntry(Element rootElement, Document doc, String attr, String fName, String lName, String sal){


        Element employee = doc.createElement("Employee");
        employee.setAttribute("ID", attr);
        rootElement.appendChild(employee);

        Element firstName = doc.createElement("First_Name");
        firstName.setTextContent(fName);
        employee.appendChild(firstName);

        Element lastName = doc.createElement("Last_Name");
        lastName.setTextContent(lName);
        employee.appendChild(lastName);

        Element salary = doc.createElement("Salary");
        salary.setTextContent(sal);
        employee.appendChild(salary);

    }

}

Next class...

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("unused")
public class GUI extends JFrame {

    private static final long serialVersionUID = 1L;

    private JLabel item;
    private JTextField firstName;
    private JTextField lastName;
    private JTextField salary;
    private JButton button1;

    GUI(){

    super("XML Writer");

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0,2));

    JLabel fn = new JLabel("First Name");
    firstName = new JTextField();
    panel.add(fn);
    panel.add(firstName);

    JLabel ln = new JLabel("Last Name");
    lastName = new JTextField();
    panel.add(ln);
    panel.add(lastName);

    JLabel s = new JLabel("Salary");
    salary = new JTextField();
    panel.add(s);
    panel.add(salary);

    button1 = new JButton("Click Me!");
    button1.setSize(20, 10);
    panel.add(button1);

    Handler handler = new Handler();
    button1.addActionListener(handler);

    }

    private class Handler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String fn = ""; String ln = ""; String sal = "";

            fn = firstName.getText();
            ln = lastName.getText();
            sal = salary.getText();

            JOptionPane.showMessageDialog(null, "Information stored in XML file");

        }


    }

}
1
  • 1
    createXML doesn't need to extend from JFrame... Commented Aug 1, 2014 at 2:04

1 Answer 1

2

Let's start with the obvious problem...

You create a JPanel, add your components to but never add that panel to anything....

GUI() {

    super("XML Writer");

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0, 2));

    JLabel fn = new JLabel("First Name");
    firstName = new JTextField();
    panel.add(fn);
    panel.add(firstName);

    JLabel ln = new JLabel("Last Name");
    lastName = new JTextField();
    panel.add(ln);
    panel.add(lastName);

    JLabel s = new JLabel("Salary");
    salary = new JTextField();
    panel.add(s);
    panel.add(salary);

    button1 = new JButton("Click Me!");
    button1.setSize(20, 10);
    panel.add(button1);

    Handler handler = new Handler();
    button1.addActionListener(handler);

    //...???
}

Try adding the panel to the GUI...

    add(panel);
}

Also, your CreateXML class doesn't need extend from JFrame, it's not even using any of it's functionality...

Your second issues is a matter of opinion...

The basic solution would be to call the static createXML.insertNewEntry(...) method, but your GUI is missing some information it needs in order to make this work...

Personally, I would create a "model" interface which contained the Document and a simplified insert method which took only the required fields from the GUI...

Updated with model example

Basic model structure...

public interface EmployeeModel {

    public void insert(String fName, String lName, String sal);

}

public interface XMLEmployeeModel extends EmployeeModel {

    public void save(File file) throws IOException;

}

public abstract class AbstractXMLEmployeeModel implements XMLEmployeeModel {

    private Document doc;

    public AbstractXMLEmployeeModel(Document doc) {
        this.doc = doc;
    }

    public Document getDoc() {
        return doc;
    }

    @Override
    public void save(File file) throws IOException {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();

            DOMSource source = new DOMSource(doc);
            StreamResult sr = new StreamResult(file);

            t.transform(source, sr);
        } catch (TransformerFactoryConfigurationError | TransformerException exp) {
            exp.printStackTrace();
            throw new IOException("Failed to save Employee model", exp);
        }
    }

}

public class DefaultXMLEmployeeModel extends AbstractXMLEmployeeModel {

    private int count = 0;

    public DefaultXMLEmployeeModel(Document doc) {
        super(doc);
    }

    @Override
    public void insert(String fName, String lName, String sal) {
        Document doc = getDoc();
        Element root = doc.getDocumentElement();

        Element employee = doc.createElement("Employee");
        employee.setAttribute("ID", Integer.toString(count++));
        root.appendChild(employee);

        Element firstName = doc.createElement("First_Name");
        firstName.setTextContent(fName);
        employee.appendChild(firstName);

        Element lastName = doc.createElement("Last_Name");
        lastName.setTextContent(lName);
        employee.appendChild(lastName);

        Element salary = doc.createElement("Salary");
        salary.setTextContent(sal);
        employee.appendChild(salary);
    }

}

And example of GUI utilising it...

public static class GUI extends JFrame {
    //...
    private XMLEmployeeModel model;

    GUI(XMLEmployeeModel model) {

        super("XML Writer");

        this.model = model;
        //...
    }

    private class Handler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            String fn = "";
            String ln = "";
            String sal = "";

            fn = firstName.getText();
            ln = lastName.getText();
            sal = salary.getText();

            model.insert(fn, ln, sal, sal);

            JOptionPane.showMessageDialog(null, "Information stored in XML file");

        }

    }

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

2 Comments

Thanks very much! Yeah, I knew the first question was because of a stupid mistake - Haven't played around with JFrame in a while. I think I understand what you mean on the second question - Am working on it.
This was extremely helpful. Thanks again! If I could give more up-votes, I would.

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.