表模型更新+ fireTableDataChanged()中断表选择 [英] Table model update + fireTableDataChanged() disrupts table selection

查看:62
本文介绍了表模型更新+ fireTableDataChanged()中断表选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,它跟踪一个人等待在餐厅就座的时间.我的问题是,每当计时器滴答"一下时,一行中的选择就会被删除.换句话说,如果您单击一行,它将以蓝色背景和轮廓突出显示,但是当计时器计时时,蓝色消失了.

I have a JTable that tracks the amount of time a person has been waiting to be seated at a restaurant. My problem is that every second, when the timer 'ticks', the selection that is on a row is removed. In other words, if you click a row it becomes highlighted with a blue background and outline, but then when the timer ticks the blue goes away.

ActionListener actListner = new ActionListener() 
    {
        public void actionPerformed(ActionEvent event) 
        {
             aTable.updateTime();
        }
    };

    Timer timer = new Timer(1000, actListner);
    timer.start();

这是在主班上

public void updateTime()
    {
        data.updateTime();
        fireTableDataChanged();
    }

这是在表格模型中

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

            for (int i = 0; i < startTime.size(); i++)
            {                  
                this.setTimeWaiting(i, hoursMin.format(new Date(newTime.getTime() - startTime.get(i).getTime())));  
            }
       }

这在数据模型中.

推荐答案

fireTableDataChanged()告诉表数据可能以任何方式发生了变化,实际上,您是在整个数据集都发生完全变化时才使用它(例如, ve将所有先前的内容替换为新信息).该表将重新从头开始绘制,也可能会清除选择.

fireTableDataChanged() tells the table that the data may have changed in any way, and essentially you use it when the entire data set has completely changed (e.g. you've replaced all of the previous contents with new information). The table will redraw itself from scratch and may also clear the selection.

请使用更保守的 fireTableCellUpdated(int,int) 并指定由于时间变化(可能在等待时间"列中的所有内容)可能已修改的每个单元格.

Instead, use the more conservative fireTableCellUpdated(int,int) and specify each cell that may have been modified due to the time change (presumably, everything in the "wait time" column).

您还可以使用 fireTableRowsUpdated(int,int) 并指定一次调用中已更新的行的整个范围,但是通常最好坚持保守的做法,以最大程度地减少不必要的重绘.

You could also use fireTableRowsUpdated(int,int) and specify the entire range of rows that have been updated in one call, but generally it's better to stick to the conservative side to minimize unnecessary redraws.

这篇关于表模型更新+ fireTableDataChanged()中断表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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