Is it possible to import a JAR containing a custom JavaFX control into Scene Builder?

前端 未结 1 1778
陌清茗
陌清茗 2021-01-13 11:58

I was working on a stage when I noticed I practically had the exact same thing three times. Rather than that (since I hate that), I decided to take what I had those 3 times

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 12:23

    You do not need .jar file to do that. You can simply create new FXML file with that what you need ex. combo box. Create class file with extending combo box, controller and add controller in FXML file (in scene builder).

    Find "Import from JAR/FXML file" in combo box near to left search box in scene builder and then select the file. Now you have new title pane in left accordion with name Custom. There you can find your components.

    @edit There are files.

    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) {
    
        }
    }
    

    Next step is add to Scene Builder

    enter image description here

    and you can add this component.

    enter image description here

    0 讨论(0)
提交回复
热议问题