2011-06-23 143 views
0

我已經從文件填充了一個JTable。將數據填充到JTable後,我可以編輯 數據,但我對數據所做的更改未應用。我想要應用更改,稍後我想將更新的數據保存到文件中。如何更新jtable模型

下面是來填充該表

ArrayList arrayList1 = new ArrayList(); 
ArrayList arrayList = new ArrayList(); 
reader = new BufferedReader(new InputStreamReader(new FileInputStream(mainpath))); 
Map<String, Object[]> result = new LinkedHashMap<String, Object[]>(); 
int count = 0; 
int i = 0; 
boolean b = false; 
int r1 = rcount - 1; 
while(reader.ready()) { 
    String line = reader.readLine(); 
    if(count == 0) 
     count++; 
    else { 
     String[] values = line.split(","); 
     String vit = values[0]; 
     String amt = values[1]; 
     String rda = values[2]; 
     String brand = values[3]; 
     String product = values[4]; 
     int rcount1 = Integer.parseInt(values[5]); 
     if(rcount1 == r1) { 
      if(result.containsKey(vit)) { 
       result.clear(); 
       result.put(vit, new Object[] { b, vit, amt, rda }); 
       arrayList = new ArrayList(result.values()); 
       for(Object v : arrayList) { 
        arrayList1.add(v); 
       } 
      } else { 
       if(arrayList.size() > 0) { 
        arrayList.clear(); 
        result.clear(); 
       } 
       result.put(vit, new Object[] { b, vit, amt, rda }); 
       arrayList = new ArrayList(result.values()); 
       for(Object v : arrayList) { 
        arrayList1.add(v); 
       } 
      } 
      combo.setSelectedItem(brand); 
      combo1.setSelectedItem(product); 
     } 
    } 
} 
jTable3.setModel(new AnimalTableModel(arrayList1)); 

第一列是一個複選框,第二欄的組合框的代碼,第三欄接受浮筒,第四接受整數。

任何建議將是真正的幫助....

在此先感謝

+0

請限制你的代碼只是什麼是絕對必要的,瞭解你的問題。並確保正確格式化以及 – vemv

+0

對不起..下一次肯定會跟着它.. –

+2

這次你可以跟着它!你可以使用「編輯」鏈接... –

回答

2

添加項目...

jTable3.getModel().getWhateverFunctionGetsYourArray().add(item); 
+0

嗨感謝您的答覆。我的問題是,從文件填充後,我對jtable模型所做的更改未應用。我想先更改少量值,然後嘗試將更正後的值保存到文件中。 –

2
jTable3.setModel(new AnimalTableModel(arrayList1)); 

似乎是錯誤的。此操作爲您的JTable設置了一個新模型,您需要的是更新現有模型。如果我想要一個ResultSet分配給它例如,我想這個代碼:

tablemodel_foo = resultset_bar; 

你並不需要,因爲它與它的分配模式連接到訪問您的JTable。 Rocky建議,除非你使用GetModel方法。

-2

您可以創建自己的JTable模型,設置新值,然後獲取集合並對其進行迭代。 這是一些模型中使用的Java

package com.java7; 

import java.util.ArrayList; 
import java.util.List; 

import javax.swing.table.DefaultTableModel; 

public class CustomerTableModel extends DefaultTableModel { 

    private static final long serialVersionUID = 281838548127943945L; 
    private List<Customer> customers; 

    public CustomerTableModel(int count) { 
     createColumns(); 
     this.customers = new ArrayList<Customer>(count); 
     Customer customer; 
     for (int i = 0; i < count; i++) { 
      customer = new Customer(); 
      customer.setId(i); 
      customer.setName("Customer " + i); 
      customer.setPhone("Phone of Customer " + i); 
      customer.setEmail("Email of Customer " + i); 
      insertRow(i, customer); 
     } 
    } 

    public void insertRow(int row, Customer customer) { 
     this.insertRow(row, customer.toArray()); 
     this.customers.add(row, customer); 
    } 

    public List<Customer> getCustomers() { 
     return this.customers; 
    } 

    private void createColumns() { 
     this.setColumnIdentifiers(new Object[] { "id", "name", "phone", "email" }); 
    } 

    public String getColumnName(int col) { 
     return super.getColumnName(col); 
    } 

    public Object getValueAt(int row, int col) { 
     Customer customer = customers.get(row); 
     String columnIdentifer = super.getColumnName(col); 
     switch (columnIdentifer) { 
     case "id": 
      return customer.getId(); 
     case "name": 
      return customer.getName(); 
     case "phone": 
      return customer.getPhone(); 
     case "email": 
      return customer.getEmail(); 
     default: 
      break; 
     } 
     return null; 
    } 

    public void setValueAt(Object value, int row, int col) { 
     Customer customer = customers.get(row); 
     String columnIdentifer = super.getColumnName(col); 
     switch (columnIdentifer) { 
     case "name": 
      customer.setName((String) value); 
      break; 
     case "phone": 
      customer.setPhone((String) value); 
      break; 
     case "email": 
      customer.setEmail((String) value); 
     default: 
      break; 
     } 
     fireTableDataChanged(); 
    } 

    @SuppressWarnings({ "rawtypes", "unchecked" }) 
    public Class getColumnClass(int c) { 
     String columnIdentifer = super.getColumnName(c); 
     switch (columnIdentifer) { 
     case "id": 
      return Integer.class; 
     case "name": 
      return String.class; 
     case "phone": 
      return String.class; 
     case "email": 
      return String.class; 
     default: 
      break; 
     } 
     return null; 
    } 

    public int getColumnCount() { 
     return 4; 
    } 

    public boolean isCellEditable(int row, int col) { 
     if (super.getColumnName(col).equals("id")) 
      return false; 
     else 
      return true; 
    } 
} 

這是Customer類:

package com.java7; 

public class Customer { 

    private int id; 
    private String name; 
    private String phone; 
    private String email; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getPhone() { 
     return phone; 
    } 
    public void setPhone(String phone) { 
     this.phone = phone; 
    } 
    public String getEmail() { 
     return email; 
    } 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    public Object[] toArray() { 
     Object[] data = new Object[4]; 
     data[0] = this.id; 
     data[1] = this.name; 
     data[2] = this.phone; 
     data[3] = this.email; 
     return data; 
    } 
} 
+2

-1對於非常奇怪的模型實現:a)dataChanged是在更新時觸發的錯誤事件b)超級數據向量的狀態未保持同步c)b的原因是用客戶數據填充super以及之後忘了填寫......如果你有一個bean支持的模型,沒有理由擴展DefaultTableModel。而是擴展AbstractTableModel。 – kleopatra