开发者

Scrollable JPanel in JTable cell

I want to create custom cell in JTable. I used custom renderer and returned JPanel object. It works but there is one problem. While program is running the JPanel draws on itself using paintComponent() method. On each "tick" (usually each 100ms) the panel is getting wider (Im drawing kind of graph) and when it becomes开发者_JAVA技巧 too big the rest is just hidden. Id like it to resize and create scrollbar. I tried several ways of putting ScrollPane but none of them worked. Basically, I want something like Thread view in 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?

If you still want use JTable:

Every scroll pane contain special JViewport control. This JViewport control actually do all scrolling work, and JSrollPane itself only layout it with scroll bars. 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). But JSrollPane need heavy layout work so this very slow solution.

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. 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. 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). 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). You can also use JViewport itself to do all pain work, but in control hard to understanding. 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜