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