- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
场景:顶层容器是一个 Swing JDialog,它有一些 fx 内容,包括一个触发按钮处置的 fx 按钮。当手动创建按钮并使用适当的 eventHandler 配置时,处理工作符合预期(隐藏对话框)。通过 fxml 创建/配置按钮时,不会处理该对话框。下面的示例包含一个手动配置的按钮和一个 fxml 加载/绑定(bind)按钮以查看不同的行为。
问题:
代码:
package fxml;
import java.io.IOException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
public class DisposeExample {
@FXML
Button closeButton;
Button fxButton;
private JDialog dialog;
/**
* The action handler method used by fx buttons.
*/
public void onAction(final ActionEvent ac) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("onAction: " +ac);
dialog.dispose();
}
});
}
protected Button createFxButton() {
Button fxButton = new Button("close from fx");
fxButton.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
onAction(event);
}
});
return fxButton;
}
public void initFX() {
final JFXPanel fxPanel = new JFXPanel();
dialog.add(fxPanel);
Platform.runLater(new Runnable() {
@Override
public void run() {
FlowPane parent = null;
try {
parent = FXMLLoader.load(DisposeExample.class.getResource(
"DisposeController.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
fxButton = createFxButton();
parent.getChildren().add(fxButton);
Scene scene = new Scene(parent);
fxPanel.setScene(scene);
}
});
}
public DisposeExample() {
dialog = new JDialog();
dialog.setTitle("Simple Swing Dialog");
initFX();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JDialog example = new DisposeExample().dialog;
example.setSize(400, 400);
example.setVisible(true);
}
});
}
}
fxml内容:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<FlowPane id="Content" fx:id="windowPanel"
xmlns:fx="http://javafx.com/fxml" fx:controller="fxml.DisposeExample">
<children>
<Button fx:id="closeButton" onAction="#onAction"
prefHeight="35.0" prefWidth="300.0" text="Close (fxml controller)">
</Button>
</children>
</FlowPane>
顺便说一句:有一个 similar question从今年开始,没有答案。
编辑
发生了一些奇怪的事情:运行几分钟后,它抛出一个 OutOfMemoryError - 更深层次的东西不会停止创建......什么?
java.lang.OutOfMemoryError: Java heap space
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.privateGetPublicMethods(Class.java:2641)
at java.lang.Class.privateGetPublicMethods(Class.java:2657)
at java.lang.Class.privateGetPublicMethods(Class.java:2657)
at java.lang.Class.privateGetPublicMethods(Class.java:2657)
at java.lang.Class.privateGetPublicMethods(Class.java:2657)
at java.lang.Class.privateGetPublicMethods(Class.java:2657)
at java.lang.Class.getMethods(Class.java:1457)
at sun.reflect.misc.MethodUtil.getMethods(MethodUtil.java:99)
at com.sun.javafx.fxml.BeanAdapter.updateMethodCache(BeanAdapter.java:265)
at com.sun.javafx.fxml.BeanAdapter.setBean(BeanAdapter.java:250)
at com.sun.javafx.fxml.BeanAdapter.<init>(BeanAdapter.java:213)
at javafx.fxml.FXMLLoader$Element.getValueAdapter(FXMLLoader.java:157)
at javafx.fxml.FXMLLoader$Element.getProperties(FXMLLoader.java:165)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:647)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:570)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2744)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2709)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2696)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2685)
at fxml.DisposeExample$3.run(DisposeExample.java:65)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
编辑
仅供引用:8u113 中的相同行为,因此提交了 issue in fx-jira , 我是不可救药的乐观主义者 :-)
最佳答案
anything wrong with the example?
一个响亮的YES - 引用了 Martin 对这个问题的评论(非常快速和清晰 :-):
The problem is in your fxml file.
"fx:controller" attribute takes the class and creates a new instance of it (using the default contructor). The default constructor of your DisposeExample class posts a new Runnable that will load the same fxml file again a create yet another instance of DisposeExample class.
You should either use a different class for your controller or set the controller manually using the setController() call or using a controller factory (setControllerFactory). Otherwise, there is no way for FXMLLoader to know that you wanted to use your particular DisposeExample object.
关于java - 混音 Swing/FX : can't dispose a dialog from fxml controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889901/
我的主 UI 是在 FXML 文件中定义的,并且应该包含人员列表。一个人会显示一张图片和一些不同的只读文本行(姓名、年龄等......)该列表本身在整个运行时会不断变化。 (增删改查) 我知道可以创建
正如标题所述,构建表并将所有内容添加到表的初始化方法与 FXMLLoader 之间存在一定的冲突,FXMLLoader 应该为弹出窗口加载 FXML。 我的代码: 主要: import javafx.
我正在创建丰富的 UI 应用程序,我在 FXML 中使用 FXML,每个部分都有单独的 Controller 。我决定关注this教程并使用我的 fxml 作为组件。所以我使用类作为 Controll
你好, 我有以下问题。 是否可以创建一个主 fxml 文件并放置/包含另一个应该具有用户定义属性的 fxml 文件。 例如,我有:main.fxml 和一个fan_object.fxml 然后将 3
我正在尝试制作一个具有用于制作新项目的对话框的应用程序。我已经对其进行了编程,但想要清理文件结构,因此我将对话框及其 Controller 的 fxml 移至它们自己的包中。该对话框的文件位于名为 n
你好, 我有以下问题。 是否可以创建一个主 fxml 文件并放置/包含另一个应该具有用户定义属性的 fxml 文件。 例如,我有:main.fxml 和一个fan_object.fxml 然后将 3
我的应用程序有选项卡式 Pane ,因此为了保持 fxml 文件易于管理,我有一个包含选项卡的主 fxml 文件,以及每个其他选项卡的单独 fxml 文件。这工作正常,但由于某种原因,应用程序已停止加
如果状态为“1”,我想加载另一个 FXML 文件。但在下面的代码中,它不会从初始化加载另一个 FXML。如果我使用按钮,那么此代码可以工作,但我想在没有任何按钮的情况下执行此操作。谁能帮我提个建议吗?
我是 JavaFX 的新手,我想知道是否有办法将一个 fxml 文件放在 child.fxml 中,例如 parent.fxml。 为什么我需要这个? 想法是,我想创建独立的屏幕(小屏幕)并编写一个父
我的程序有一个主要的 FXML 文档,其中包含 TabPane .对于每个选项卡,我希望它有自己的 Controller 和 fxml 文件。当我尝试将外部 fmxl 文件包含到主 fxml 文档中时
我有一个用 fxml 编写的边框 Pane ,它的左 Pane 和中央 Pane 具有可互换的布局。 边框 fxml:
在上面的 fxml 中,我有许多使用相同源 fx:include source="MyCombo.fxml"的 fxml include 标记。可以这样做吗?这样做会影响 F
我需要创建许多不同的 FXML 文件,并且每个文件都有一致的布局。每个都有一个 AnchorPane,它将保存单独的内容。 有没有办法加载“基本”FXML 文件,然后加载第二个 FXML 文件,并将数
我正在尝试创建一个非常简单的带有黑色背景的VBox。就这样。没有其他的。我使用 FXML 来描述我的 VBox。 样本.fxml: Controller .java: package sa
我不明白为什么我总是有同样的错误: image 当我尝试在我的strcuture项目中添加库(fx java)时。 我的build.gradle: plugins { id 'java'
这就是我想要实现的目标。 /Package A/ /Package A/ApplicationController.java /Package A/Application.fxml 在我的 Appli
我有两个 FXML 文件。第一个描述了要显示的第一个 Pane ,其中包含一个选项卡 Pane 、一个菜单和一个菜单项,该菜单项应该在选项卡 Pane 中打开一个新选项卡并在其中绘制一组新节点。这是代
我想要做的是,如果调整场景(应用程序窗口)的大小,我想相应地调整窗口内内容的大小。我已经发布了到目前为止我已经完成的代码 加载器类 public class ResizingButtons exten
我使用的是 Java JDK 13 和 FontAwesomeFX 11。 我有一个 FXML 文件,其中包含一些 FontAwesomeIconViews,但是当将该文件加载到我的 Controll
我明白了 javafx.fxml.LoadException: 当我使用以下代码行加载 fxml 文件时。 AnchorPane anchorPane = (AnchorPane)loader.loa
我是一名优秀的程序员,十分优秀!