我已从文件中填充了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 :(得分:2)
添加项目......
jTable3.getModel().getWhateverFunctionGetsYourArray().add(item);
答案 1 :(得分:2)
jTable3.setModel(new AnimalTableModel(arrayList1));
似乎是错的。此操作为您的JTable设置了一个新模型,您想要的是更新现有模型。例如,如果我想为其分配ResultSet
,我会对此进行编码:
tablemodel_foo = resultset_bar;
您无需访问JTable,因为它与指定的模型相关联。除非你按照Rocky的建议使用GetModel
方法。
答案 2 :(得分:-2)
您可以创建自己的JTable模型,设置新值然后获取集合并迭代它。 这是使用 Java 7
的一些模型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;
}
}