JTable Checkbox should start timer when checked

故事扮演 提交于 2019-12-24 10:07:18

问题


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 Row contains a Boolean for the checkbox and long values for the start and stop time; use System.currentTimeMillis() as needed.

  • Condition the start and stop times in an ItemListener in your CellEditor.

  • Render the difference as an elapsed time using a suitable format.

  • In the ActionListener of 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!