带有自定义渲染器的 JList

发布于 2024-10-10 12:28:53 字数 204 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情勿念 2024-10-17 12:28:53

这是 JList(以及 JTabelJComboBox 等)的正常行为。

您的自定义呈现器返回的 JPanel 不会添加到 Swing 层次结构中。 JList 仅使用它的 paint 方法,在正确的位置绘制渲染器。渲染器只是充当一个印记,您在 JList 中看到的不是组件,而是组件的图像。

这是在屏幕上显示许多组件的有效方式,而无需实际实例化组件的开销。请注意,您的渲染器可以始终返回相同的实例(这是更好的选择)。

有关更多详细信息,请参阅 Swing 教程

如果您希望 JList 的条目像真实组件一样工作,您可以执行以下操作。首先,使用 JTable 而不是 JList。具有一列但没有标题的 JTableJList 大致相同。为什么使用JTable?因为JTable提供了编辑器。编辑器在 JTable 上注册,就像渲染器一样。当用户单击 JTable 的单元格时,通常会出现编辑器。编辑器叠加在渲染器上,这次它是一个真正的组件。如果渲染器和编辑器组件是相同的,那么用户就会感觉 JTable 的单元格是真实的组件。

Swing 教程 提供了此技术的示例。

This is the normal behavior of the JList (and JTabel, JComboBox, etc...).

The JPanel that your custom renderer returns is not added to the Swing hierarchy. Only its paint method is used by the JList, to draw the renderer at the right place. The renderer just acts as a stamp, and what you see in the JList 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 a JTable instead of a JList. A JTable with one column and no header is roughly the same as a JList. Why use a JTable? Because JTable provides Editors. Editors are registered on the JTable, 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.

少女七分熟 2024-10-17 12:28:53

我无法选择它们,如果我有一个按钮或文本区域,我无法按下它

渲染器只是组件的绘画。它不是真正的组件,因此您无法单击按钮或在文本区域中输入文本。

为此,您需要使用真实的组件。因此,最好使用组件创建自定义面板,然后将该面板添加到已添加到滚动窗格的另一个面板。

i cant select them and if i have a button or a text area in it i can not press it

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.

叹梦 2024-10-17 12:28:53

这是我的解决方案:

public class AccountRenderer extends DefaultListCellRenderer {

private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (value != null) {
        Account entry = (Account) value;
        renderer.setText(entry.getName());
    }
    return renderer;
}

}

Here's my solution:

public class AccountRenderer extends DefaultListCellRenderer {

private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (value != null) {
        Account entry = (Account) value;
        renderer.setText(entry.getName());
    }
    return renderer;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文