问题
I am looking to have a column in a JTable that counts the time that boolean column in the table is 'true' for. It is a timer for how the long the checkbox is checked. I'm having trouble wrapping my head around all of the mechanics for the algorithm.
ActionListener actListner = new ActionListener() 
    {
        public void actionPerformed(ActionEvent event) 
        {
            System.out.println("k");
            aTable.updateTime();
        }
    };
    Timer timer = new Timer(1000, actListner);
    timer.start();
    TableModelListener tableListener = new TableModelListener()
    {
        public void tableChanged(TableModelEvent e) 
        {
         int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel)e.getSource();
            String columnName = model.getColumnName(column);
            Object data = model.getValueAt(row, column);
            if (aTable.data.getVisible(row))
            {
                //aTable.data.setTimeVisible(row, date math);
            }
        }
    };
The "updateTime()" function is used for tracking the amount of time a row exists once it is added, and I was thinking about also using that function (since it is called every time by the timer ticking) to set the values for the checkbox checked tracker, but I'm not sure.
public void updateTime()
    {
        //data.updateTime();
        Date newTime = new Date();
            Integer time = null;
            System.out.println("updateTime");
            for (int i = 0; i < data.startTime.size(); i++)
            {
                time = Integer.parseInt(data.twoMin.format(new Date(newTime.getTime() - data.startTime.get(i).getTime())));
                //Date waiting = new Date(time);
                if (time >= 10)
                {
                    data.setTimeWaiting(i, data.twoMin.format(new Date(newTime.getTime() - data.startTime.get(i).getTime()))); 
                    System.out.println("2");
                }
                else if (time < 10)
                {
                    data.setTimeWaiting(i, (data.oneMin.format(new Date(newTime.getTime() - data.startTime.get(i).getTime()))));
                    System.out.println("1");
                }
                fireTableRowsUpdated(i,i);
            }
    }
Would I need to pass an array with the locations of the start times? This is confusing me.
回答1:
Here is an outline of one way to do this:
- In your implementation of TableModel, manage a - List<Row>, where each- Rowcontains a- Booleanfor the checkbox and- longvalues for the start and stop time; use- System.currentTimeMillis()as needed.
- Condition the start and stop times in an - ItemListenerin your CellEditor.
- Render the difference as an elapsed time using a suitable format. 
- In the - ActionListenerof a javax.swing.Timer, periodically invoke- setValueAt()in the model for each active- Row; the listening table should update automatically.
A related example is shown here.
来源:https://stackoverflow.com/questions/23602711/jtable-checkbox-should-start-timer-when-checked