无法弄清楚为什么这个算法正在更新arraylist中的所有字段

时间:2014-05-15 19:57:58

标签: java swing algorithm jtable cellrenderer

我有一个JTable单元格,显示一个复选框和JLabel,它跟踪检查该复选框的时间。问题是,当我添加多行并检查一行时,每行开始计数,而不是仅选择一行。

似乎我的代码忽略了特定于行的if语句。我迷失了为什么。

这是表模型中的算法。在计时器事件中每秒调用一次。

public void updateTime()
    {
        Date newTime = new Date();

            for (int i = 0; i < data.startTime.size(); i++)
            {

                if (data.getVisible(i).selected == true)
                {
                    System.out.println("updateTime");
                    System.out.println(i);

                    String value = df.format(new Date(newTime.getTime() - data.rowList.get(i).startTime));
                    data.setVisible(i, value);
                }
                else if (data.getVisible(i).selected == false)
                    data.setVisible(i, new String("00:00"));

                fireTableRowsUpdated(i,i);
            }  
    }

这是数据模型中的代码

public void setVisible(int index, Object newVar)
       {
           //System.out.println(index);


           if (newVar.getClass() == Boolean.class)
           {
               if ((Boolean) newVar)
               {
                    Value newValue = new Value(new Boolean(true), visible.get(index).value);

                    rowList.remove(index);
                    Row aRow = new Row((Boolean)newVar, System.currentTimeMillis());
                    rowList.add(index, aRow);

                    visible.remove(index);
                    visible.add(index, newValue);
               }
               else if ((Boolean) newVar == false)
               {

                    rowList.remove(index);
                    Row aRow = new Row(new Boolean(false), new Long(0));
                    rowList.add(index, aRow);
                    System.out.println("VALUE OF ROWLIST");
                    for (int i = 0; i < rowList.size(); i++)
                    {
                    System.out.println(rowList.get(i).startTime);
                    }

                    Value newValue = new Value((Boolean)newVar, "");

                    visible.remove(index);
                    visible.add(index, newValue);
               }
               }
               else if (newVar.getClass() == String.class)
               {
                   Value newValue = new Value(visible.get(index).selected, newVar.toString());

                   visible.remove(index);
                   visible.add(index, newValue);
               }

这是细胞渲染器

public class CheckboxCellRenderer extends JCheckBox
    implements TableCellRenderer {

    TableCellRenderer delegate;

    public CheckboxCellRenderer(TableCellRenderer defaultRenderer){
    this.delegate = defaultRenderer;
    }

    public CheckboxCellRenderer(){

    }

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {

        SimpleDateFormat df = new SimpleDateFormat("mm:ss");

        Value v = (Value) value;
        this.setSelected(v.selected);
        if (v.selected)
        this.setText(v.value);
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        return this;
    }
}

和值类

public class Value implements Comparable<Value> {

    public Boolean selected;
    public String value;

    public Value(Boolean selected, String value) {
        this.selected = selected;
        this.value = value;
    }

    @Override
    public int compareTo(Value v) {
        return this.value.compareTo(v.value);
    }

    @Override
    public boolean equals(Object v) {
        return v instanceof Value && this.value.equals(((Value) v).value);
    }

    @Override
    public int hashCode() {
        return this.value.hashCode();
    }
}

值编辑器

public class ValueEditor extends AbstractCellEditor
    implements TableCellEditor, ItemListener {
    SimpleDateFormat df = new SimpleDateFormat("mm:ss");

    private CheckboxCellRenderer vr = new CheckboxCellRenderer();

    public ValueEditor() {
        vr.addItemListener(this);
    }

    @Override
    public Object getCellEditorValue() {
        return vr.isSelected();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int col) {
        Value v = (Value) value;
        vr.setSelected(v.selected);
        if (!v.selected)
            vr.setText("00:00");
        else
            vr.setText(v.value);
        return vr;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        this.fireEditingStopped();
        if (e.getStateChange() == ItemEvent.SELECTED) {
        } 
        else 
        {
            vr.setText("00:00");
        }
    }
}

0 个答案:

没有答案