如何使用布尔数据类型模拟JTable中的单选按钮行为

时间:2013-10-18 13:32:52

标签: java swing jtable

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获取产品信息并在表格中显示,我的第一列包含复选框,但我想要单选按钮。如何完成。

1 个答案:

答案 0 :(得分: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(迭代遍历行)。