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


}

现在我只想在“已选择”列中选择一个复选框。我正在从 eBay 获取产品信息并将其显示在表格中,我的第一列包含复选框,但我想要单选按钮。如何做到这一点。

4

1 回答 1

1

用户 kleopatra 强调我的最后一个答案违反了编码标准,因此这是一个在模型本身内工作的解决方案。

由于表模型处理表中的所有数据处理,因此通过稍微修改 setValeAt() 方法以包含执行此操作的代码,很容易模仿单选按钮的行为。

考虑以下代码:

public void setValueAt(Object arg0, int arg1, int arg2) {

    if (arg2 == 0) {                 
        boolean checked = (Boolean)arg0;

        if(checked){
            //Handle if user checked a box.

            //Iterate through all rows
            for(int i = 0; i < getRowCount();i++){
                boolean isSelected = dataList.get(i).getSelected();

                if(isSelected != (i == arg1)){
                    dataList.get(i).setSelected(i == arg1);
                    fireTableCellUpdated(i, 0);
                }
            }
        }else{
            //Handle if user unchecks the box.
        }              
    }else if(!(dataList.set(arg1, arg0).equals(arg0))){
        fireTableCellUpdated(arg1, arg2);
    }
}

代码首先检查所设置的值是否是一个选择框(在第 0 列中),然后在 for 循环中将所有其他第 0 列值设置为 false(遍历行)。

于 2013-10-18T14:45:17.300 回答