将 JScrollPane 组件添加到 JTable 列

发布于 2024-11-15 17:34:17 字数 330 浏览 1 评论 0原文

我正在尝试向 JTable 中的特定列添加滚动功能。我已经实现了一个自定义 TableCellRenderer 组件,我可以很好地看到表格内的滚动窗格,但我无法滚动它。我也尝试过实现 TableCellEditor 但没有任何运气。

    public Component getTableCellEditorComponent(JTable arg0, Object arg1,
        boolean arg2, int arg3, int arg4) {
    return scrollPane;
}

有谁知道如何使包含scrollPane 的单元格可滚动?

I'm trying to add scrolling capabilities to a certain column in my JTable. I've implemented a custom TableCellRenderer component and I can see the scroll pane inside the table just fine, but I am not able to scroll it. I've tried implementing TableCellEditor as well and didn't have any luck.

    public Component getTableCellEditorComponent(JTable arg0, Object arg1,
        boolean arg2, int arg3, int arg4) {
    return scrollPane;
}

Does anyone have any ideas how to make those cells which contain a scrollPane scrollable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

怎会甘心 2024-11-22 17:34:17

使用 TableCellRenderer 无法添加任何滚动行为,因为它不接收任何事件而仅绘制组件。
但是,可以通过使用自定义 TableCellEditor 来实现此目的,其中 getTableCellEditor 为:

public Component getTableCellEditorComponent(JTable table, Object value, boolean   isSelected, int row, int column) {
    JTextArea area = new JTextArea();
    area.setLineWrap(true);
    area.setText((String) value);

    JScrollPane pane = new JScrollPane(area);

    return pane;
}

此外,您必须控制 CellEditor 的编辑行为。为了使单元格始终可编辑和可滚动,isCellEditable 应该如下所示:

public boolean isCellEditable(EventObject anEvent) {
    return true;
}

不过,就我个人而言,我发现这个解决方案更像是一种黑客行为。
另外,这应该仅用于测试。在我看来,你确实必须实现更好的编辑行为。

With TableCellRenderer it's not possible to add any scrolling behaviour, as it does not receive any events and only draws the component.
It is possible - however - to accomplish this by using a custom TableCellEditor with getTableCellEditor being:

public Component getTableCellEditorComponent(JTable table, Object value, boolean   isSelected, int row, int column) {
    JTextArea area = new JTextArea();
    area.setLineWrap(true);
    area.setText((String) value);

    JScrollPane pane = new JScrollPane(area);

    return pane;
}

Additionally, you have to control the editing behaviour of your CellEditor. To make the cell editable and scrollable always, isCellEditable should look like this:

public boolean isCellEditable(EventObject anEvent) {
    return true;
}

Personally, I find this solution to be more of a hack than anything, though.
Also, this should only be for testing. You really do have to implement a better editing behaviour in my opinion.

破晓 2024-11-22 17:34:17

渲染器只是绘制单元格。我相信您需要实现一个 TableCellEditor 才能滚动。

A Renderer just paints the cells. I believe you need to implement a TableCellEditor to scroll.

寄风 2024-11-22 17:34:17

作为替代方案,请考虑将单个滚动窗格放置在单独的容器中,并在选择侦听器中更新其视图。

As an alternative, consider placing a single scroll pane in a separate container and updating it's view in your selection listener.

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