简体   繁体   中英

putting data into array and then JTable

Referring to an earlier question i asked

lining up data in console output java

I wish to put my output in an array so that i can then further put this into a JTable

The extract for my code so far is, i am currently printing out the output to the console.

String assd = null;
String eventy = null;
String assdFT = null;


for (int i = 0; i < list.getLength(); i++) {
Element element = (Element)list.item(i);
String nodeName = element.getNodeName();

switch (nodeName) {
case "assd":
assd = element.getChildNodes().item(0).getNodeValue();


break;
case "eventy":
eventy = element.getChildNodes().item(0).getNodeValue(); 

break;
case "assdFT":

assdFT = element.getChildNodes().item(0).getNodeValue(); 
break;

System.out.printf("%-30s  %-20s  %s%n", assd,  eventy,assdFT);




Object[][] data = {{assd,  eventy,assdFT}};//this only appears to put the elements in row 1, since System.out.println(data[1][0]) causes an out of array exception but System.out.println(data[0][0]) prints out all the elements of assd

To put data directly into a JTable pass an instance of your custom AbstractTableModel to the JTable constructor. Within TableModel, you can define what data is displayed and how it is accessed.

It would probably look something like this:

public class HeaderTableModel extends AbstractTableModel {

/**
 * 
 */
private static final long serialVersionUID = 8974549762036798969L;

private Object[][] myData;

public HeaderTableModel(final Object[][] theRows) {
    myHeaderRows = theRows;
}


/*
 * (non-Javadoc)
 * 
 * @see javax.swing.table.TableModel#getColumnCount()
 */
@Override
public int getColumnCount() {
    return LocalStorage.getNumColumns();
}

/*
 * (non-Javadoc)
 * 
 * @see javax.swing.table.TableModel#getRowCount()
 */
@Override
public int getRowCount() {
    return LocalStorage.getNumRows();
}

/*
 * (non-Javadoc)
 * 
 * @see javax.swing.table.TableModel#getValueAt(int, int)
 */
@Override
public Object getValueAt(final int theRow, final int theColumn) {
    return myHeaderRows[theRow][theColumn];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM