package com.tweeteye.gui.model;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import com.tweeteye.entity.ImageRecord;
import com.tweeteye.entity.enumiration.SearchTypeEnum;
import com.tweeteye.gui.MainWindow;
import javax.swing.ImageIcon;
public class ImageTableModel extends AbstractTableModel
{
private static final long serialVersionUID = 1669175969068584634L;
protected SearchTypeEnum type;
public List<ImageRecord> dataList = new ArrayList<ImageRecord>();
@SuppressWarnings("rawtypes")
private Class[] columnTypes = { java.lang.Boolean.class,javax.swing.ImageIcon.class,
javax.swing.ImageIcon.class, java.lang.Object.class };
private String[] columnNames = {"Select","Logo","Image","Title"};
public List<ImageRecord> getData() {
return dataList;
}
public void setData(List<ImageRecord> dataList) {
this.dataList = dataList;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Class getColumnClass(int column) {
return columnTypes[column];
}
public String getColumnName(int col) {
return columnNames[col].toString();
}
public void setValueAt(Object arg0, int arg1, int arg2) {
ImageRecord imageRecord=dataList.get(arg1);
if (arg2 == 0) {
imageRecord.setSelected((Boolean) arg0);
}
fireTableCellUpdated(arg1, arg2);
}
public Object getValueAt(int arg0, int arg1) {
if (arg1 == 0)
return dataList.get(arg0).getSelected();
if (arg1 == 1)
return dataList.get(arg0).getImage();
else
return dataList.get(arg0).getTitle();
}
@Override
public boolean isCellEditable(int row, int column) {
if (column == 0)
return true;
else
return false;
}
public int getColumnCount() {
return columnTypes.length;
}
public int getRowCount() {
return dataList.size();
}
public SearchTypeEnum getType() {
return type;
}
public void setType(SearchTypeEnum type) {
this.type = type;
}
}
現在我只想在「Selected」列中選中一個複選框。我從eBay獲取產品信息並將其顯示在表格中,我的第一列包含複選框,但我希望單選按鈕。如何完成此操作。如何用布爾數據類型模仿JTable中的單選按鈕行爲
實現該模型做其他行的更新 - 這是一個固有模式的責任,以保持它的數據一致 – kleopatra
用戶不要指望複選框,以這樣的行爲。不應該使用單選按鈕,如[this](http://stackoverflow.com/a/11259671/261156)? –
參見@Guillaume波萊的單選按鈕[示例](http://stackoverflow.com/questions/11259579/adding-jradiobutton-into-jtable)或該複選框[示例](http://stackoverflow.com/a/7920159/230513)。 – trashgod