• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Help - JTable!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If anyone has any advice on this it would be greatly appreciated!! Spent the last couple of weeks trying to work it out! Cant populate the table properly - keeps duplicating the first record in the list.
public class AccountsTableModel extends AbstractTableModel {
//Creates a String array to hold the column names which will be displayed
//in the table
private List columnNames = new ArrayList();
private List dataList = new ArrayList();
private List rowList = new ArrayList();
private List entries;
private Entry debitEntry;
//Constructor for the AccountsTableModel Class
public AccountsTableModel (Account account){
addColumnNames();
//Gets the list of entries from the Account class
entries = account.getEntries();
//Creates an iterator to go through the list of entries
Iterator entriesIterator = entries.iterator();
while(entriesIterator.hasNext()){
Entry entry = (Entry)entriesIterator.next();
Entry creditEntry = entry.getTransaction().getCreditEntry();
Transaction transaction = entry.getTransaction();
String date = entry.getTransaction().getDate();
rowList.add(date);
String name = creditEntry.getAccount().getName();
rowList.add( name);
Integer transactionNumber = new Integer(entry.getTransaction().getTransactionNumber());
rowList.add(transactionNumber);
String nullAmount = "";
Integer amount;
if (entry.getDebit() == true){
rowList.add(nullAmount);
amount = new Integer(entry.getAmount());
rowList.add(amount);
}
else{
amount = new Integer (entry.getAmount());
rowList.add (amount);
rowList.add (nullAmount);
}

dataList.add(rowList);
System.out.println ("DATALIST" + dataList);
/*Iterator dataIterator = dataList.iterator();
while(dataIterator.hasNext()){
Object object = (Object)dataIterator.next();
System.out.println(object);
System.out.println(dataList.size());
}*/
}// end while loop
}
public void addColumnNames() {
columnNames.add ("Date");
columnNames.add ("Transaction");
columnNames.add ("Number");
columnNames.add ("Cr Amount");
columnNames.add ("Dr Amount");
}

public int getColumnCount() {
return columnNames.size();
}
public int getRowCount() {
return dataList.size();
}
public String getColumnName(int col) {
return (String)columnNames.get(col);
}

public Object getValueAt(int row, int col) {
//System.out.println("Col" + col);
//System.out.println("Row" + row);
List rowList = (List) (dataList.get(row));
System.out.println("ROWLIST" + rowList);
//System.out.println(rowList.size());
//System.out.println(rowList.get(col));
rowList.get(col);
//System.out.println("DATALIST:" + dataList);
return rowList.get(col);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*Don't implement this method unless tables editable.*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/* Don't implement this method unless table's data can change.*/
/* public void setValueAt(Object value, int row, int col) {
dataList.get(col) = value;
fireTableCellUpdated(row, col);
}*/
}
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're saving the rowList at the class level, so it never gets reset each time you go through the loop in the AccountsTableModel constructor. You should reset the rowList to be a new ArrayList each time through the loop. Otherwise every ArrayList added to the dataList is actually a reference to the same ArrayList, and the data is the same for every row.
 
Everybody's invited. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic