如何使用Java Swing实现可拖动选项卡?
如何使用 Java Swing 实现可拖动选项卡? 我想将选项卡拖放到不同位置以重新排列选项卡,而不是静态 JTabbedPane。
How do I implement a draggable tab using Java Swing? Instead of the static JTabbedPane I would like to drag-and-drop a tab to different position to rearrange the tabs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其添加到 isDragAcceptable 以避免异常:
Add this to isDragAcceptable to avoid Exceptions:
@Tony:看起来Euguenes解决方案只是忽略了在交换期间保留TabComponents。
ConvertTab 方法只需要记住 TabComponent 并将其设置为它创建的新选项卡。
尝试使用这个:
@Tony: It looks like Euguenes solution just overlooks preserving TabComponents during a swap.
The convertTab method just needs to remember the TabComponent and set it to the new tabs it makes.
Try using this:
在 上找到了此代码管:
Found this code out there on the tubes:
我喜欢 Terai Atsuhiro san 的 DnDTabbedPane ,但我想从中得到更多。 最初的 Terai 实现在 TabbedPane 中传输选项卡,但如果我可以从一个 TabbedPane 拖动到另一个 TabbedPane 就更好了。
受到 @Tom 努力的启发,我决定自己修改代码。
我添加了一些细节。 例如,幽灵选项卡现在沿着选项卡式窗格滑动,而不是与鼠标一起移动。
setAcceptor(TabAcceptor a_acceptor)
应该让使用者代码决定是否让一个选项卡从一个选项卡式窗格转移到另一个选项卡式窗格。 默认接受器始终返回true
。I liked Terai Atsuhiro san's DnDTabbedPane, but I wanted more from it. The original Terai implementation transfered tabs within the TabbedPane, but it would be nicer if I could drag from one TabbedPane to another.
Inspired by @Tom's effort, I decided to modify the code myself.
There are some details I added. For example, the ghost tab now slides along the tabbed pane instead of moving together with the mouse.
setAcceptor(TabAcceptor a_acceptor)
should let the consumer code decide whether to let one tab transfer from one tabbed pane to another. The default acceptor always returnstrue
.诅咒! 被谷歌搜索打败了。 不幸的是,确实没有简单的方法可以在 Swing 中创建可拖动的选项卡窗格(或任何其他组件)。 因此,虽然上面的示例已经完成,但我刚刚编写的这个示例稍微简单一些。 因此,它有望更清楚地展示所涉及的更先进的技术。 步骤如下:
上面的示例将为您提供您想要的内容,但如果您想真正了解此处应用的技术,最好的练习是修整此示例的边缘并向其添加上面演示的额外功能。
或者也许我只是感到失望,因为我花时间编写这个解决方案,而这个解决方案已经存在:p
Curses! Beaten to the punch by a Google search. Unfortunately it's true there is no easy way to create draggable tab panes (or any other components) in Swing. So whilst the example above is complete this one I've just written is a bit simpler. So it will hopefully demonstrate the more advanced techniques involved a bit clearer. The steps are:
The above example will give you what you want but if you want to really understand the techniques applied here it might be a better exercise to round off the edges of this example and add the extra features demonstrated above to it.
Or maybe I'm just disappointed because I spent time writing this solution when one already existed :p