【发布时间】:2017-01-10 22:43:08
【问题描述】:
我已经运行了Mastering FXML example、How to create custom components in JavaFX 2.0 using FXML 并尝试了该站点的各种其他解决方案,但我仍然没有找到一个足够简单的示例来展示如何设置一个不是唯一部分的自定义控件图形用户界面。由于问题仍在弹出,我们似乎需要为我们中的一些人提供一个更简单的示例..
我正在尝试创建一个简单的控件,该控件由一个垂直的 SplitPane 组成,顶部有一个 Button,下部有一个标签。然后,我想将此 SplitPane 控件的实例放在 TabPane 的多个选项卡中。 控件不会显示,或者我陷入各种错误,这取决于我尝试遵循的示例。所以,我会回溯一点,只是简单地问:如何将 SplitPane 分离出来作为这里的自定义控件?
这是 FXML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<TabPane prefHeight="256.0" prefWidth="477.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customcontroltest.FXMLDocumentController">
<tabs>
<Tab>
<content>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="114.0" prefWidth="160.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Button fx:id="button" onAction="#handleButtonAction" text="Click Me!" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label fx:id="label" minHeight="16" minWidth="69" />
</children>
</AnchorPane>
</items>
</SplitPane>
</content>
</Tab>
</tabs>
</TabPane>
还有控制器类:
package customcontroltest;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable
{
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event)
{
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
还有主要的测试类:
package customcontroltest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CustomControlTest extends Application
{
@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
我创建了一个新的 FXML 文件并将整个 SplitPane 标记及其所有内容剪切/粘贴到其中。我用<packageName.ControlClassName /> 替换了FXML 文档中的SplitPane 标记。然后我制作了控制器类来扩展 SplitPane。我尝试在 FXML 标记和/或控制器类中指定控制器,但从来没有做对。
有正确知识的人愿意花几分钟来展示一个可行的例子吗?我猜更多的人会发现这样的例子非常有用。
因此,SplitPane 应该是新的自定义控件,然后您可以默认将其加载到 TabPane 的第一个选项卡中。然后我将编写代码以将更多实例添加到后续选项卡中。
非常感谢您。
更新:
我已将SplitPane 分解为它自己的 FXML 和控制器类。
这是 FXML (CustomSplitPane.fxml):
<fx:root type="javafx.scene.control.SplitPane" dividerPositions="0.5" orientation="VERTICAL" prefHeight="114.0" prefWidth="160.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customcontroltest.CustomSplitPaneController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Button fx:id="button" onAction="#handleButtonAction" text="Click Me!" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label fx:id="label" minHeight="16" minWidth="69" />
</children>
</AnchorPane>
</items>
</fx:root>
以及控制器类(CustomSplitPaneController.java):
package customcontroltest;
public class CustomSplitPaneController extends AnchorPane
{
@FXML
private Label label;
private SplitPane mySplitPane;
public CustomSplitPaneController()
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CustomSplitPane.fxml"));
try
{
fxmlLoader.load();
} catch (IOException exception)
{
throw new RuntimeException(exception);
}
}
@FXML
private void handleButtonAction(ActionEvent event)
{
label.setText("Hello World!");
}
}
原来的主要 FXML 现在看起来像这样:
<TabPane prefHeight="256.0" prefWidth="477.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="customcontroltest.FXMLDocumentController">
<tabs>
<Tab>
<content>
<customcontroltest.CustomSplitPaneController />
</content>
</Tab>
</tabs>
</TabPane>
CustomSplitPaneController 中的fxmlLoader.load() 似乎导致了 java.lang.StackOverflowError。
也许现在对这里的人来说更明显的是我错过了什么?
再次感谢。
【问题讨论】:
-
我不明白您发布的代码的哪一部分被您认为是此处的“自定义控件”。您是否尝试过按照documentation 中描述的方式执行此操作?
-
我有,但似乎缺少一些东西。我希望“自定义控件”成为 SplitPane 标记内的所有内容。
-
那么你为什么不在你的问题中发布呢?如果他们看不到您的代码,没有人可以帮助您弄清楚您缺少什么。
-
公平点。我希望有人能够在一个工作示例中为我破解 SplitPane,而不是调查我的代码中的错误。我可能是错的。我将再写一次尝试并发布代码。谢谢。
-
不过,您已经引用了一个非常好的工作示例……目前尚不清楚(至少对我而言)您想要的内容不在您链接的示例中。
标签: javafx