I defined the following FirstWindow.fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="switcherContainer" layoutX="0.0" layoutY="0.0" prefHeight="170.0" prefWidth="200.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="rep.ButtonController">
<fx:define>
<ToggleGroup fx:id="rType"/>
</fx:define>
<Label layoutX="35.0" layoutY="25.0" prefHeight="17.0" prefWidth="260.0" text="Choose the report type:" />
<RadioButton layoutX="35.0" layoutY="60.0" mnemonicParsing="false" text="A" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="80.0" mnemonicParsing="false" text="B" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="100.0" mnemonicParsing="false" text="C" toggleGroup="$rType" />
<RadioButton layoutX="35.0" layoutY="120.0" mnemonicParsing="false" text="D" toggleGroup="$rType" />
</AnchorPane>
I added this FirstWindow.fxml file in Main class:
AnchorPane rootPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/RootScene.fxml"));
primaryStage.setTitle("Report Generation");
primaryStage.setScene(new Scene(rootPane));
AnchorPane innerPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/FirstWindow.fxml"));
rootPane.getChildren().addAll(innerPane);
primaryStage.show();
And my ButtonController class looks like this:
public class ButtonController implements Initializable {
static int step = 0;
#FXML
private Button nextButton;
#FXML
ToggleGroup rType;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void onNextButtonClick(ActionEvent event) {
System.out.println("Button Clicked!");
try {
if (rType != null) {
System.out.println("RadioButton selected: " + rType.getSelectedToggle().getUserData().toString());
} else {
System.out.println("rType is null");
}
...
primaryStage.show();
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
}
}
But I always get the following output in the console:
rType is null
Why? And how to fix it?
Related
Hello I Have this portion of my code, I actually need disable the button when the user is logging, I am trying to pass the button to the FXML, but nothing happens: this is the code in the Main Controller, when the user is logged and data match with the password and username the button with the variable btn1 must disabled, I post my entire code for any help.
public Button getBtn1() {
return btn1;
}
public void conexion () {
String usus="";
String Passu ="";
String Bd="jdbc:sqlserver: // THUMANO2:1433;databaseName=QUORA";
String Usuario="sa";
String Pass="milkas87";
String SqlQuery= "select NOMBREUSUARIO, CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS))) as PASS from usuarios where CONVERT (VARCHAR(50), (DecryptByPassPhrase('*xc/6789oÑ---+y',PASS)))='"+fcontrasena.getText().toString().trim()+"'";
Connection Conexion = null;
try {
Conexion=DriverManager.getConnection(Bd, Usuario, Pass);
PreparedStatement ps =Conexion.prepareStatement(SqlQuery);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
usus = rs.getString("NOMBREUSUARIO");
Passu = rs.getString("PASS");
}
if(fcontrasena.getText().toString().trim().equals(Passu) && fusuario.getText().toString().equals(usus)) {
Stage administrador=new Stage();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Admin.fxml"));
Stage login=(Stage)fusuario.getScene().getWindow();
Parent root = loader.load();
AdminScreenController controlador = loader.<AdminScreenController>getController();
controlador.setBtn1(btn1);
Scene scene=new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
administrador.setScene(scene);
administrador.setTitle("AdminScreen");
login.hide();
administrador.show();
} catch(Exception e) {}
}
} catch(SQLException e) {
JOptionPane.showMessageDialog(null,"error","ERROR",0);
}
}
this is the code for AdminScreenController:
public void setBtn1(Button btn1) {
btn1.setDisable(true);
}
this is the FXML Document:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="576.0" prefWidth="791.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AdminScreenController">
<children>
<BorderPane fx:id="bpane" prefHeight="621.0" prefWidth="791.0">
<left>
<VBox fx:id="vboxr" prefHeight="576.0" prefWidth="205.0" BorderPane.alignment="CENTER">
<children>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn1" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Planilla Sistemas" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn2" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="AMP" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="116.0" prefWidth="205.0">
<center>
<Button fx:id="btn3" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Tareas Funcionarios" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0">
<center>
<Button fx:id="btn4" mnemonicParsing="false" prefHeight="51.0" prefWidth="127.0" text="Indicadores" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<BorderPane prefHeight="106.0" prefWidth="205.0" />
</children>
</VBox>
</left>
<top>
<BorderPane prefHeight="145.0" prefWidth="791.0" BorderPane.alignment="CENTER">
<left>
<Pane fx:id="imgview" prefHeight="145.0" prefWidth="205.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>
</top>
</BorderPane>
</children>
</StackPane>
its something wrong? i need some orientation here. thanks.
Here is an example that shows the MVC relationship in JavaFX. The button is defined in FXML and injected into the controller where it can be accessed.
TestAppView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.app.TestAppController">
<Button fx:id="myButton" text="Hello World" />
</VBox>
TestAppController.java
public class TestAppController implements Initializable {
#FXML
private Button myButton;
#Override
public void initialize(URL url, ResourceBundle resources) {
// Prints Hello World & disables the button when clicked
myButton.setOnAction((e) -> {
System.out.println("Hello World");
myButton.setDisable(true);
});
}
}
TestAppMain.java
public class TestAppMain extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(TestAppController.class.getResource("TestAppView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 200, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Pagination?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<Pane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.medplus.posoffline.gui.ReturnsPickListController">
<children>
<Pane prefHeight="600.0" prefWidth="800.0">
<children>
<Label layoutX="5.0" layoutY="15.0" text="RequestId" />
<TextField fx:id="requestId" layoutX="75.0" layoutY="15.0" prefWidth="140.0" />
<Label layoutX="245.0" layoutY="15.0" text="Status" />
<ComboBox fx:id="returnStatusComb" layoutX="295.0" layoutY="15.0" prefWidth="140.0">
</ComboBox>
<Label layoutX="470.0" layoutY="15.0" text="Type" />
<ComboBox fx:id="returnTypeComb" layoutX="510.0" layoutY="15.0" prefWidth="140.0">
</ComboBox>
<Button layoutX="598.0" layoutY="60.0" mnemonicParsing="false" onAction="#searcReturnDetailsfroPickList" text="Search" />
<TableView fx:id="tableView" layoutY="95.0" prefHeight="450.0" prefWidth="800.0">
<columns>
<TableColumn fx:id="requestIdsBtn" minWidth="200.0" prefWidth="75.0" text="RequestID" />
<TableColumn fx:id="status" minWidth="200.0" prefWidth="75.0" text="Status" />
<TableColumn fx:id="type" minWidth="200.0" prefWidth="75.0" text="Type" />
<TableColumn fx:id="dateCreated" minWidth="200.0" prefWidth="75.0" text="DateCreated" />
</columns>
</TableView>
<Pagination fx:id="returnsScreenpagination" layoutX="529.0" layoutY="394.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</Pane>
</children>
</Pane>
Controller
#Override
public void initialize(URL location, ResourceBundle resources) {
setCombsValues();
ReturnRequestSearchCriteria returnRequestSearchCriteria = new ReturnRequestSearchCriteria();
returnRequestSearchCriteria.setDetailsRequired(false);
try {
returnRequests = transferUtility.getStoreReturnDetails(returnRequestSearchCriteria);
} catch (TransferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnsScreenpagination = new Pagination(5,0);
returnsScreenpagination.setPageFactory((Integer pageIndex) -> {
System.out.println(pageIndex);
if (UtilValidate.isEmpty(returnRequests)) {
return null;
} else {
return createPage(pageIndex,returnRequests);
}
});
public TableView<ReturnRequest> createPage(int pageIndex,List<ReturnRequest> returnRequests) {
status.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestType>("status"));
type.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestStatus>("type"));
dateCreated.setCellValueFactory(new PropertyValueFactory<ReturnRequest, Date>("dateCreated"));
requestIdsBtn.setCellValueFactory(new PropertyValueFactory<ReturnRequest, String>("requestId"));
requestIdsBtn.setSortable(false);
dateCreated.setCellFactory(column -> {
TableCell<ReturnRequest, Date> cell = new TableCell<ReturnRequest, Date>() {
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
#Override
protected void updateItem(Date item, boolean empty) {
super.updateItem(item, empty);
if(empty) {
setText(null);
}
else {
this.setText(format.format(item));
}
}
};
return cell;
});
requestIdsBtn.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<ReturnRequest, String>,
ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<ReturnRequest, String> param) {
ObservableValue<String> obsLong = new SimpleLongProperty(param.getValue().getRequestId()).asString();
return obsLong;
}
});
requestIdsBtn.setCellFactory(
new Callback<TableColumn<ReturnRequest, String>, TableCell<ReturnRequest, String>>() {
#Override
public TableCell<ReturnRequest, String> call(TableColumn<ReturnRequest, String> p) {
return new ButtonCell();
}
});
List<ReturnRequest> sublist = returnRequests.subList(0, 5);
System.out.println(sublist);
observableProductList.addAll(sublist);
tableView.setItems( observableProductList);
return tableView;
}
I Am not able to get any data in the TableView. In call back method ...print which I kept, it's not getting printed.
Please help in getting data in a table according to my pagination.
Is there any other way to display pagination in JavaFX including FXML file
when pagination call back method is called?
Thanks in advance
I'm working on the project which contains multiple controllers (1 main and two for each of the Fxml files). Unfortunately while running the program, console throw the NullPointerExeption at me. I found out that it's the main controller fault, but still having this information I'm unable to fix this. Could you give me some tips how to solve this issue ?
Here i got the full track:
MainController class:
public class MainController {
#FXML LogInController logInController;
DBConnect dbConnect;
#FXML FlightControlController flightControlController;
#FXML public void initialize() {
logInController.initialize(this);
flightControlController.initialize(this);
dbConnect.initialize(this);
}
}
DBConnect class:
public void dbConnect() throws SQLException {
try {
Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nasa", "root", "1234");
} catch(Exception e) {
System.out.println(e.getStackTrace());
}
}
public boolean isCorrect(String login,String password) throws SQLException, IOException {
Stmt = Conn.createStatement();
Rs = Stmt.executeQuery("SELECT * FROM Users WHERE login='"+login+"'AND password='"+password+"';");
if(Rs.next()) {
return true;
} else {
return false;
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
LogInController class:
MainController mainController;
#FXML DBConnect dbConnect;
#FXML TextField logginField = null;
#FXML PasswordField passwordFiled=null;
#FXML Button logInButton;
#FXML
public void buttonClicked(ActionEvent event) throws SQLException, IOException {
mainController.dbConnect.dbConnect();
if(mainController.dbConnect.isCorrect(logginField.getText(),passwordFiled.getText())) {
Parent root = FXMLLoader.load(getClass().getResource("/view/FlightControlView.fxml"));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.show();
} else {
System.out.print("An error have occured");
}
}
public void initialize(MainController mainController) {
this.mainController=mainController;
}
FlightControlController Class:
#FXML Label label;
MainController mainController;
public void initialize(MainController mainController) {
this.mainController = mainController;
}
And the error which occurs:
Caused by: java.lang.NullPointerException at
controller.LogInController.buttonClicked(LogInController.java:31) ...
62 more
I'm not sure but is it possible that MainController (which doesn't have his own Fxml file) so that FXMLLoader doesn't initialize MainController's properties?
#EDIT
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="693.0" prefWidth="1062.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LogInController">
<children>
<SplitPane dividerPositions="0.21320754716981133" layoutX="331.0" layoutY="220.0" prefHeight="693.0" prefWidth="1062.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label alignment="CENTER" layoutY="-1.0" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="222.0" text="NASA DATABASE" />
<TextField fx:id="logginField" layoutX="18.0" layoutY="101.0" promptText="Login" />
<PasswordField fx:id="passwordFiled" layoutX="18.0" layoutY="165.0" promptText="Password" />
<Button fx:id="logInButton" onAction="#buttonClicked" layoutX="79.0" layoutY="230.0" mnemonicParsing="false" text="Log In" />
</children>
</AnchorPane>
<AnchorPane prefHeight="691.0" prefWidth="493.0" SplitPane.resizableWithParent="false">
<children>
<Label alignment="CENTER" layoutX="97.0" layoutY="104.0" prefHeight="417.0" prefWidth="635.0" text="NOT AVAILABLE, PLEASE LOG IN" AnchorPane.leftAnchor="113.0" AnchorPane.rightAnchor="113.0">
<font>
<Font name="System Bold Italic" size="37.0" />
</font>
</Label>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
I got a question about passing a selected date from DatePicker to another FXML file. I’ve seen code in similar threads but I can’t seem to get my head around it.
Basically I want to select a date from DatePicker (in DiscountController) and be able to retrieve that selected date in another FXML file(in ConfirmController) and print it. Here is what I go so far.
Can anyone tell what I’m doing wrong/missing here?
public class DiscountController implements Initializable {
private final static DiscountController instance = new DiscountController();
public static DiscountController getInstance() {
return instance;
}
#FXML private DatePicker dp;
#FXML private Label lblDate;
public DatePicker getDp() {
return dp;
}
//button to print the selected date in a label. This works fine
#FXML private void ConfirmBtnAction (ActionEvent event) {
lblDate.setText(dp.getValue().toString());
}
//button to move to next FXML screen
#FXML private void nextBtnAction(ActionEvent event) {
try{
Parent parent = FXMLLoader.load(getClass().getResource("/appl/Confirm.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
the fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.DiscountController">
<children>
<Label contentDisplay="LEFT" layoutX="236.0" layoutY="33.0" prefHeight="42.0" prefWidth="133.0" text="Discount">
<font>
<Font size="28.0" />
</font>
</Label>
<Button fx:id="btnNext" layoutX="525.0" layoutY="358.0" mnemonicParsing="false" onAction="#nextBtnAction" prefWidth="61.0" text="Next" />
<Label layoutX="231.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Select Date" />
<Label layoutX="404.0" layoutY="158.0" prefHeight="21.0" prefWidth="101.0" text="Select discount %" />
<Label layoutX="54.0" layoutY="158.0" prefHeight="21.0" prefWidth="69.0" text="Product" />
<DatePicker fx:id="dp" layoutX="189.0" layoutY="117.0" />
<Label fx:id="lblDate" layoutX="219.0" layoutY="247.0" prefHeight="35.0" prefWidth="95.0" />
<Button fx:id="btnConfirm" layoutX="241.0" layoutY="200.0" mnemonicParsing="false" onAction="#ConfirmBtnAction" text="Confirm" />
<TextField layoutX="391.0" layoutY="117.0" />
<Label fx:id="lblProduct" layoutX="22.0" layoutY="121.0" prefHeight="17.0" prefWidth="149.0" />
</children>
</AnchorPane>
ConfirmController file
public class ConfirmController implements Initializable {
//button to fetch selected date from DiscountController and print it
//Having a problem, here, this one doesn't work
#FXML private void btnFetchAction (ActionEvent event) {
String A;
A = DiscountController.getInstance().getDp().getValue().toString();
System.out.println(A);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
and the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ivikappl.ConfirmController">
<children>
<Button layoutX="382.0" layoutY="74.0" mnemonicParsing="false" prefHeight="49.0" prefWidth="115.0" text="Generate barcode" textAlignment="CENTER" />
<Label layoutX="91.0" layoutY="31.0" prefHeight="30.0" prefWidth="79.0" text="Summary">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label layoutX="40.0" layoutY="123.0" prefHeight="20.0" prefWidth="85.0" text="Productname">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Label fx:id="lblDate" layoutX="40.0" layoutY="79.0" prefHeight="20.0" prefWidth="67.0">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Label layoutX="40.0" layoutY="166.0" prefHeight="20.0" prefWidth="67.0" text="Discount">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
<Button fx:id="btnFetch" layoutX="40.0" layoutY="236.0" mnemonicParsing="false" onAction="#btnFetchAction" text="Fetch" />
</children>
</AnchorPane>
You can't use the singleton pattern here because the FXMLLoader essentially creates an instance of DiscountController by calling the no-argument constructor (i.e. not by calling DiscountController.getInstance()). (You could go this route if you really wanted, by setting a controller factory on the FXMLLoader that loaded the discount FXML file, but it really doesn't make sense for controllers to be singletons. In many cases you really need multiple instances of them. And there are much simpler ways to achieve what you are trying to do anyway.)
Define a method in ConfirmController to which you can pass the date:
public class ConfirmController {
#FXML
private Label lblDate ;
public void setDate(LocalDate date) {
lblDate.setText(date.toString());
}
// ... other code as before...
}
and then just call it when you load the FXML:
public class DiscountController implements Initializable {
#FXML private DatePicker dp;
// ...
#FXML private void nextBtnAction(ActionEvent event) {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/appl/Confirm.fxml"));
Parent parent = loader.load();
ConfirmController confirmController = loader.getController();
confirmController.setDate(dp.getValue());
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
catch(IOException e){
}
}
// ...
}
See Passing Parameters JavaFX FXML for a bunch of other ways to do this.
I try to add a TreeTableView which i have created by coding to right side of my splitpane. The SplitPane has fix Id: splitId and right side of it has the Id splitidRight. I have created splitpane and added it to anchorepane by scenebuiler. Then I have crated BorderPane with menubar. Then i add splitpane to the center of BorderPane. Now I want to add the treetable to the rightside of splitpane. The Error is javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.control.SplitPane but beside this error i am not sure if i am doing the insertion at the right place.
public class Main extends Application {
private DataConstructor dc = new DataConstructor();
private BorderPane rootLayout;
private Stage primaryStage;
TreeItem<String> root = new TreeItem<>("Functions");
public static void main(String[] args) {
Application.launch(Main.class, args);
}
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
primaryStage.setTitle("IT-Saturation");
initRootLayout();
showOverViw();
makeTreeTable();
}
private void makeTreeTable() {
Scene scene = new Scene(new Pane(), 1200, 1800);
Pane sceneRoot = (Pane) scene.getRoot();
root.setExpanded(true);
//........make treetable.....//
treeTableView.setPrefWidth(1200);
treeTableView.setShowRoot(false);
treeTableView.setTableMenuButtonVisible(true);
sceneRoot.getChildren().add(treeTableView);
SplitPane sp = null;
try {
sp = FXMLLoader.load(getClass().getResource("/view/OverView.fxml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StackPane container = new StackPane();
container.getChildren().add(treeTableView);
sp.getItems().add(container);
sp.setDividerPositions(0.3f, 0.6f, 0.9f); // you can tweak it any how
// primaryStage.setScene(scene);
// primaryStage.show();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the overview inside the root layout.
*/
public void showOverViw() {
try {
// Load overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
AnchorPane overView = (AnchorPane) loader.load();
// Set overview into the center of root layout.
rootLayout.setCenter(overView);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
*
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
This is the rootLayout.fxml (BorderPane)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
and this is OverView.fxml with splitpane in it:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane fx:id="splitId" dividerPositions="0.14691151919866444" layoutX="58.0" layoutY="42.0" prefHeight="700.0" prefWidth="1200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<Label layoutX="23.0" layoutY="173.0" text="Function count" />
<TextField editable="false" layoutX="1.0" layoutY="225.0" promptText="FuncCount" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" />
<Label layoutX="20.0" layoutY="315.0" text="Organization count" />
<TextField editable="false" layoutX="1.0" layoutY="366.0" promptText="OrgCount" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" />
<ToggleButton layoutX="30.0" layoutY="487.0" mnemonicParsing="false" text="Draw/Reset" />
</children>
</AnchorPane>
<AnchorPane fx:id="splitIdRight" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
As the error says you're trying to cast the AnchorPane which is the top root node of your OverView.fxml to SplitPane.
Change your line
SplitPane = null;
to
AnchorPane =null;
that should solve your error.