此类用于创建包含6列的jtable,最后一列包含布尔复选框。我在getColumnClass中指定了它。我不能取消选中/选中这些框,它们是不可编辑的。我怀疑类MyTableModel中的方法setValueAt和getValueAt是不正确的,但无法理解它。请帮忙。以下是我向您展示的3个类:带有main方法的gui类,扩展AbstractTableModel的MyTableModel和单元格渲染器类ColorRenderer。
import java.awt.Color;
import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class table extends JFrame {
JTable table = new JTable();
MyTableModel model = new MyTableModel(getDummyData());
JScrollPane scroll;
List<Integer> highlightedCell;
public table() throws FileNotFoundException, IOException, InvalidFormatException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public Vector getDummyData() throws IOException, InvalidFormatException {
highlightedCell = new ArrayList<Integer>();
String nnnname;
Vector vector;
Vector vectorOfVector = new Vector();
excelToList e = new excelToList("input.xls");
List<Person> names = e.createWorkbook();
int rowNum = 0;
for (Person p : names) {
nnnname = p.getName();
vector = new Vector();
vector.add(nnnname);
vector.add(p.getDone());
vector.add(p.getWaiting());
vector.add(p.getORDERED());
vector.add(p.getKPR1());
vector.add(new Boolean(false));
vectorOfVector.add(vector);
rowNum++;
if ((nnnname.matches("\\A\\p{ASCII}*\\z"))) {
highlightedCell.add(rowNum - 1);
}
}
return vectorOfVector;
}
public void createTable() throws FileNotFoundException, IOException, InvalidFormatException {
table.setModel(model);
table.setFillsViewportHeight(true);
TableCellRenderer original = table.getDefaultRenderer(Object.class);
table.setDefaultRenderer(Object.class,
new ColorRenderer(highlightedCell, original));
scroll = new JScrollPane(table);
add(scroll);
setSize(1000, 1000);
}
public static void main(String args[]) throws FileNotFoundException, IOException, InvalidFormatException {
table t = new table();
t.createTable();
}
}
class MyTableModel extends AbstractTableModel {
private String[] columnName = {"NAME", "DONE", "WAITING",
"OVERALL", "PERCENTAGE 1", "PERCENTAGE 2"};
private Vector dataVector;
public MyTableModel(Vector vectorOfVector) throws IOException, InvalidFormatException {
dataVector = vectorOfVector;
}
public int getRowCount() {
return dataVector.size();
}
public int getColumnCount() {
return columnName.length;
}
Vector v;
public Object getValueAt(int i, int i1) {
v = (Vector) dataVector.elementAt(i);
return v.elementAt(i1);
}
@Override
public Class getColumnClass(int c) {
switch (c) {
case 5:
return Boolean.class;
default:
return String.class;
}
}
@Override
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return true;
} else {
return false;
}
}
@Override
public void setValueAt(Object value, int row, int col) {
Vector rowVector = (Vector) dataVector.get(row);
rowVector.set(col, value);
fireTableCellUpdated(row, col);
}
}
class ColorRenderer implements TableCellRenderer {
List<Integer> highlightedCoordinates;
private TableCellRenderer cellRenderer;
public ColorRenderer(List<Integer> list, TableCellRenderer cellRenderer) {
if (cellRenderer == this || cellRenderer == null) {
throw new IllegalArgumentException();
}
this.cellRenderer = cellRenderer;
highlightedCoordinates = list;
}
public Component getTableCellRendererComponent(JTable jtable, Object color,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c;
c = cellRenderer.getTableCellRendererComponent(jtable, color, hasFocus, hasFocus, row, row);
if (column == 0 && highlightedCoordinates.contains(row)) {
c.setBackground(Color.GREEN);
} else {
c.setBackground(Color.yellow);
}
return c;
}
}
答案 0 :(得分:2)
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return true;
} else {
return false;
}
}
如果我的理解是正确的,那么您尝试将第0个col设置为可编辑。添加复选框的col索引以返回true。
答案 1 :(得分:1)
将你的isCellEditable(int row, int col)
改为anthoon所说:
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return true;
} else if( col == 5){//column with your checkboxes.
return true;
} else {
return false;
}
}