Results 1 to 3 of 3
  1. #1
    hddbogdan is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Smile 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;
    }

    };

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  3. #3
    hddbogdan is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the advice. I have used the DefaultTableModel as suggested and solved all the previous problems.

    Also I will follow your advice in regard to the use of "code" tags when posting code.

Similar Threads

  1. TableModel and TableCellRenderer problem
    By cpliu903 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2011, 12:32 PM
  2. Map in TableModel
    By tiro in forum AWT / Swing
    Replies: 1
    Last Post: 07-01-2009, 08:53 AM
  3. Replies: 0
    Last Post: 11-14-2008, 05:14 AM
  4. Swing problem: JTable/TableModel
    By Levish2002 in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2008, 09:53 PM
  5. Create different instance of a tablemodel
    By Bill in forum AWT / Swing
    Replies: 6
    Last Post: 03-27-2008, 04:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •