2013-07-27 98 views
0

我正在處理從XML文件中收集的數據。我創建了一個名爲XMLReader的類,它假定能夠使用名爲readFromFile的方法來讀取我的XML數據格式,該方法返回ArrayList<HashMap<String, String>>JTable不顯示我的xml數據

我還創建了一個類extendsAbstractTableModel,並且我從構造函數創建了一個XMLReader實例並調用readFromFile方法。關鍵是我實現了我所知道的需要實現的一切,並覆蓋了所有需要覆蓋的內容,但仍然無法讓我的JTable顯示XML文件中的數據。波紋管分別是XMLReader級和extendsAbstractTableModel級。

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<HashMap<String, String>> 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<HashMap<String, String>> tblData = new ArrayList<>(); 


     for(int i = 0; i<nList.getLength(); i++) 
     { 
      Node nNode = nList.item(i); 
      NamedNodeMap attr = nNode.getAttributes(); 
      Node nodeAttr = attr.getNamedItem("id"); 
      String[] rowValues = new String[8]; 
      HashMap<String, String> mp = new HashMap<>(); 

      sN = nodeAttr.getTextContent(); 


      if(nNode.getNodeType() == Node.ELEMENT_NODE) 
      { 
       NodeList valueList = nNode.getChildNodes(); 
       for(int j=0; j<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; 
} 

這裏是AbstractTableModel

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<TableData> tblData = new ArrayList<TableData>(); 
ArrayList<FileContent> tblData2 = new ArrayList<FileContent>(); 

String[] columnName = {"S/N", "VisitorName", "Date of Visit", "Scheduled Date", "Whom to see", "Reason of visit", "status of Visit"}; 
ArrayList<HashMap<String, String>> 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]; 
} 

} 

搞笑的事情發生了,該表顯示,當我把代碼波紋管的線的信息,它只是針對測試的,但是當我把它放在的TableModel構造它顯示我所有的值,但是當我刪除表拒絕顯示

for(int i=0; i<fileData.size(); i++){ 
     tblData.add(new TableData(1, "", "", "", "", "", "")); 
    } 

我一直在這個對於T他過去ten hours現在沒有解決或解釋爲什麼所有有趣的事情發生。我將不勝感激所有幫助。由於

回答

3

就出在這裏你的問題的原因:

@Override 
public int getRowCount() { 
return tblData.size(); 
} 

tblData無關,與你的XML數據。你應該有(確保fileData不爲空):

@Override 
public int getRowCount() { 
return fileData.size(); 
} 

通過將代碼:

for(int i=0; i<fileData.size(); i++){ 
    tblData.add(new TableData(1, "", "", "", "", "", "")); 
} 

你只能讓tblDatafileData大小相等。因此,getRowCount方法返回一個有效值。

+0

非常感謝,我甚至都沒有嘗試翻閱。謝謝更多 –

+0

我只是不能停止激動,非常感謝 –