简体   繁体   English

复选框在JTable中无法正常工作/不可编辑

[英]checkbox is not working/uneditable in JTable

This class is to create jtable with 6 columns, the last one containing boolean checkboxes. 这个类是用6列创建jtable,最后一个包含布尔复选框。 I specified it in getColumnClass. 我在getColumnClass中指定了它。 I can't uncheck/check these boxes, they are uneditable. 我不能取消选中/选中这些框,它们是不可编辑的。 I'm suspecting that methods setValueAt and getValueAt in class MyTableModel aren't correct, but can't get my head around it. 我怀疑类MyTableModel中的方法setValueAt和getValueAt是不正确的,但无法理解它。 Please help. 请帮忙。 Here are 3 classes I'm showing you: the gui class with main method, the MyTableModel which extends AbstractTableModel and the cell renderer class ColorRenderer. 以下是我向您展示的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;
    }
}
public boolean isCellEditable(int row, int col) {
    if (col < 1) {
        return true;
    } else {
        return false;
    }
}

If my understanding is correct you are trying to set only the 0th col as editable. 如果我的理解是正确的,那么您尝试将第0个col设置为可编辑。 Add the col index of checkbox to return as true. 添加复选框的col索引以返回true。

change your isCellEditable(int row, int col) as anthoon said to the following: 改变你的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;   
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM