0

There appears to be numerous examples and tutorials where the contents of a JTable are populated when initially creating the table but I'm struggling to understand how to populate the table from an Action event.

Courtesy of source code derived from Mkyong and an example tutorial from How to use tables I'd like to populate a JTable from a JButton click action event. The JButton will call my addDataToTable() method from the TableDemo class.

Exif.java

@XmlRootElement
public class Exif {

private int imgId;
private Date imgTimesatmp;
private String imgFilename;
private Double imgLatitude;
private Double imgLongitude;

public Exif() {
}

public int getImgId() {
    return imgId;
}

@XmlAttribute
public void setImgId(int imgId) {
    this.imgId = imgId;
}

public Double getImgLatitude() {
    return imgLatitude;
}

@XmlElement
public void setImgLatitude(Double imgLatitude) {
    this.imgLatitude = imgLatitude;
}

public Double getImgLongitude() {
    return imgLongitude;
}

@XmlElement
public void setImgLongitude(Double imgLongitude) {
    this.imgLongitude = imgLongitude;
}

public Date getImgTimesatmp() {
    return imgTimesatmp;
}

@XmlElement
public void setImgTimesatmp(Date imgTimesatmp) {
    this.imgTimesatmp = imgTimesatmp;
}

public String getImgFilename() {
    return imgFilename;
}

@XmlElement
public void setImgFilename(String imgFilename) {
    this.imgFilename = imgFilename;
}
}

ExifTableModel.java

public class ExifTableModel extends AbstractTableModel {

private String[] columnNames = {"Image Id",
    "Timestamp",
    "Filename",
    "Latitude",
    "Longitude"};

private ArrayList<ArrayList<Object>> tableData = new ArrayList<ArrayList<Object>>();

@Override
public int getColumnCount() {
    return columnNames.length;
}

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

@Override
public String getColumnName(int col) {
    return columnNames[col];
}

@Override
public Object getValueAt(int row, int col) {
    if (tableData.size() > 0) {
        return tableData.get(row).get(col);
    }
    return null;
}

@Override
public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
}

@Override
public void setValueAt(Object value, int row, int col) {
    if (tableData.size() <= row) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        for (int i = 0; i < columnNames.length; i++) {
            arrayList.add("");
        }
        tableData.add(arrayList);
    }

    ArrayList<Object> object = tableData.get(row);
    object.add(col, value);
    tableData.set(row, object);
    fireTableDataChanged();
}
}

TableDemo.java

public class TableDemo extends JPanel {
private JTable table;

public TableDemo() {
    super(new GridLayout(1, 0));

    table = new JTable(new ExifTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);

    addDataToTable();
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    TableDemo newContentPane = new TableDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}

private void addDataToTable() {
    try {

        File file = new File("myDoc.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Exif.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Exif exif = (Exif) jaxbUnmarshaller.unmarshal(file);
        System.out.println(exif.toString());

        table.setValueAt(exif, 0, 0);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

}

Deepak
  • 2,287
  • 1
  • 23
  • 30
newToJava
  • 173
  • 1
  • 15

1 Answers1

0

Ok here is what you can do.

Use JAXB or SAX or whatever you desire t read the XML and map them to objects. Store these objects in a list. JAXB does that for you while in SAX this will be an additional step.

Make sure your objects have getters and setters. You can then take this data and use it to populate the table like in the examples.

Do not read the large XML files in actionPerformed. It is just a relay to let you know that button has been pressed. Instead start a thread that will read the file. Your JAXB marshalling and unmarshalling should be done in that thread.

An SO User
  • 24,612
  • 35
  • 133
  • 221