Results 1 to 3 of 3
Thread: TableModel initialize/delete
- 04-11-2011, 06:29 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
TableModel initialize/delete
Hello,
I am trying to create an application for managing a warehouse which contains products stored in a TreeSet. For displaying the products I have created an AbstractTableModel which I fill with the elementes of the tree.
The problem occurs when I delete a product form the tree. Say I have n products in the data model, after deletion I have n-1 but when I rewrite the data model it only rewrites the first n-1 rows, leaving the nth product unchanged.
I need a way to reinitialize or delete the model so that I can build it from scratch each time I display the products table.
public void setTable() {
int i = 0;
for (Product pr : set) {
model.setValueAt(pr.getName(), i, 0);
model.setValueAt(pr.getQuantity(), i, 1);
if (pr.getQuantity() >= underStock && pr.getQuantity() <= overStock)
model.setValueAt("In Stock", i, 2);
else if (pr.getQuantity() < underStock)
model.setValueAt("Under-Stock", i, 2);
else
model.setValueAt("Over-Stock", i, 2);
i++;
}
}
/** The model. */
public TableModel model = new AbstractTableModel() {
private static final long serialVersionUID = 1L;
/** The row data. */
Object rowData[][] = { { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" }, { "", "", "" }, { "", "", "" },
{ "", "", "" }, { "", "", "" } };
/** The column names. */
Object columnNames[] = { "Product", "Quantity", "Status" };
public String getColumnName(int column) {
return columnNames[column].toString();
}
public int getRowCount() {
return rowData.length;
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
}
};
- 04-11-2011, 04:21 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Your implementation of the TableModel is wrong so you are getting all these problems. There is no need for you to create a custom TableModel.
Use the DefaultTableModel. Then you can use the addRow(...) method to add your items to the model. You can also use the removeRow(...) to remove items. The model will then invoke the proper fireXXX methods which will tell the table to repaint itself.
Also, use the "Code" tags when posting code, not the "Quote" tags.
- 04-11-2011, 06:27 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
TableModel and TableCellRenderer problem
By cpliu903 in forum AWT / SwingReplies: 1Last Post: 03-06-2011, 11:32 AM -
Map in TableModel
By tiro in forum AWT / SwingReplies: 1Last Post: 07-01-2009, 07:53 AM -
Set focus on particular cell in a JTable when using TableModel
By sridhar_negi in forum AWT / SwingReplies: 0Last Post: 11-14-2008, 04:14 AM -
Swing problem: JTable/TableModel
By Levish2002 in forum AWT / SwingReplies: 2Last Post: 08-24-2008, 08:53 PM -
Create different instance of a tablemodel
By Bill in forum AWT / SwingReplies: 6Last Post: 03-27-2008, 03:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks