JTable Checkbox应该在选中时启动计时器 [英] JTable Checkbox should start timer when checked

查看:59
本文介绍了JTable Checkbox应该在选中时启动计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个JTable中的列,该列计算表中布尔列为"true"的时间.它是一个复选框,用于选中复选框的时间.我在解决算法的所有机制时遇到了麻烦.

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);
            }
        }
    };

"updateTime()"函数用于跟踪添加一行后存在的时间,我在考虑是否也使用该函数(因为每次被计时器打钩都会调用它)来设置复选框选中的跟踪器的值,但我不确定.

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.

推荐答案

以下是执行此操作的一种方法的概述:

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.

CellEditor .

渲染区别在于使用合适的格式.

ActionListener中" rel ="nofollow noreferrer"> javax.swing.Timer ,定期在模型中为每个活动的Row调用setValueAt();监听表应该会自动更新.

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.

这篇关于JTable Checkbox应该在选中时启动计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆