Reputation: 4047
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;
}
}
Now I want to make only one check box select-able in the column "Selected". I am fetching product information from eBay and displaying it in table, my 1st column contains check box, But I want radio button.How can this be done.
Upvotes: 3
Views: 520
Reputation: 8405
User kleopatra highlighted that my last answer violates coding standards, thus this is a solution that works within the model itself.
Since the table model handles all data processing within the table, it is easy to mimic radio button behavior by modifying your setValeAt() method slightly to include code that does that.
Consider the following code:
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);
}
}
The code first checks if the value being set is a selection box (in column 0) then proceeds to set all other column 0 values to false within the for loop (that iterates through the rows).
Upvotes: 1