将 JScrollPane 组件添加到 JTable 列
我正在尝试向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 TableCellRenderer 无法添加任何滚动行为,因为它不接收任何事件而仅绘制组件。
但是,可以通过使用自定义 TableCellEditor 来实现此目的,其中 getTableCellEditor 为:
此外,您必须控制 CellEditor 的编辑行为。为了使单元格始终可编辑和可滚动,isCellEditable 应该如下所示:
不过,就我个人而言,我发现这个解决方案更像是一种黑客行为。
另外,这应该仅用于测试。在我看来,你确实必须实现更好的编辑行为。
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:
Additionally, you have to control the editing behaviour of your CellEditor. To make the cell editable and scrollable always, isCellEditable should look like this:
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.
渲染器只是绘制单元格。我相信您需要实现一个
TableCellEditor
才能滚动。A Renderer just paints the cells. I believe you need to implement a
TableCellEditor
to scroll.作为替代方案,请考虑将单个滚动窗格放置在单独的容器中,并在选择侦听器中更新其视图。
As an alternative, consider placing a single scroll pane in a separate container and updating it's view in your selection listener.