Results 1 to 3 of 3
  1. #1
    Norther is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default Refreshing Jtable once again...

    Well, I know that this subject is pretty common, yet whole day of searching have not given me a solution. Here is the problem:

    I've got a Jtable that represents a table in DB. Here is the TableModel:
    Java Code:
    public class ShopTableModel extends AbstractTableModel{    
        
        private String[] columnNames = {"ID", "Model", "Brand", "Type", "Description",
                                        "Price", "Amount", "Notes"};    
        private List<Item> tableData;
        private ItemsDAO itemsDAO;
    
        public ShopTableModel(ItemsDAO itemsDAO) {
            this.itemsDAO = itemsDAO;
            setTableData(itemsDAO.getItems());
        }
               
        public void setTableData(List<Item> tableData) {
            this.tableData = tableData;
        }
    
        public int getRowCount() {
            return tableData.size();
        }
    
        public int getColumnCount() {
            return columnNames.length;
        }
    
        public Object getValueAt(int row, int column) {
            Item item = tableData.get(row);
    
            if (column == 0)
                return item.getId();
            else if (column == 1)
                return item.getModel();
            else if (column == 2)
                return item.getBrand();
            else if (column == 3)
                return item.getType();
            else if (column == 4)
                return item.getDescription();
            else if (column == 5)
                return item.getPrice();
            else if (column == 6)
                return item.getAmount();
            else if (column == 7)
                return item.getNotes();
            else
                return new String("Data Not Found");
        }
    
    
        @Override
        public String getColumnName(int arg0) {
            return columnNames[arg0];
        }
    
        public Item getItem(int index) {
            return tableData.get(index);
        }
    
        public void refresh() {
            setTableData(itemsDAO.getItems());
            fireTableDataChanged();
        }
    }
    Now, when I remove something from the DB everything works fine, the row is removed & everyone is happy. Here is the code:
    Java Code:
    public void deleteItem(int id) {
            itemsDAO.deleteItem(id);
            tableModel.refresh();
    }
    But when I add an item into the DB nothing happens. The table stays the same. Here is the code for adding an item:
    Java Code:
    public void insertItem(String model,
                               String brand, 
                               String type, 
                               String description,
                               String price,
                               String variations,
                               String amount,
                               String notes) {
            
            Item item = new Item();
            item.setModel(model);
            item.setBrand(brand);
            item.setType(type);
            item.setDescription(description);
            item.setPrice(new Float(price));
            item.setVariations(variations);
            item.setAmount(new Integer(amount));
            item.setNotes(notes);
            itemsDAO.insertItem(item);
            tableModel.refresh();
        }
    When I restart the app the new item does appear. Moreover, when I delete an item, the new item appears also.

    Thank you for your attention.
    Last edited by Norther; 06-29-2010 at 07:53 PM.

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Your delete and insert methods seem to be refreshing different tableModels -
    Deleting refreshes "tableModel" and insert is refreshing "shopTableModel". Are they both the same object?

    Berkeleybross

  3. #3
    Norther is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Yes, I have spotted that and renamed everything into tableModel. I simply forgot to update the code in my post. The problem still unsoloved.
    Last edited by Norther; 06-29-2010 at 07:52 PM.

Similar Threads

  1. japplet - refreshing windows
    By Symbiot in forum New To Java
    Replies: 6
    Last Post: 05-29-2010, 06:12 PM
  2. Refreshing JList - need help
    By Unsub in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 06:31 AM
  3. Refreshing Text Field Value
    By nehakuls in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-13-2009, 11:05 AM
  4. JTableHeader not refreshing
    By aznboarder in forum AWT / Swing
    Replies: 4
    Last Post: 04-11-2009, 04:31 AM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 07:09 AM

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
  •