Note that there are some explanatory texts on larger screens.

plurals
  1. POJTable not displaying my xml data
    primarykey
    data
    text
    <p>I am working with data collected from an XML file. I created a class called <code>XMLReader</code> which is suppose to be able to read my XML data format using a method called <code>readFromFile</code> which returns an <code>ArrayList&lt;HashMap&lt;String, String&gt;&gt;</code>. </p> <p>I Also created a class that <code>extends</code> <code>AbstractTableModel</code>, and I create an instance of <code>XMLReader</code> from the constructor and call the <code>readFromFile</code> method. The point is I implemented everything I know that needs to be Implemented and overrode everything that needs overriding, but still I still can't get my <code>JTable</code> to display the data from the XML file. Bellow is the <code>XMLReader</code> class and the class that <code>extends</code> <code>AbstractTableModel</code> respectively.</p> <pre><code> package meetingmanager; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XMLReader { private static final String filePath = "XMLData.xml"; public String sN; public String firstName; public String surName; public String visitTime; public String scheduledTime; public String whomToVisit; public String reasonForVisit; public String status; public XMLReader() { } public ArrayList&lt;HashMap&lt;String, String&gt;&gt; readFromFile() { try{ File xmlFile = new File(filePath); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = docFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("Visitor"); ArrayList&lt;HashMap&lt;String, String&gt;&gt; tblData = new ArrayList&lt;&gt;(); for(int i = 0; i&lt;nList.getLength(); i++) { Node nNode = nList.item(i); NamedNodeMap attr = nNode.getAttributes(); Node nodeAttr = attr.getNamedItem("id"); String[] rowValues = new String[8]; HashMap&lt;String, String&gt; mp = new HashMap&lt;&gt;(); sN = nodeAttr.getTextContent(); if(nNode.getNodeType() == Node.ELEMENT_NODE) { NodeList valueList = nNode.getChildNodes(); for(int j=0; j&lt;valueList.getLength(); j++) { rowValues[0] = sN; Node eachValue = valueList.item(j); String nodeName = eachValue.getNodeName(); String value = eachValue.getTextContent(); mp.put("sN", sN); int count = j+1; switch(nodeName) { case "FirstName": firstName = value; rowValues[count] = value; mp.put("FirstName", value); break; case "SurName": surName = value; rowValues[count] = value; mp.put("SurName", value); break; case "VisitTime": visitTime = value; rowValues[count] = value; break; case "ScheduledTime": scheduledTime = value; rowValues[count] = value; mp.put("ScheduledTime", value); break; case "WhomToVisit": whomToVisit = value; rowValues[count] = value; mp.put("WhomToVisit", value); break; case "ReasonForVisit": reasonForVisit = value; mp.put("ReasonForVisit", value); rowValues[count] = value; break; case "Status": status = value; rowValues[count] = value; mp.put("Status", value); break; } } // tblData.add(rowValues); tblData.add(mp); } } return tblData; //System.out.println(tblData.get(1000)[0]); }catch(ParserConfigurationException | SAXException | IOException | DOMException ex) { } return null; } </code></pre> <p>Here is the <code>AbstractTableModel</code> class</p> <pre><code> package meetingmanager; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class TableModel extends AbstractTableModel implements TableModelListener{ ArrayList&lt;TableData&gt; tblData = new ArrayList&lt;TableData&gt;(); ArrayList&lt;FileContent&gt; tblData2 = new ArrayList&lt;FileContent&gt;(); String[] columnName = {"S/N", "VisitorName", "Date of Visit", "Scheduled Date", "Whom to see", "Reason of visit", "status of Visit"}; ArrayList&lt;HashMap&lt;String, String&gt;&gt; fileData; @SuppressWarnings("unchecked") public TableModel() { fileData = new XMLReader().readFromFile(); } @Override public void tableChanged(TableModelEvent e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int getRowCount() { return tblData.size(); } @Override public int getColumnCount() { return 7; } @Override public Object getValueAt(int rowIndex, int columnIndex) { switch(columnIndex){ case 0: return fileData.get(rowIndex).get("sN"); case 1: return fileData.get(rowIndex).get("FirstName")+ " "+ fileData.get(rowIndex).get("SurName"); case 2: return fileData.get(rowIndex).get("VisitTime"); case 3: return fileData.get(rowIndex).get("ScheduledTime"); case 4: return fileData.get(rowIndex).get("WhomToVisit"); case 5: return fileData.get(rowIndex).get("ReasonForVisit"); case 6: return fileData.get(rowIndex).get("Status"); default: return ""; } } @Override public String getColumnName(int col) { return columnName[col]; } } </code></pre> <p>Funny things happen, the table displays the information when I put the line of code bellow, it was only meant for testing, but when I placed it in the constructor of <code>TableModel</code> it displayed all my values, but when I remove it the table refuses to display</p> <pre><code>for(int i=0; i&lt;fileData.size(); i++){ tblData.add(new TableData(1, "", "", "", "", "", "")); } </code></pre> <p>I have been working on this for the past <code>ten hours</code> now without a solution or explanation to why all the funny things happen. I would appreciate all help. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload