JavaFX 2自定义弹出窗格 [英] JavaFX 2 custom popup pane

查看:425
本文介绍了JavaFX 2自定义弹出窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX 2颜色选择器有一个按钮,弹出一个颜色选择器窗格,如下所示:





我想做类似的事情,因为我想要一个自定义窗格单击按钮时弹出,单击其他内容时消失(在我的情况下,一些图像缩略图)。实现这一目标的最佳方式是什么?我应该使用ContextMenu并以某种方式向MenuItem添加一个窗格,还是还有其他我应该看的东西?

解决方案

它是使用当前的JavaFX 2.2 API很难做到这一点。



以下是一些选项。






使用带有图形集的MenuButton的MenuItem



这是。



ColorPicker扩展 PopupControl 。您可以这样做,但并非构建您自己的PopupControl所需的所有API目前都是公开的。因此,对于JavaFX 2.2,您将不得不依赖于已弃用的内部com.sun类,并将被JDK8中的公共javafx.scene.control类替换。






使用ContextMenu



所以,我认为你的想法是使用ContextMenu并添加一个窗格到菜单可能是目前最好的方法。您应该可以使用 CustomMenuItem 在普通的 MenuItem 上设置图形 ContextMenu 有很好的相对定位逻辑。一个 ContextMenu 也可以由 MenuButton






使用自定义对话框



为此,请在相对于节点的位置显示透明舞台。



一些示例代码可以帮助您开始我暂时链接此处
示例代码对主窗口的两侧进行相对定位,但您可以更新它以执行相对于给定节点边的定位(如ContextMenu的show方法)。






使用玻璃窗格



为此,请创建一个StackPane作为主窗口的根目录。将主内容窗格作为StackPane中的第一个节点,然后创建一个Group作为stackpane中的第二个节点,以便它将覆盖主要内容的顶部。通常,顶部组不包含任何内容,但是当您想要显示弹出窗口时,将其放在顶部组中并将其转换为相对于主要内容中相应节点的位置。



您可以查看此演示中的锚点节点如何用于查看适应您的背景。







JavaFX8是否有针对此的相关更新?


Java 8的相关性差别不大,一般来说,这些选项基于Java 2.2功能在本文中列出。 Java 8确实添加了 Dialog 提醒功能,但这些是更有针对性地使用带边框,标题和按钮的对话框,而不是问题中所需的功能。也许你可以从Dialog类开始并大量定制它以获得接近所需的东西,但你最好从空白阶段或PopupControl开始。


The JavaFX 2 colour picker has a button that pops up a colour chooser pane like so:

I'd like to do something similar, in that I'd like a custom pane to pop up when the button is clicked on and disappear when something else is clicked (in my case, a few image thumbnails). What would be the best way of achieving this? Should I use a ContextMenu and add a pane to a MenuItem somehow, or is there something else I should look at?

解决方案

It's kind of difficult to do well with the current JavaFX 2.2 API.

Here are some options.


Use a MenuButton with a graphic set in it's MenuItem

This is the approach taken in Button with popup showed below's executable sample code.


Use a PopupControl

Take a look at how the ColorPicker does this in it's code.

ColorPicker extends PopupControl. You could do this, but not all of the API required to build your own PopupControl is currently public. So, for JavaFX 2.2, you would have to rely on internal com.sun classes which are deprecated and will be replaced by public javafx.scene.control classes in JDK8.


Use a ContextMenu

So, I think your idea to "use a ContextMenu and add a pane to a MenuItem" is probably the best approach for now. You should be able to do this by using a CustomMenuItem or setting a graphic on a normal MenuItem. The ContextMenu has nice relative positioning logic. A ContextMenu can also be triggered by a MenuButton.


Use a Custom Dialog

To do this, display a transparent stage at a location relative to the node.

There is some sample code to get you started which I have temporarily linked here. The sample code does relative positioning to the sides of the main window, but you could update it to perform positioning relative to the sides of a given node (like the ContextMenu's show method).


Use a Glass Pane

To do this, create a StackPane as your root of your main window. Place your main content pane as the first node in the StackPane and then create a Group as the second node in the stackpane, so that it will layer over the top of the main content. Normally, the top group contains nothing, but when you want to show your popup, place it in the top group and translate it to a location relative to the appropriate node in your main content.

You could look at how the anchor nodes in this demo are used to see how this might be adaptable to your context.


Is there a relevant update for this for JavaFX8?

There is not much difference of relevance for Java 8, in general the options are as outlined in this post based on Java 2.2 functionality. Java 8 does add Dialog and Alert functionality, but those are more targeted at use of dialogs with borders, titles and buttons rather than the kind of functionality desired in the question. Perhaps you might be able to start from the Dialog class and heavily customize it to get something close to what is needed, but you are probably better off starting from a blank stage or PopupControl instead.

这篇关于JavaFX 2自定义弹出窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆