将JScrollPane组件添加到JTable列

将JScrollPane组件添加到JTable列

本文介绍了将JScrollPane组件添加到JTable列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

有没有人有任何想法如何让那些包含scrollPane的单元格可滚动?

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

推荐答案

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

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;
}

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

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.

这篇关于将JScrollPane组件添加到JTable列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:25