FXML Stuffs (include and define)
Hello folks,
Today I would like to blog about the FXML define and include tag which can be very useful for those who are very keen to use FXML in their application. The FXML is an XML file which is loaded by javafx using FXMLLoader . It’s all loaded at the runtime and it’s really fast to load and easy to learn too. The previous blog about the FXML as Flexible XML is just a basic about FXML . Today in this blog here you will learn about how to include FXML files in your main FXML file.
I’m talking about the <fx:include> tag of the FXML.
Let’s see the basic flow of how the <fx:include> are loaded. Let’s assume there are two FXML Main.fxml and Child.fxml

                                                    

01 <?xml version="1.0" encoding="UTF-8"?>
02  
03 <?import java.lang.*?>
04 <?import javafx.scene.*?>
05 <?import javafx.scene.control.*?>
06 <?import javafx.scene.layout.*?>
07 <?import javafx.scene.shape.*?>
08 <?import javafx.scene.effect.*?>
09 <?import fxmlstuff.Main?>
10  
11 <Main xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlstuff.Main" >
12     <fx:define>
13         <fx:include source="Home.fxml" fx:id="homeContent" />
14         <fx:include source="About.fxml" fx:id="aboutContent" />
15  
16    </fx:define>
17     <center>
18         <TabPane fx:id="tabpane" translateY="5" translateX="5"  >
19             <tabs>
20                 <Tab text="HOME" fx:id="homeTab" content="$homeContent" closable="false" />
21                 <Tab text="ABOUT" fx:id="aboutTab" content="$aboutContent" closable="false"/>
22             </tabs>
23         </TabPane>
24     </center>
25 </Main>

You can see the highlighted lines of which helps to load the content of Home.fxml and About.fxml in the Main.fxml . Also we ‘ve added fx:id property for making instances            available of Home and About class. Now let’s see the FXController class of Main.fxml                                                                                  

01 package fxmlstuff;
02  
03 import java.net.URL;
04 import java.util.ResourceBundle;
05 import javafx.fxml.FXML;
06 import javafx.fxml.Initializable;
07 import javafx.scene.layout.BorderPane;
08  
09 /**
10  * @author Narayan
11  */
12  
13 public class Main extends BorderPane implements Initializable{
14  
15     @FXML
16     private Home homeContent;
17     @FXML
18     private About aboutContent;
19  
20     @Override
21     public void initialize(URL url, ResourceBundle rb) {
22     }
23 }

Here in the above image you can see the Child.fxml is called inside Main.fxml embeded by <fx:define> Now you need to know that the <fx:define> are used for defining any variables or any instances inside FXML.
Let’s see things in real . We are going to make one simple Tab based application which simply get’s it’s tab content from different FXML files. Firstly we’ll make Main.fxml which contains the TabPane.
Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.effect.*?>
<?import fxmlstuff.Main?> <Main xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlstuff.Main" >
<fx:define>
<fx:include source="Home.fxml" fx:id="homeContent" />
<fx:include source="About.fxml" fx:id="aboutContent" /> </fx:define>
<center>
<TabPane fx:id="tabpane" translateY="5" translateX="5" >
<tabs>
<Tab text="HOME" fx:id="homeTab" content="$homeContent" closable="false" />
<Tab text="ABOUT" fx:id="aboutTab" content="$aboutContent" closable="false"/>
</tabs>
</TabPane>
</center>
</Main>

You can see the highlighted lines of which helps to load the content of Home.fxml and About.fxml in the Main.fxml . Also we ‘ve added fx:id property for making instances available of Home and About class. Now let’s see the FXController class of Main.fxml

package fxmlstuff;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane; /**
* @author Narayan
*/ public class Main extends BorderPane implements Initializable{ @FXML
private Home homeContent;
@FXML
private About aboutContent; @Override
public void initialize(URL url, ResourceBundle rb) {
}
}

Here in the controller we have just made the instance of Home and About class with respective to their fx:id
Home.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import fxmlstuff.Home?>
<Home id="homeContent" xmlns:fx="http://javafx.com/fxml"
spacing="10" translateY="40" translateX="20" fx:controller="fxmlstuff.Home" >
<children>
<Label text="Add New Dock of Home" />
<Button text="Add !" onAction="#handleAction" />
</children>
</Home>

Home.java

package fxmlstuff;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.VBox; /**
* @author Narayan
*/
public class Home extends VBox implements Initializable{ @FXML
private void handleAction(ActionEvent event) {
} @Override
public void initialize(URL url, ResourceBundle rb) {
} }

About.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import fxmlstuff.About?>
<About id="aboutContent" xmlns:fx="http://javafx.com/fxml"
spacing="10" translateY="40" translateX="20" fx:controller="fxmlstuff.About">
<children>
<Label text="Add New Dock of About" />
<Button text="Add !" onAction="#handleButtonAction" />
</children>
</About>

About.java

package fxmlstuff;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.VBox; /**
* @author Narayan
*/
public class About extends VBox implements Initializable { @FXML
private void handleButtonAction(ActionEvent event) {
} @Override
public void initialize(URL url, ResourceBundle rb) {
} }

Now you have finally finished the FXML stuffs of adding components inside the FXML. You can now just create one FXML Executor class which helps to execute and load your FXML using FXMLLoader.load() . I’m going to use the FXMLTest class as the executor of the FXML files in Stage.
 
FXMLTest.java

package fxmlstuff;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage; /**
*
* @author Narayan
*/
public class FXMLTest extends Application{ public static void main(String[] args) {
Application.launch(args);
} @Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,800,600);
stage.setTitle("FXML Test");
stage.setScene(scene);
stage.show();
}
}

The preview of this javafx class will be something like this.

   

The continue of this blog will be posted in coming week. The next blog post will contain the extended part of this application where you can take control over the event triggered from About.fxml and Home.fxml FXML component to the Main fxml.
That’s it for today. You can now easily include the fxml file inside the FXML file using things like this.
Have a :)   good day and fell free on commenting your views about this post.
Thanks

FXML Stuffs (include and define)的更多相关文章

  1. 预处理命令[#define]说明

    宏定义 宏定义是对一些常见的变量.字符串等进行定义,被定义的数据在编译会进行自动替换.有时一些变量或字符串被多次使用,当需要修改时,就需要对源文件中它们出现的地方一一修改,效率比较低,而通过宏定义,只 ...

  2. #include <NOIP2008 Junior> 双栈排序 ——using namespace wxl;

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  3. #include <NOIP2009 Junior> 细胞分裂 ——using namespace wxl;

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...

  4. 编译预处理命令--define和ifdef的使用

    这里将对常用的预处理命令进行学习. 一.宏定义  ·defined 格式:`defined     宏名      数值 或者 `define      宏名 注意:后面没有‘;‘,和单片机不一样: ...

  5. 小心!#define max(a,b) a>b?a:b

    今天做oj的时候,定义了两个宏: //wrong code#define max_2(a,b) a>b?a:b #define max_3(a,b,c) (a>b?a:b)>c?(a ...

  6. c++ typedef和#define的作用范围

    typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...

  7. const和define的差别

    1.const有什么用途?(1)可以定义const常量(2)const可以修饰函数的参数和返回值,甚至函数的定义体.被const修饰的东西都受到强制保护,可以预防以外的变动,能提高程序的健壮性. in ...

  8. #include &lt;NOIP2008 Junior&gt; 双栈排序 ——using namespace wxl;

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  9. #include &lt;NOIP2009 Junior&gt; 细胞分裂 ——using namespace wxl;

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...

随机推荐

  1. centos下查看端口占用情况,杀死进程

    第一种:我们知道端口号用下面这种方法 有时候我们知道某个服务端口正在后台运行,想关掉它.比如说我tomcat是8080端口,在后台运行.怎么关掉它呢? 根据端口查看这个进程的pid netstat - ...

  2. 在 github 新建一个文件夹

    创建新文件的时候名字后面加个斜杠(/)就可以了.

  3. UVALive - 3266 Tian Ji -- The Horse Racing(贪心)

    题目链接 题意 两人赛马,每居获胜得200,平局无事发生,输了也输200.求最优的策略使赢的钱最多. 分析 排序,从最快的开始比,若比不过,则用最弱的消耗最强的.模拟即可. #include<i ...

  4. Oracle——存储过程简单入门实例

    1.连接plsql developer,打开一个SQL Window 2.SQL Window中创建表user_info -- Create table create table USER_INFO ...

  5. js 布局转换问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JavaJavaScript之内存与变量初始化

    0.搞清三个概念:预加载与执行期:js变量存储(栈区与堆区):js变量的类型(引用类型(对象)与基本数据类型); JS在预编译时,对于函数的预加载方面,浏览器仅仅选择编译声明式函数(function ...

  7. Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0

    1 启动hbase的时候爆出警告 Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; suppor ...

  8. MySQL事务隔离级别以及验证

    事务的并发执行,容易出现的几个现象 --------------------------     1.脏读         读未提交,一个事务读取了另外一个事务改写还没有提交的数据,如果另外一个    ...

  9. 【转】ReactNative&weex&DeviceOne对比

    React Native出来有一段时间了,国内的weex和deviceone是近期发布的,我可以说从2011年就开始关注快速开发的跨平台平台技术了,接触过phoneGap.数字天堂.appcan等早期 ...

  10. 消息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ 9节课

    1.JMS介绍和使用场景及基础编程模型     简介:讲解什么是小写队列,JMS的基础知识和使用场景     1.什么是JMS: Java消息服务(Java Message Service),Java ...