带有自定义渲染器的 JList
我有一个 JList,上面显示多个 JPanel,我创建了一个返回新 JPanel 的自定义渲染器。 JPanels 显示在 JList 中,但它们不可访问,我无法选择它们,如果其中有按钮或文本区域,我无法按下它。 我想尝试一下这是否适用于 JList,因为我想进行进一步的分页。我设法通过将面板添加到 Jscroll 窗格来让它工作,但我很想让 JList 工作起来。
谢谢
I have a JList that shows multiple JPanels on them , i have created a custom renderer that returns a new JPanel.
The JPanels are displayed in the JList but they are not accessible, i cant select them and if i have a button or a text area in it i can not press it.
I want to try if this works in a JList because i want to do further pagination. I managed to get it work by adding panels to a Jscroll pane, but would love to make the JList thing work.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是
JList
(以及JTabel
、JComboBox
等)的正常行为。您的自定义呈现器返回的
JPanel
不会添加到 Swing 层次结构中。JList
仅使用它的paint
方法,在正确的位置绘制渲染器。渲染器只是充当一个印记,您在JList
中看到的不是组件,而是组件的图像。这是在屏幕上显示许多组件的有效方式,而无需实际实例化组件的开销。请注意,您的渲染器可以始终返回相同的实例(这是更好的选择)。
有关更多详细信息,请参阅 Swing 教程。
如果您希望 JList 的条目像真实组件一样工作,您可以执行以下操作。首先,使用
JTable
而不是JList
。具有一列但没有标题的JTable
与JList
大致相同。为什么使用JTable
?因为JTable
提供了编辑器。编辑器在JTable
上注册,就像渲染器一样。当用户单击 JTable 的单元格时,通常会出现编辑器。编辑器叠加在渲染器上,这次它是一个真正的组件。如果渲染器和编辑器组件是相同的,那么用户就会感觉 JTable 的单元格是真实的组件。Swing 教程 提供了此技术的示例。
This is the normal behavior of the
JList
(andJTabel
,JComboBox
, etc...).The
JPanel
that your custom renderer returns is not added to the Swing hierarchy. Only itspaint
method is used by theJList
, to draw the renderer at the right place. The renderer just acts as a stamp, and what you see in theJList
are not components, but images of components.This is an efficient way of displaying many components on the screen, without having the overhead of real instantiated components. Note that your renderer can return always the same instance (it's even preferable).
See the Swing tutorial for more details.
If you want the entries of the
JList
to act like real components, you can do the following. First, use aJTable
instead of aJList
. AJTable
with one column and no header is roughly the same as aJList
. Why use aJTable
? BecauseJTable
provides Editors. Editors are registered on theJTable
, just like Renderers are. An Editor appears generally when a user clicks on a JTable's cell. The Editor is superimposed over the renderer, and this time it's a real component. If the Renderer and the Editor components are identical, then the user has the feeling that the JTable's cells are real components.The Swing tutorial has examples for this technique.
渲染器只是组件的绘画。它不是真正的组件,因此您无法单击按钮或在文本区域中输入文本。
为此,您需要使用真实的组件。因此,最好使用组件创建自定义面板,然后将该面板添加到已添加到滚动窗格的另一个面板。
A renderer is just a painting of a component. It is not a real component so no you can't click a button or enter text into a text area.
You need to use real components for this. So it is probably better to create a custom panel with you components and then add the panel to another panel that has been added to a scroll pane.
这是我的解决方案:
}
Here's my solution:
}