摆动 - 组件的短时高亮显示 [英] swing - short-time highlight of a component

查看:34
本文介绍了摆动 - 组件的短时高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JTable,用户可以在其中选择一行.如果发生这种情况,我想短时间突出显示"页面的另一部分,以表明这是在用户交互后更改的页面部分.

I have a JTable, where a user can select a single row. If that happens, i want to "highlight" another part of the page for a short time to indicate that this is the part of the page that changed after the user interaction.

所以我的问题是:实现这一目标的最佳方法是什么?目前,我是通过设置该面板的背景颜色并启动一个 SwingWorker 来实现的,该 SwingWorker 在短暂延迟后将颜色设置回原位.它按预期工作,但使用这样的 SwingWorker 是个好主意吗?这种方法有什么缺点吗?你会如何解决这个问题?

So my question is: What's the best way to achieve this? At the moment i did it by setting the background color of that panel and starting a SwingWorker which sets the Color back after a short delay. It works as intended, but is it a good idea to use a SwingWorker like that? Are there any drawbacks to that approach? How would you solve this?

提前致谢.

推荐答案

我想 Swing Timer 会是更好的选择,因为它为所有计划事件重用单个线程并在主事件循环上执行事件代码.因此,在您的 SelectionListener 代码中,您执行以下操作:

I guess a Swing Timer would be a better option as it reuses a single thread for all scheduled events and executes the event code on the main event loop. So, inside your SelectionListener code you do:

// import javax.swing.Timer;

final Color backup = componentX.getBackground();
componentX.setBackground(Color.YELLOW);
final Timer t = new Timer(700, new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    componentX.setBackground(backup);
  }
});
t.setRepeats(false);
t.start();

这篇关于摆动 - 组件的短时高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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