Adding GUI functionality to my Java Program that writes XML Files



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");

}


}

}

No comments:

Post a Comment