NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Adding columns and rows to a jTable model

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
njhutc



Joined: 06 Dec 2009
Posts: 11

PostPosted: Sun Dec 12, 2010 2:10 pm    Post subject: Adding columns and rows to a jTable model Reply with quote

I have a swing GUI with a jTable based on a model derived from AbstractTableModel. The data is held in an arraylist of arraylist of strings. The number of rows and columns can be changed dynamically by the user.

The problem is shown when resizing the table from from 2*2 to 4*4. The resizing of the header is OK as is the resizing of the number of rows. That is, the rows being re-sized first, setting a breakpoint shows 4rows * 2cols OK. When resizing the columns, the first two rows are re-sized OK so, in terms of row lengths I get 4, 4, 2, 2. Resizing the next row seems to re-size both rows 3 and 4. Can anyone advise on why this is and a way around it. Thanks.

The relevant code from the model is shown below and the code that causes the problem is in bold:

public void addColumns(int count) {
int i,j;
for (i=0;i<count;i++) {
tableColumnNames.add("");
}
for (i=0;i<tableData.size();i++) {
for (j=0;j<count;j++) {
tableData.get(i).add("");

}
}
super.fireTableStructureChanged();
}

public void deleteColumn(int column_no) {

int i;
tableColumnNames.remove(column_no);
for (i=0;i<tableData.size();i++) {
tableData.get(i).remove(column_no);
}
super.fireTableStructureChanged();
}

public void addRow(int count) {
int i;
ArrayList<String> tempRow;
tempRow = new ArrayList<String>();
for (i=0;i<tableData.get(0).size();i++) {
tempRow.add("");
}
for (i=0;i<count;i++) {
tableData.add(tempRow);
}
fireTableStructureChanged();
}

public void resizeTable(int newNoOfCols, int newNoOfRows) {
int origRows = getRowCount();
int origCols = getColumnCount();
int NoOfrowsDiff = newNoOfRows - origRows;
int NoOfcolsDiff = newNoOfCols - origCols;

System.out.println("Resizing table Rows = " + origRows + " to" + newNoOfRows + " Diff = " + NoOfrowsDiff);
System.out.println("Resizing table Cols = " + origCols + " to" + newNoOfCols + " Diff = " + NoOfcolsDiff);
if (NoOfrowsDiff > 0) {
System.out.println("Adding " + NoOfrowsDiff + " rows");
addRow(NoOfrowsDiff);
} else if (NoOfrowsDiff <= 0) {
System.out.println("Deleting " + NoOfrowsDiff + " rows");
for (int i=NoOfrowsDiff; i<0; i++)
deleteRow(getRowCount()-1);
}
if (NoOfcolsDiff > 0) {
System.out.println("Adding " + NoOfcolsDiff + " columns");
addColumns(NoOfcolsDiff);
} else if (NoOfcolsDiff <= 0) {
System.out.println("Deleting " + NoOfcolsDiff + " columns");
for (int i=NoOfcolsDiff; i<0; i++)
deleteColumn(getColumnCount()-1);
}
super.fireTableStructureChanged();
}
}
Back to top
njhutc



Joined: 06 Dec 2009
Posts: 11

PostPosted: Mon Dec 13, 2010 8:13 pm    Post subject: Adding columns and rows to jTable Reply with quote

Discovered the answer myself. The two added rows must be unique instances of ArrayList<String>; so the call to 'new ArrayList<String>()' and associated code that fills the new ArrayLists with 'spaces' must be inside the 'count' loop in the addRow() procedure. Hence:

public void addRow(int count) {
int i,j;
ArrayList<String> tempRow;
for (i=0;i<count;i++) {
tempRow = new ArrayList<String>();
for (j=0;j<tableData.get(0).size();j++) {
tempRow.add("");
}

tableData.add(tempRow);
}
fireTableStructureChanged();
}

I've fallen into this trap before so should have realised. Hope this is a useful tip for others.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo