Accessing controls of a controller from another controller in javafx - java

I am building an simple javafx application using afterburner. I have main window which have a border pane and it's upper part contains menus and in center it have a vertical split pane which have two anchor panes with fx:id=inputPane and fx:id=tablePane. Now in inputPane i am loading different scenes and submitting data from it into tablePane.
Now i have a scene opened in inputPane which have a button and i want to submit the data from this scene into table as well as i want to open an another scene into inputPane and again want to submit it's data into table.
adara.fxml
<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="180.0" minWidth="-Infinity" prefHeight="507.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.qaf.presentation.adara.AdaraPresenter">
<top>
<Pane prefHeight="26.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<MenuBar fx:id="menuBar" prefHeight="25.0" prefWidth="800.0">
<menus>
<Menu mnemonicParsing="false" text="Stock">
<items>
<MenuItem fx:id="createStock" mnemonicParsing="false" onAction="#showCreateStockForm" text="Create" />
<MenuItem fx:id="incoming" mnemonicParsing="false" onAction="#showIncomingForm" text="Incoming" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</Pane>
</top>
<center>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="469.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<items>
<BorderPane fx:id="contentBorderPane" prefHeight="200.0" prefWidth="200.0">
<center>
<AnchorPane fx:id="contentBox" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
<AnchorPane fx:id="tableBox" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
</items>
</SplitPane>
</center>
</BorderPane>
adaraView.java
public class AdaraView extends FXMLView {
}
adaraPresenter.java
public class AdaraPresenter implements Initializable {
#FXML
AnchorPane contentBox;
#FXML
AnchorPane tableBox;
StockCreationPresenter stockCreationPresenter;
InwardsPresenter inwardsPresenter;
ViewStockPresenter viewStockPresenter;
#Override
public void initialize(URL location, ResourceBundle resources) {
StockCreationView scView = new StockCreationView();
// other codes
.
.
ViewStockView vsView = new ViewStockView();
// other codes
.
.
contentBox.getChildren().add(scView.getView());
tableBox.getChildren().add(vsView.getView());
}
public void showCreateStockForm(){
StockCreationView scView = new StockCreationView();
// other codes
.
.
ViewStockView vsView = new ViewStockView();
// other codes
.
.
contentBox.getChildren().add(scView.getView());
tableBox.getChildren().add(vsView.getView());
}
public void showIncomingForm(){
InwardsView inView = new InwardsView();
// other codes
.
.
ViewStockView vsView = new ViewStockView();
// other codes
.
.
contentBox.getChildren().add(inView.getView());
tableBox.getChildren().add(vsView.getView());
}
}
Here is the main class which is starting the stage and application.
Main.java
public class App extends Application
{
#Override
public void start(Stage stage) throws Exception {
AdaraView appView = new AdaraView();
Scene scene = new Scene(appView.getView());
stage.setTitle("Adara");
final String uri = getClass().getResource("app.css").toExternalForm();
scene.getStylesheets().add(uri);
stage.setScene(scene);
stage.show();
}
#Override
public void stop() throws Exception {
Injector.forgetAll();
}
public static void main(String[] args) {
launch(args);
}
}
Now here is the InwardsPresenter.java which have to save the stockCreationPresenter.java data into table as well as it have to open the inwardsView into the contentPane.
InwardsPresenter.java
public class InwardsPresenter implements Initializable {
#FXML
Button saveButton;
#FXML
TextField orderNo;
#FXML
TextField other;
#FXML
CheckBox save;
#Inject
AdaraPresenter adaraPresenter;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void save() {
Inwards inward = newInward();
inward.setOrderNo(Integer.parseInt(orderNo.getText()));
inward.setOther(other.getText());
inward.setSave(save.isSelected());
this.newInward.set(inward);
adaraPresenter.incomingForm();
}
}
// while executing adaraPresenter.incomingForm(); it's showing NPE to contentBox and tableBox.
Thank you very much for any help.

Related

JavaFX TableView pagination - can not create fxml based solution

I am using JavaFX in my project (also Spring, Hibernate e.t.c.).
I am trying to create Pagination for TableView. I found many reasonable solutions, but all off them are controller-based solutions. The best of them in this solution.
But in my situation - I am using multiple fxml files which were created in SceneBuilder with all huge design options and which contains the TabPane and for one Tab I have one FXML-file and one controller for it.
I have one basic file which imports others, and in my Main class I am loading it:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="592.0" prefWidth="920.0" xmlns="http://javafx.com/javafx/8"
fx:controller="com.varinsia.statistical.controller.GenericController">
<children>
<TabPane layoutX="3.0" layoutY="50.0" prefHeight="542.0" prefWidth="914.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Statistics">
<content>
<fx:include fx:id="statistics" source="/fxml/child/statistics.fxml"/>
</content>
</Tab>
<Tab text="Shedule">
<content>
<fx:include fx:id="shedule" source="/fxml/child/shedule.fxml"/>
</content>
</Tab>
<Tab text="Ponab devices">
<content>
<fx:include fx:id="ponabDevices" source="/fxml/child/ponabDevices.fxml"/>
</content>
</Tab>
<Tab text="Als devices">
<content>
<fx:include fx:id="alsDevices" source="/fxml/child/alsDevices.fxml"/>
</content>
</Tab>
<Tab text="Search">
<content>
<fx:include fx:id="search" source="/fxml/child/search.fxml"/>
</content>
</Tab>
<Tab text="Settings">
<content>
<fx:include fx:id="settings" source="/fxml/child/settings.fxml"/>
</content>
</Tab>
</tabs>
</TabPane>
<Label layoutX="127.0" layoutY="9.0" text="..."
underline="true">
<font>
<Font size="18.0"/>
</font>
</Label>
</children>
</AnchorPane>
In my controller class for my statistics.fxml I am creating the TableView and columns:
#FXML
public TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> objectColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> noteColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> stageColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> dateColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> vagonColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, String> repeatColumn;
#FXML
public TableColumn<StatisticsRemarkTableDto, Integer> remarkIdColumn;
And all other things like in this solution.
Information from the entity is added to my table and everything works fine. But!
Problem starts when i am trying to add Pagination, because I can't understand how I should do this. My Main method looks like this:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring/application-context.xml");
context.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);
primaryStage.setTitle(Constants.MAIN_TITLE);
primaryStage.setScene(new Scene((Parent) context.getBean(SpringFXMLLoader.class).load(Constants.FXML_PATH), 914, 542));
primaryStage.show();
}
}
In all TableView-pagination examples Pagination were added in the Main class, like in the code from other valid and checked example:
private Node createPage(int pageIndex) {
int fromIndex = pageIndex * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));
return new BorderPane(table);
}
#Override
public void start(final Stage stage) throws Exception {
Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
pagination.setPageFactory(this::createPage);
Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
stage.setScene(scene);
stage.setTitle("Table pager");
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
Is there some way to pass this issue and to add Pagination to my TableView?
I'll be very glad to any answers on this subject, the problem does not suffer the first day.
RESOLVED!:
Thanks to James_D and his solution.
I have tried this variant and it worked fine. My TableView is now with pagination.
I have added to my statistical.fxml:
<Pagination fx:id="statisticsTableViewPagination" layoutX="2.0" layoutY="188.0" prefHeight="275.0"
prefWidth="912.0">
<fx:define>
<TableView fx:id="statisticsTableView" layoutX="2.0" layoutY="188.0" prefHeight="301.0" prefWidth="912.0">
<placeholder>
<Label text="No search results found."/>
</placeholder>
<columns>
<TableColumn fx:id="objectColumn" prefWidth="131.0" text="Object"/>
<TableColumn fx:id="noteColumn" minWidth="0.0" prefWidth="167.0" text="Remark"/>
<TableColumn fx:id="stageColumn" prefWidth="282.0" text="Stage"/>
<TableColumn fx:id="dateColumn" prefWidth="72.0" text="Date"/>
<TableColumn fx:id="vagonColumn" prefWidth="133.0" text="Laboratory"/>
<TableColumn fx:id="repeatColumn" prefWidth="125.0" text="Repeats"/>
<TableColumn fx:id="remarkIdColumn" minWidth="9.0" prefWidth="15.0" text="Id" visible="false"/>
</columns>
</TableView>
</fx:define>
</Pagination>
And in my controller I have added the same parts as in this example. And everything is working fine. Thanks a lot!
Just define the Pagination in statistics.fxml:
<Pagination fx:id="pagination"/>
and then configure it in the controller for that class:
#FXML
private TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
private Pagination pagination ;
public void initialize() {
pagination.setPageFactory(this::createPage);
// etc...
}
Since the table itself is placed in the scene graph by the page factory, it isn't part of the scene graph until after the initialize() method is invoked, i.e. after the FXML has been parsed. So you can either just define the table in the controller directly (and not include it in the FXML file at all):
private TableView<StatisticsRemarkTableDto> statisticsTableView;
#FXML
private Pagination pagination ;
public void initialize() {
statisticsTableView = new TableView<>();
// create columns, etc...
pagination.setPageFactory(this::createPage);
// etc...
}
or you can define it in FXML in a <fx:define> block:
<Pagination fx:id="pagination">
<fx:define>
<TableView fx:id="statisticsTableView">
<columns>
<TableColumn fx:id="objectColumn" ... />
<!-- etc -->
</columns>
</TableView>
</fx:define>
</Pagination>
and then inject it into the controller with #FXML in the usual way.

JavaFXML include FXML change upper controller

I have a small question.
I have a Menu.FXML which has a Controller (MenuController).
Inside the Menu.FXML I Include another .FXMl (Inner.FXML) and it includes a Label.
The Inner.FXML has a MouseClick event handler, so when I Click the Inner.FXML it does something, I want my Inner.FXML mouse listener to change the text which is inside the Menu.FXML.
How can I do this?
Thank you very much.
[CODE]
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
InnerController:
public class InnerController implements Initializable {
public void buttonClick(ActionEvent event){
System.out.println("change label!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Menu FXML:
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
<children>
<Label text="Label" />
<fx:include source="Inner.fxml" />
</children>
</VBox>
InnerFXML.
<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: green;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.InnerController">
<children>
<Button layoutX="271.0" layoutY="187.0" mnemonicParsing="false" onAction="#buttonClick" text="Button" />
</children>
</Pane>
Create and observable string property in the InnerContoller and set it from the button's handler. Then observe the property from the outer controller.
public class InnerController {
private final ReadOnlyStringWrapper text = new ReadOnlyStringWrapper();
public ReadOnlyStringProperty textProperty() {
return text.getReadOnlyProperty() ;
}
public String getText() {
return text.get();
}
public void buttonClick(ActionEvent event){
System.out.println("change label!");
text.set("Hello world");
}
public void initialize() {
}
}
Then add an fx:id to your fx:include:
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nestedcontroller.MenuController">
<children>
<Label text="Label" fx:id="label" />
<fx:include source="Inner.fxml" fx:id="innerPane" />
</children>
</VBox>
And then in the MenuController just observe the property:
public class MenuController {
#FXML
private Label label ;
#FXML
private InnerController innerPaneController ;
public void initialize() {
innerPaneController.textProperty().addListener((obs, oldText, newText) ->
label.setText(newText));
// or just label.textProperty().bind(innerPaneController.textProperty());
}
}
See the FXML documentation for details.

Edit the values of the TableView from TextField

Hi I am trying in the spirit of the example in http://code.makery.ch/java/javafx-2-tutorial-intro/ to make a similar application where the TableView values can be edited by a series of TextFields instead of a popup form. The reason why I would wannt do it like that is that I am having many fields in a similar application I want to develop and I would like to avoid the user editing them on the TableView
The TableView shows a list of Persons where for each I record name, surname and country. I have tried to create a bidirectionalBinding between the name property of the Person bean and the textProperty() of the TextField but this doesn't work.
I also tried to add to the textProperty a ChangeListener so when it changes to update the ObservableList of propoerties and this also didn't work
Apparently I am doing something wrong and so far I have the following code:
FXDocumentController.java
public class FXMLDocumentController implements Initializable {
private static final Logger logger = Logger.getLogger(FXMLDocumentController.class.getName());
private ObservableList<Person> data;
#FXML
private TableView<Person> tableview;
#FXML
private TableColumn<Person, String> colName;
#FXML
private TableColumn<Person, String> colSurname;
#FXML
private TableColumn<Person, String> colCountry;
#FXML
private TextField name;
#Override
public void initialize(URL url, ResourceBundle rb) {
assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
colName.setCellValueFactory(
new PropertyValueFactory<Person,String>("name"));
colSurname.setCellValueFactory(
new PropertyValueFactory<Person,String>("surname"));
colCountry.setCellValueFactory(
new PropertyValueFactory<Person,String>("country"));
DBClass objDbClass = new DBClass();
try{
con = objDbClass.getConnection();
buildData();
tableview.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 1) {
Person p = tableview.getSelectionModel().getSelectedItem();
//name.textProperty().setValue(p.getName());
name.textProperty().bindBidirectional(p.name);
//name.textProperty().bind(p.name);
}
}
}
});
name.textProperty().addListener(new ChangeListener(){
#Override
public void changed(ObservableValue ov, Object t, Object t1) {
System.out.println("Value changed!");
/** Tried also this but this wont work
name.textProperty().setValue((String)t1);
int index = tableview.getSelectionModel().getSelectedIndex();
data.get(index).name.setValue( (String) t1);
tableview.setItems(data);
* */
}
}
);
}
catch(ClassNotFoundException ce){
logger.info(ce.toString());
}
catch(SQLException ce){
logger.info(ce.toString());
}
} //initialize
private void buildData() {
data = FXCollections.observableArrayList();
try{
data.add(new Person("Jon", "Doe", "USA"));
data.add(new Person("Lars", "Andersson", "Sweden"));
tableview.setItems(data);
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
Person.java
public class Person {
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty surname = new SimpleStringProperty();
public SimpleStringProperty country = new SimpleStringProperty();
public Person(String name, String surname, String country){
this.name.set(name);
this.surname.set(surname);
this.country.set(country);
}
public String getName(){
return name.get();
}
public String getSurname(){
return surname.get();
}
public String getCountry(){
return country.get();
}
}
And for the sake of completeness I am giving the fxml file (very ugly but I am experimenting with the functionality;)) and the launcher
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" blendMode="SRC_OVER" cache="false" disable="false" focusTraversable="false" prefHeight="576.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="javafxdemoproject.FXMLDocumentController">
<children>
<SplitPane dividerPositions="0.3336594911937378" focusTraversable="true" layoutX="0.0" layoutY="55.0" prefHeight="521.0" prefWidth="1024.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="410.0" prefWidth="297.0">
<children>
<TableView fx:id="tableview" editable="true" prefHeight="520.0" prefWidth="338.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="178.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="Name" fx:id="colName" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="154.0" text="Surname" fx:id="colSurname" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="112.0" text="Country" fx:id="colCountry" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="404.0" prefWidth="460.0">
<children>
<Accordion layoutX="0.0" layoutY="0.0" prefHeight="520.0" prefWidth="262.0">
<expandedPane>
<TitledPane fx:id="personalTp" animated="false" text="Personal Details">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="name" layoutX="44.0" layoutY="27.0" prefWidth="200.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</expandedPane>
<panes>
<fx:reference source="personalTp" />
<TitledPane fx:id="x2" animated="false" text="Positions held">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ListView prefHeight="621.0" prefWidth="454.0" AnchorPane.bottomAnchor="-2.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="74.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</children>
</AnchorPane>
</items>
</SplitPane>
<MenuBar layoutY="0.0" AnchorPane.rightAnchor="908.0">
<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>
<ToolBar layoutY="24.0" AnchorPane.leftAnchor="0.0">
<items>
<Button mnemonicParsing="false" text="Button" />
</items>
</ToolBar>
</children>
</AnchorPane>
JavaFXDemoProject.java
public class JavaFXDemoProject 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();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Questions
1) Is it at all possible what I am trying to do?
2) Why isn't this working with bidirectionalBinding
Any help appreciated
I changed your controller class like this
public void initialize(URL url, ResourceBundle rb) {
assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
colName.setCellValueFactory(
new PropertyValueFactory<Person,String>("name"));
colSurname.setCellValueFactory(
new PropertyValueFactory<Person,String>("surname"));
colCountry.setCellValueFactory(
new PropertyValueFactory<Person,String>("country"));
buildData();
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
#Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
if (oldValue !=null) name.textProperty().unbindBidirectional(oldValue.nameProperty());
if (newValue !=null) name.textProperty().bindBidirectional(newValue.nameProperty());
}
});
} //initialize
and your person class a bit
public class Person {
private StringProperty name = new SimpleStringProperty();
private StringProperty surname = new SimpleStringProperty();
private StringProperty country = new SimpleStringProperty();
public Person(String name, String surname, String country){
this.name.set(name);
this.surname.set(surname);
this.country.set(country);
}
public StringProperty nameProperty(){return name;}
public StringProperty surnameProperty(){return surname;}
public StringProperty countryProperty(){return country;}
}
Now the TextField can bind to the property in Person

Why member is not injected in JavaFX controller?

I have the following FXML:
<BorderPane fx:controller="mypackage.MainStage" id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<bottom>
<ToolBar>
<items>
<Slider id="slider" prefWidth="443.0" />
<Label text="Record:" />
<TextField prefWidth="46.0" />
<Label text=" of " />
<Label id="totalLabel" text="0" />
</items>
</ToolBar>
</bottom>
<center>
<Pane id="mainPane" prefHeight="200.0" prefWidth="200.0" />
</center>
<top>
<ToolBar>
<items>
<Button mnemonicParsing="false" text="Load more" />
</items>
</ToolBar>
</top>
</BorderPane>
and the following controller code:
public class MainStage implements Initializable {
#FXML
private Pane mainPane;
#FXML
private Label totalLabel;
#Override
public void initialize(URL location, ResourceBundle resources) {
totalLabel.setText("245");
}
}
and the following application code
public class MyClass extends Application
{
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainStage.fxml"));
Scene scene = new Scene(root);
stage.setTitle("MyClass");
stage.setScene(scene);
stage.show();
}
public static void main( String[] args )
{
launch(args);
}
}
If I set breakpoint to initialize() I see it is called, but member is null.
Why isn't it injected?
Because I used id while was to use fx:id tag.

JavaFX 2.0 FXML Child Windows

After much searching I found this question How to create a javafx 2.0 application MDI. What I really wanted to know is if I can create a pop-up window or child window to the main window using JavaFX components and Scene Builder to create the new window.
I ended up with this for a modal pop-up window:
In the Main class I wanted to save the primary stage to a field I can access from my primary controller class. So, I added a static variable Stage to it and this in the Main.Start() method:
primaryController.primaryStage = primaryStage;
This the method that a button in the primaryController uses:
public void OnBtnShowChild(ActionEvent event) {
MessageBoxController msgBox = new MessageBoxController();
try {
msgBox.showMessageBox(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
}
This is the MessageBoxController class that I created with help from Scene Builder. It has the basic layout of a standard pop-up box that can be used to display an Icon (ImageView), TextBox (for your message text), and two buttons (for YES/NO functionality). I am not sure yet how to have it communicate the results of what button was pressed back to the primaryController.
public class MessageBoxController implements Initializable {
#FXML
// fx:id="btnNo"
private Button btnNo; // Value injected by FXMLLoader
#FXML
// fx:id="btnYes"
private Button btnYes; // Value injected by FXMLLoader
#FXML
// fx:id="imgMessage"
private ImageView imgMessage; // Value injected by FXMLLoader
#FXML
// fx:id="txtMessage"
private TextField txtMessage; // Value injected by FXMLLoader
private Stage myParent;
private Stage messageBoxStage;
public void showMessageBox(Stage parentStage) {
this.myParent = parentStage;
try {
messageBoxStage = new Stage();
AnchorPane page = (AnchorPane) FXMLLoader.load(MessageBoxController.class.getResource("/MessageBox/MessageBoxFXML.fxml"));
Scene scene = new Scene(page);
messageBoxStage.setScene(scene);
messageBoxStage.setTitle("Message Box");
messageBoxStage.initOwner(this.myParent);
messageBoxStage.initModality(Modality.WINDOW_MODAL);
messageBoxStage.show();
} catch (Exception ex) {
System.out.println("Exception foundeth in showMessageBox");
ex.printStackTrace();
}
}
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle arg1) {
txtMessage.setText("Howdy");
}
public void OnBtnYes(ActionEvent event) {
}
public void OnBtnNo(ActionEvent event) {
}
}
And finally, this is the FXML file I created in Scene Builder:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="172.0" prefWidth="524.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MessageBox.MessageBoxController">
<children>
<VBox prefHeight="172.0" prefWidth="524.0" styleClass="vboxes" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER" prefHeight="109.99990000000253" prefWidth="516.0" spacing="30.0">
<children>
<ImageView fx:id="imgMessage" fitHeight="110.0" fitWidth="146.66666666666666" pickOnBounds="true" preserveRatio="true" styleClass="null" />
<TextField fx:id="txtMessage" editable="false" prefHeight="47.0" prefWidth="325.0" />
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</HBox>
<HBox alignment="CENTER" prefHeight="58.0" prefWidth="516.0" spacing="30.0">
<children>
<Button fx:id="btnYes" mnemonicParsing="false" onAction="#OnBtnYes" text="Button" />
<Button fx:id="btnNo" mnemonicParsing="false" onAction="#OnBtnNo" text="Button" />
</children>
</HBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</VBox>
</children>
<stylesheets>
<URL value="#MyCSS.css" />
</stylesheets>
</AnchorPane>
With this I can create a modal pop-up window, and I also want to create other child windows for displaying data in other ways using different controls. And, most importantly, I can use Scene Builder to create the layout.
What do you think? Is this a good way to do this until they add real support in Java 8 and JavaFX 8?
did you try wit the Group class? you can add diferent elements with fxml and controllers.
Group root= new Group();
AnchorPane frame=FXMLLoader.load(getClass().getResource("frame.fxml"));
AnchorPane content= FXMLLoader.load(getClass().getResource("principal.fxml"));
root.getChildren().add(window);
root.getChildren().add(frame);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

Categories