是否可以将包含自定义JavaFX控件的JAR导入Scene Builder?

当我注意到我几乎完全有同样的事情三次时,我正在一个舞台上工作。 而不是那个(因为我讨厌那个),我决定把我拥有的那些3次并把它变成一个自定义组件。

现在我知道我可以在代码中添加它但我无法预测布局行为(其中两个将直接进入选项卡,第三个将进入网格窗格)。

我尝试将包含控件的.jar导入Scene Builder。 弹出一个对话框,询问我想要导入的JAR中的内容,但它完全是空的。

我之前看到有些人添加了“自定义控件”(我松散使用的术语),但发现基本上它只是将一堆组件转储到一起形成控件。 这可能适用于某些人,但我不是在寻找。 澄清

我想要的是什么(我是否要查看FXML代码):

 

我不想要的:

   //bla bla column constraint stuff  //etc, etc  

这可能吗? 我再次离开C#和VS2010而且在自定义控件方面我有点被宠坏了所以如果有可能做出类似的事情,有人可以告诉我怎么样?

是否可以将包含自定义JavaFX控件的已编译JAR文件导入到Scene Builder中,以便将其从库中删除将导致我在那里描述的内容?

编辑1好的,这就是我在FXML文件中的内容。 根据mlody991,我需要3个文件来完成这项工作:FXML文件(使用SceneBuilder构建):

                 

Java类文件(代表代码中的实际对象):

 package DGCSDefiner; import java.io.IOException; import static java.util.Arrays.asList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.ColorPicker; import javafx.scene.control.ComboBox; import javafx.scene.control.Slider; import javafx.scene.control.TabPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; public class DGCSDefiner extends Pane { //DigiGames Color Settings Definer. // Values injected by FXMLLoader // // @FXML private GridPane gpLinearSettings; // fx:id="gpLinearSettings" @FXML private StackPane spSettings; // fx:id="spSettings" @FXML private TabPane tabsRadialSettings; // fx:id="tabRadialSettings" // // @FXML private ColorPicker cpSolidColor, // fx:id="cpSolidColor" cpFirstLinearColor, // fx:id="cpFirstLinearColor" cpSecondLinearColor, // fx:id="cpSecondLinearColor" cpFirstRadialColor, // fx:id="cpFirstRadialColor" cpSecondRadialColor; // fx:id="cpSecondRadialColor" // // @FXML private ComboBox cbxColorStyle; // fx:id="cbxColorStyle" @FXML private ComboBox cbxLinearAngle; // fx:id="cbxLinearAngle" // // @FXMLprivate Slider sliderRadius, // fx:id="sliderRadius" sliderHPos, // fx:id="sliderHPos" sliderVPos; // fx:id="sliderVPos" // // private FXMLLoader Loader; @FXML // This method is called by the FXMLLoader when initialization is complete void initialize() { // // assert this.gpLinearSettings != null : "fx:id=\"gpLinearSettings\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.spSettings != null : "fx:id=\"spSettings\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.tabsRadialSettings != null : "fx:id=\"tabsRadialSettings\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; // // assert this.cpSolidColor != null : "fx:id=\"cpSolidColor\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.cpFirstLinearColor != null : "fx:id=\"cpFirstLinearColor\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.cpSecondLinearColor != null : "fx:id=\"cpSecondLinearColor\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.cpFirstRadialColor != null : "fx:id=\"cpFirstRadialColor\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.cpSecondRadialColor != null : "fx:id=\"cpSecondRadialColor\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; // // assert this.cbxColorStyle != null : "fx:id=\"cbxColorStyle\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.cbxLinearAngle != null : "fx:id=\"cbxLinearAngle\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; // // assert this.sliderRadius != null : "fx:id=\"sliderRadius\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.sliderHPos != null : "fx:id=\"sliderHPos\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; assert this.sliderVPos != null : "fx:id=\"sliderVPos\" was not injected: check your FXML file 'JFXMLColorStyleDefiner.fxml'."; // // // // this.cbxColorStyle.getItems().addAll( asList(ColorStyles.values()) ); for (int x = 0; x  { ColorStyles CS = this.cbxColorStyle.getValue(); this.cpSolidColor.setVisible(CS == ColorStyles.SOLID); this.gpLinearSettings.setVisible(CS == ColorStyles.LINEAR); this.tabsRadialSettings.setVisible(CS == ColorStyles.RADIAL); }); // this.Loader = new FXMLLoader( this.getClass().getResource("DGCSDefiner.fxml") ); this.Loader.setRoot(this); this.Loader.setController(this); // } public DGCSDefiner(){ try{ this.Loader.load(); } catch(IOException e){ throw new RuntimeException(e); } } /** * Get the ColorSettings defined by the control. * @return Defined Color Settings. */ public ColorSettings getColorSettings(){ if (this.cbxColorStyle.getSelectionModel().getSelectedIndex() < 0) return null; switch(this.cbxColorStyle.getValue()){ case SOLID: return new ColorSettings(this.cpSolidColor.getValue()); case LINEAR: return new ColorSettings( this.cpFirstLinearColor.getValue(), this.cpSecondLinearColor.getValue(), this.cbxLinearAngle.getValue() ); case RADIAL: return new ColorSettings( this.cpFirstRadialColor.getValue(), this.cpSecondRadialColor.getValue(), (int)this.sliderRadius.getValue(), (int)this.sliderHPos.getValue(), (int)this.sliderVPos.getValue() ); } return null; //This should never happen. } /** * Load defined color settings. * @param cs Predefined color settings. */ public void setColorSettings(ColorSettings cs){ this.cbxColorStyle.setValue(cs.Style); switch(cs.Style){ case SOLID: this.cpSolidColor.setValue(cs.clrPrimary); break; case LINEAR: this.cbxLinearAngle.setValue(cs.intAngle); this.cpFirstLinearColor.setValue(cs.clrPrimary); this.cpSecondLinearColor.setValue(cs.clrSecondary); break; case RADIAL: this.sliderRadius.setValue(cs.intSize); this.sliderHPos.setValue(cs.intHPos); this.sliderVPos.setValue(cs.intVPos); this.cpFirstRadialColor.setValue(cs.clrPrimary); this.cpSecondRadialColor.setValue(cs.clrSecondary); } } } 

然后是最后一个文件,其目的不包括我:

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package DGCSDefiner; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.Initializable; /** * * @author Will */ public class DGCSDefinerController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { } } 

有人可以向我解释这个最后文件的目的吗? 它的function是什么? 我可以从我提供的示例代码中看到它已被设置为FXML控制器,但仍然没有告诉我什么。 我之前从未真正设置过FXML控制器,因为我通常在代码中执行此操作,因此明确的目的是将此控件导入到Scene Builder中吗?

编辑2好的。 我有预感,事实certificate这是正确的,但没有用。 我删除了控件CSS样式表引用,它允许我添加控件,没问题。

但是,当我放弃它时,FXML文件中发生了什么:

它从此开始(仅显示相关部分):

  

对此:

              

这正是我不想要的。

我希望看到的是这样的效果:

      

在我将自定义控件拖放到设计器中之后的FXML代码中。 那可能吗? 我需要将它编译成jar吗?

编辑3为了使它更清晰, 这正是我想要看到的。 这看起来不错,但问题是没有关于如何将自定义控件导入到Scene Builder中的信息,这样当我将它拖放到canvas上时,我只能在一行代码的某处附近,而不是书和一半它的写作(这是愚蠢的。如果我想,我可以(并且会)自己做)。

你不需要.jar文件来做到这一点。 你可以简单地用你需要的东西创建新的FXML文件。 combobox。 使用扩展combobox,控制器创建类文件,并在FXML文件中添加控制器(在场景构建器中)。

在场景构建器中靠近左侧搜索框的combobox中找到“从JAR / FXML文件导入”,然后选择该文件。 现在左手风琴的新标题窗格名为Custom。 在那里你可以找到你的组件。

@edit有文件。

MyGridPane.fxml

               

MyGridPane.java

 package MyGridPane; import javafx.fxml.FXMLLoader; import java.io.IOException; /** * Created by Marcin on 2014-09-01. */ public class MyGridPane { MyGridPane(){ FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MyGridPane.fxml")); fxmlLoader.setRoot(this); fxmlLoader.setController(this); try { fxmlLoader.load(); } catch (IOException exception) { throw new RuntimeException(exception); } } } 

MyGridPaneController.java

 package MyGridPane; import javafx.fxml.Initializable; import java.net.URL; import java.util.ResourceBundle; /** * Created by Marcin on 2014-09-01. */ public class MyGridPaneController implements Initializable{ @Override public void initialize(URL location, ResourceBundle resources) { } } 

下一步是添加到Scene Builder

在此处输入图像描述

你可以添加这个组件。

在此处输入图像描述