我正在尝试从 URL 读取 XML 文件,然后使用该数据创建 JTable。
当我尝试打印数据时,它工作得很好,但是创建 JTable 时有些东西不起作用,我不知道为什么。
我没有收到任何异常或错误。
这是我写的代码,希望你能帮助我。
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.awt.BorderLayout;
import java.net.URL;
import java.net.URLConnection;
public class Table extends JTable {
JTable currTable;
Object rowData[][] = new String[15][6];
Object columnNames[] = { "Name", "Unit", "CurrencyCode", "Country", "Rate", "Change" };
public Table() {
try {
URL url = new URL("http://www.boi.org.il/currency.xml");
URLConnection connection = url.openConnection();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(connection.getInputStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("CURRENCY");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
rowData[temp][0] = new String(eElement
.getElementsByTagName("NAME").item(0)
.getTextContent());
rowData[temp][1] = new String(eElement
.getElementsByTagName("UNIT").item(0)
.getTextContent());
rowData[temp][2] = new String(eElement
.getElementsByTagName("CURRENCYCODE").item(0)
.getTextContent());
rowData[temp][3] = new String(eElement
.getElementsByTagName("COUNTRY").item(0)
.getTextContent());
rowData[temp][4] = new String(eElement
.getElementsByTagName("RATE").item(0)
.getTextContent());
rowData[temp][5] = new String(eElement
.getElementsByTagName("CHANGE").item(0)
.getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
currTable = new JTable(rowData, columnNames);
}
public static void main(String[] args) {
JFrame printTable = new JFrame("Exchange Rate Table");
printTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable myTable = new Table();
JScrollPane scrollPane = new JScrollPane(myTable);
printTable.add(scrollPane, BorderLayout.CENTER);
printTable.setSize(400, 500);
printTable.setVisible(true);
}
}
最佳答案
这是因为您要将扩展 JTable
的 Table 对象添加到 JScrollPane
中。您无需向其中添加任何内容,而是将数据设置为 currTable
成员变量。要使其正常工作,您应该将 main
函数中的两行分别更改为以下内容:
Table myTable = new Table();
JScrollPane scrollPane = new JScrollPane(myTable.currTable);
但实际上,从 Table
类扩展 JTable
是没有意义的。创建一个创建并返回 rowData
对象的方法,并在主函数中创建一个 JTable
,如下所示:
JTable myTable = new JTable(rowData, columnNames);
还有一些更多的评论,以改进您的代码:
- 无需调用
String
构造函数,您正在创建额外的对象。rowData[temp][0] = eElement.getElementsByTagName("NAME").item(0).getTextContent();
也可以正常工作 - 不要在数组创建代码中对 15 和 6 等数字进行硬编码。 15 应该是 XML 元素的数量,您可以使用
nList.getLength()
获取该数量。 6 是columnNames
数组的长度。 - 看看Google Java Style Guide .
关于java - 创建 JTable java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30890917/