getListCellRendererComponent调用了多少次? [英] How many times is getListCellRendererComponent called?

查看:79
本文介绍了getListCellRendererComponent调用了多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解getListCellRendererComponent方法的工作原理,但我不明白.我制作了一个扩展BasicComboBoxRenderer的单独的类,并添加了一个计数器,该计数器在每次调用getListCellRendererComponent时都会打印.然后,我运行带有main方法的测试类,该方法仅显示带有使用我的自定义渲染器类的JComboBox的框架.此组合框共有3个项目,我已经设置了setMaximumRowCount(2),因此只显示其中2个.

I'm trying to understand how getListCellRendererComponent method works but I don't get it. I made a separate class that extends BasicComboBoxRenderer and I added a counter which is printed every time getListCellRendererComponent is called. I then run a test class with a main method that shows a frame with just a JComboBox that uses my custom renderer class. This combobox has 3 items in total and I've set setMaximumRowCount(2) so it only shows 2 of them.

  • 当我第一次运行程序并出现带有组合框的框架时,计数器会通知getListCellRendererComponent被调用了6次.
  • 当框架失去焦点时(例如,当我单击桌面时),该方法执行1次.
  • 当框架重新获得焦点(单击我的框架)时,该方法执行1次.
  • 当我单击箭头按钮并第一次出现下拉列表时,计数器说该方法执行8次.
  • 当我再次单击箭头按钮并且列表消失时,该方法被称为1次(这种情况总是发生).
  • 当我第一次单击箭头按钮后,该方法被调用了5次.
  • 当我单击滚动条按钮向上或向下时,该方法执行1次.
  • 当我将光标移到列表中未选中的项目上时,该方法将执行2次,然后再执行1次(这是最荒谬的)
  • 当我单击列表中的一个项目时,该方法将执行4次.

起初,我认为此方法将执行与列表中项目数一样多的次数(加上组合框显示区域中出现的其他项目).

At first I thought that this method will be executed as many times as the number of the items in the list (plus one more that appears on the combobox display area).

但是从上面我只能理解一种或两种情况,例如,当我单击滚动条按钮并且该方法执行1次时,可能是因为呈现了一个新项.他们其余的人似乎疯了……

But I can only understand one or two cases from the above, for example when I click the scrollbar buttons and the method executes 1 time, probably because a new item is rendered. The rest of them seem insane...

推荐答案

我希望在任何一次调用渲染器时,至少要进行n +1次迭代.

I would expect no less then n + 1 iterations of the renderer to be called at any one time.

该组件需要

  1. 找出内容的最佳尺寸.这可以通过使用原型值来实现,或者如果未指定原型值,则遍历所有项以找到最大界限(即3倍)
  2. 如果某项存在+1次,则呈现所选项目
  3. 如果弹出窗口可见+3次,则呈现列表
  4. 可能会寻找工具提示

=可能有7次迭代

失去焦点时,组件需要渲染所选项目+1

When losing focus, the component needs to render the selected item +1

重新获得焦点时,组件将尝试再次渲染所选项目+1

When regaining focus, the component will try a render te selected item again +1

显示弹出窗口时,请参见第一部分

When the popup is displayed, see the first part

第二次可能表明该组件已缓存第一个弹出操作的结果(该组件可能在焦点事件之间使其内部缓存无效)

The second time probably indicates that the component has cached the result of the first popup action (it's possible that the component is invalidating its internal cache between focus events)

每次更改滚动窗格的视图时,它都需要呈现以前未在屏幕上显示的所有项目,这是出于优化的原因而进行的(假设第一个包含100多个项目,则渲染所有这些项目都是在浪费时间,因此包含了原型值)

Each time you change the view of the scrollpane, it needs to render any items not previously shown on the screen, this is done for optimisation reasons (imagine a lst with 100s of items, rendering all of them is a waste of time, hence the inclusion of the prototype value)

鼠标动作可以触发一系列不同的动作,例如鼠标移入,鼠标移出,鼠标移动.最幸运的是,这些与工具提示管理器和组件一起尝试确定工具提示是否可用

Mouse actions can be triggering a series of different actions, mouse in, mouse out, mouse move. Most likly these are to do with the tooltip manager and component trying to determine if a tooltip is available

尝试设置原型值&看看当组件显示其弹出窗口时,是否改变了迭代次数

Try setting a prototype value & see if that changes the number of iterations when the component displays its popup

这篇关于getListCellRendererComponent调用了多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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