简体   繁体   English

JTable单元中的可滚动JPanel

[英]Scrollable JPanel in JTable cell

I want to create custom cell in JTable. 我想在JTable中创建自定义单元格。 I used custom renderer and returned JPanel object. 我使用了自定义渲染器并返回了JPanel对象。 It works but there is one problem. 它有效,但是有一个问题。 While program is running the JPanel draws on itself using paintComponent() method. 程序运行时,JPanel使用paintComponent()方法自行绘制。 On each "tick" (usually each 100ms) the panel is getting wider (I m drawing kind of graph) and when it becomes too big the rest is just hidden. I 在每个“滴答”(通常每100ms)上,面板会变宽(我正在m drawing kind of graph) and when it becomes too big the rest is just hidden. I m drawing kind of graph) and when it becomes too big the rest is just hidden. I d like it to resize and create scrollbar. m drawing kind of graph) and when it becomes too big the rest is just hidden. I希望它调整大小并创建滚动条。 I tried several ways of putting ScrollPane but none of them worked. 我尝试了几种放置ScrollPane的方法,但没有一种起作用。 Basically, I want something like Thread view in Java VisualVM. 基本上,我想要类似Java VisualVM中的“线程”视图的东西。 Any ideas? 有任何想法吗?

It is hard work to put scroll pane inside ordinary table cell, because only one component used to render all cells in table. 将滚动窗格放置在普通表单元格中是困难的,因为只有一个组件用于呈现表中的所有单元格。 May be remove JTable and just put large set of components in ordinary JPanel with grid layout? 可以删除JTable并仅将大型组件集放在具有网格布局的普通JPanel中吗?

If you still want use JTable: 如果仍然要使用JTable:

Every scroll pane contain special JViewport control. 每个滚动窗格都包含特殊的JViewport控件。 This JViewport control actually do all scrolling work, and JSrollPane itself only layout it with scroll bars. 这个JViewport控件实际上完成所有滚动工作,而JSrollPane本身仅使用滚动条对其进行布局。 Because single viewport used for all cells, you must store somewhere this viewport scroll position and restore it for every painting cell (on getCellRenderer method). 因为单个视口用于所有单元格,所以您必须将该视口滚动位置存储在某个位置,并为每个绘画单元恢复它(在getCellRenderer方法上)。 But JSrollPane need heavy layout work so this very slow solution. 但是JSrollPane需要繁重的布局工作,因此这种解决方案非常慢。

May be most preferable solution: override you JPane cell renderer paint method, and this paint method must create sub-graphics (Graphics.create() ) with proper scroll offset. 可能是最可取的解决方案:重写JPane单元渲染器的绘制方法,并且此绘制方法必须创建具有适当滚动偏移量的子图形(Graphics.create())。 Scroll offset data must be stored somewhere (in table model for example). 滚动偏移数据必须存储在某个地方(例如,在表模型中)。 When user click on cell, JTable begin cell editing, and install cell editor. 当用户单击单元格时,JTable将开始单元格编辑,然后安装单元格编辑器。 This must be special cell editor, what contains scroll pane itself. 这必须是特殊的单元格编辑器,其中包含滚动窗格本身。 User scroll this control (none special support required) and went he finished cell editing you must save scroll position to model (in stopCellEditing() function). 用户滚动此控件(不需要任何特殊支持),然后完成单元格编辑,您必须将滚动位置保存到模型中(在stopCellEditing()函数中)。 Then cell rendered paint itself and in paint method get content scroll offset from model. 然后单元格渲染的绘画本身和绘画方法中的内容滚动偏离模型。 User see scrolled cell. 用户看到滚动的单元格。 You also can put JScrollBar inside cell rendered, so user can see scroll position (but it need special support). 您还可以将JScrollBar放在渲染的单元格内,以便用户可以看到滚动位置(但需要特殊的支持)。 You can also use JViewport itself to do all pain work, but in control hard to understanding. 您还可以使用JViewport本身来完成所有繁琐的工作,但在控制上却难以理解。 I do not see any troubles wit use scroll pant in cell editor, so it must work. 我看不到在单元格编辑器中使用滚动裤的任何麻烦,因此它必须可以工作。 In any way it is huge work. 无论如何,这是一项巨大的工作。

So you've set up a custom table cell renderer, and I'm assuming this renderer is a JScrollPane view on a JPanel . 因此,您已经设置了自定义表格单元格渲染器,并且我假设此渲染器是JPanel上的JScrollPane视图。 You might need to play around with calls to setPreferredSize() on the scroll pane and calling setPreferredSize() and/or setSize() on the panel (or whatever inner element is being viewed in the scroll pane) before calling validate() to trigger a layout update. 您可能需要玩弄调用setPreferredSize()滚动窗格和调用setPreferredSize()和/或setSize()在面板上(或其他内部元件被滚动窗格查看)之前调用validate()来触发布局更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM