shir
shir

Reputation: 13

Creating JTable java

I'm trying to read an XML file from a URL and then create a JTable with the data.

When I try just to print the data it works perfectly but something with the creation of the JTable is not working and I'm not sure why.

I dont get any exceptions or errors.

Here is the code i wrote, hope you can help me.

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

}

}

Upvotes: 0

Views: 873

Answers (1)

mavroprovato
mavroprovato

Reputation: 8372

This is because you are adding the Table object, which extends JTable, to the JScrollPane. You don't add anything to it, instead you set the data to the currTable member variable. To make it work, you should change the 2 respective lines in your main function to the following:

Table myTable = new Table();

JScrollPane scrollPane = new JScrollPane(myTable.currTable);

But really, it makes no sense to extend JTable from your Table class. Create a method that creates and returns the rowData object, and create a JTable in your main function like this:

JTable myTable = new JTable(rowData, columnNames);

Also some more comments in order to improve your code:

  • No need to call the String constructor, you are creating extra objects. rowData[temp][0] = eElement.getElementsByTagName("NAME").item(0).getTextContent(); will work just as fine
  • Don't hard code numbers like 15 and 6 in your array creation code. 15 should be the number of XML elements, which you can get with nList.getLength(). 6 is the length of the columnNames array.
  • Take a look at the Google Java Style Guide.

Upvotes: 1

Related Questions