Hi I'm working on a project and I need to use a JTable. I sub classed AbstractTableModel and then put the data into the table. That works correctly. I create a new JTable object passing in my AbstractTableModel. Next, I make it a AutoScrollPane. When I add the AutoScrollPane to the GUI the table shows up but without any data. I'll provide short code snippets of what I think is necessary since my code is a bit of a mess right now.
//JPanel tableToShow = new JPanel();
TablePane table = new TablePane(gp.txt);
table.addData();
JTable scrollTable = new JTable(table);
scrollTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
scrollTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(scrollTable);
//tableToShow.add(scrollPane);
//text.setFont(new Font("Courier", Font.BOLD, 20));
//text.setPreferredSize(new Dimension(1200,150));
setLayout(new BorderLayout(10,10));
add(jplRadio, BorderLayout.WEST);
add(plotPanel, BorderLayout.EAST);
add(label, BorderLayout.NORTH);
add(scrollPane, BorderLayout.SOUTH);
and
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TablePane extends AbstractTableModel {
public String[] ColumnNames = {"Date", "Level", "Duration", "Weight"};
public String[][] data = null;
private TextImporter txt = null;
public TablePane(TextImporter txt) {
this.txt = txt;
}
public void addData() {
ArrayList<?> currentList = null;
data = new String[4][txt.getSession().size()];
for(int i = 0; i < ColumnNames.length; i++) {
if(i == 0) {
currentList = txt.getSession();
} else if(i == 1) {
currentList = txt.getAmplitude();
} else if(i==2) {
currentList = txt.getDuration();
} else if(i==3) {
currentList = txt.getLegPressure();
}
for(int j = 0; j < txt.getSession().size(); j++) {
data[i][j] = (String) currentList.get(j);
}
}
//System.out.print(data);
}
// public void displayTable() {
//
// }
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return ColumnNames.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
}
I can provide more code if needed