1

此类是创建 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;
    }
}
4

2 回答 2

3
public boolean isCellEditable(int row, int col) {
    if (col < 1) {
        return true;
    } else {
        return false;
    }
}

如果我的理解是正确的,您正在尝试仅将第 0 列设置为可编辑。添加复选框的 col 索引以返回 true。

于 2012-07-17T09:12:17.720 回答
1

将您的isCellEditable(int row, int col)as 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;   
  }
} 
于 2012-07-17T09:18:57.110 回答