JTable基于另一列通过用户输入验证单元格值

时间:2016-05-11 10:30:41

标签: java swing jtable

你好我有一个简单的JTable,有六个重要的列有表格模型中描述的标题:

private final String[] columNames = new String[]{"Id","TicketID","PolicyNo","Remarks 1","Hourly Rate","Part Time","Validate 1"};

每次用户根据Column(2)中的最后3位数值输入必须相等的3位数时,我需要验证。请看下面的图片 Run File

这是我的代码

private final DefaultTableCellRenderer renderer = new TabelaCellRenderer();

/**
 * Creates new form EditableTableExample
 */
public EditableTableExample() {
    initComponents();
    setLocationRelativeTo(null);
    setTitle("Editable Table Example");
    initTable();
}

private void initTable(){
    DocsDetail row1 = new DocsDetail(1, "1216266","19889086","", 100.0, true,0);
    DocsDetail row2 = new DocsDetail(1, "1216266","33350761","", 100.0, false,0);
    DocsDetail row3 = new DocsDetail(1, "1216266","22780770","", 100.0, true,0);
    DocsDetail row4 = new DocsDetail(1, "1216266","57611607","", 100.0, false,0);
    DocsDetail row5 = new DocsDetail(1, "1216266","72914755","", 100.0, true,0);

    //build the list
    List<DocsDetail> employeeList = new ArrayList<>();
    employeeList.add(row1);
    employeeList.add(row2);
    employeeList.add(row3);
    employeeList.add(row4);
    employeeList.add(row5);

    //create the model
    DocsDetailTabelModel model = new DocsDetailTabelModel(employeeList);
    //create the table
    tableEx.setModel(model);
    tableEx.setRowSorter(new TableRowSorter(model));
    //tableEx.setDefaultRenderer(Object.class, renderer);
    //tableEx.getColumnModel().getColumn(6).setCellEditor(new IntegerEditor(0, 1000));
}

private class TabelaCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(jtable, value, isSelected, hasFocus, row, column);
        String nopolis = "";
        int lastDigits = 0;
        int modelRow = jtable.convertRowIndexToView(row);
        jtable.convertRowIndexToView(row);
        nopolis = jtable.getModel().getValueAt(modelRow, 2).toString();
        lastDigits = Integer.parseInt(nopolis.substring(nopolis.length() - 3));
        if (column == 6) {
            if(!value.equals(0)){
                System.out.println("lastDigits: "+lastDigits);
                if (value.equals(lastDigits)) {
                   jtable.setCellSelectionEnabled(true);
                    setFont(getFont().deriveFont(Font.BOLD));
                } else {
                   jtable.setCellSelectionEnabled(false);
                   setFont(getFont().deriveFont(Font.PLAIN));
                }
            }
        }else {
            setForeground(Color.black);
            setFont(getFont().deriveFont(Font.PLAIN));
        }
        return this;
    }
}

看起来很简单(或者不简单)。除了我不知道我应该寻找什么事件。像“细胞价值变化”之类的东西?或Cell Validator或Cell Renderer,我已经尝试过这些方法,但我没有找到解决方案;(无论如何,在我想出一些奇怪的noobish解决方案之前,我想我可能会问这里有什么正确的方法:)< / p>

1 个答案:

答案 0 :(得分:0)

您需要创建一个自定义编辑器来编辑输入的数据。

查看Table Five Character Editor以获取一个示例,以帮助您入门。

那里的示例只是确保您在单元格中输入5个字符。您显然会更改编辑以验证输入的3个字符。