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.
Related
Could anyone please explain why this is not working?
Loop (should) create a repeating pattern of VBox's inside a VBox, inside a ScrollPane, inside a BorderPane (central position), each with different data drawn from a database.
public void equipmentPaneBuilder(LinkedList<ModelEquipment> list) {
LinkedList<ModelEquipment> equipmentList = list;
for (int i = 0; i < equipmentList.size(); i++) {
ModelEquipment item = equipmentList.get(i);
try {
PaneEquipment equipmentPane = new PaneEquipment();
equipmentPane.updateFields(item.getTechId(), item.getShortName(), item.getLongDesc()); equipmentPane.setId("equipPane" + i);
EquipmentPanels.getChildren().add(equipmentPane);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is the custom node built on VBox that should be added to the EquipmentPanels VBox using the method above.
import javafx.fxml.FXML;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
public class PaneEquipment extends VBox {
#FXML private TextField techId;
#FXML private TextField shortText;
#FXML private TextArea longDesc;
public PaneEquipment() {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/fxml/PaneEquipment.fxml"));
fxmlLoader.setController(this);
try {
fxmlLoader.load();
}
catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public void updateFields(String techIdArg, String shortNameArg, String longDescArg) {
techID.setText(techIdArg);
shortText.setText(shortNameArg);
longDesc.setText(longDescArg);
}
}
FXML for the PaneEquipment objects/nodes:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0">
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="40.0" text="Tech ID" />
<TextField fx:id="techID" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="150.0" promptText="Tech ID Number" />
<Label layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="92.0" text="Name" />
<TextField fx:id="shortText" layoutX="50.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="465.0" promptText="Short text description (max. 40 characters)" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0">
<children>
<Label prefHeight="132.0" prefWidth="65.0" text="Description" wrapText="true" />
<TextArea fx:id="longDesc" prefHeight="100.0" prefWidth="682.0" promptText="Long text description (max. 300 characters)" wrapText="true" />
</children>
</HBox>
</children>
</VBox>
Based on my testing, everything is working fine, except that the equipmentPane's are not displaying when the program runs, I just get a blank scroll pane. The database and linked list work fine. The updatefields method works fine. No exception is printed.
If I replace the equipmentPaneBuiler loop with this, it displays correctly:
try {
VBox vbox = new VBox();
Label label = new Label();
vbox.getChildren().add("text");
EquipmentPanels.getChildren().add(vbox);
} catch (Exception e) {
e.printStackTrace();
}
So I presume it's an issue with my custom node, but I just can't figure it out.
You did not implement the Custom Component approach 100% correctly.
In your case 2 distinct VBoxes (or VBox-derived objects) exist during the execution of the PaneEquipment constructor. The object being constructed and the VBox created by FXMLLoader. The VBox created by FXMLLoader contains the contents but gets ignored and you only add the empty PaneEquipment.
To implement the approach correctly you need to use PaneEquipment as root and replace the root element of the fxml with <fx:root>
<fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0">
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="40.0" text="Tech ID" />
<TextField fx:id="TechID" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="150.0" promptText="Tech ID Number" />
<Label layoutX="10.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="92.0" text="Name" />
<TextField fx:id="ShortText" layoutX="50.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="465.0" promptText="Short text description (max. 40 characters)" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0">
<children>
<Label prefHeight="132.0" prefWidth="65.0" text="Description" wrapText="true" />
<TextArea fx:id="LongDesc" prefHeight="100.0" prefWidth="682.0" promptText="Long text description (max. 300 characters)" wrapText="true" />
</children>
</HBox>
</children>
</fx:root>
public PaneEquipment() {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/fxml/PaneEquipment.fxml"));
fxmlLoader.setController(this);
fxmlLoader.setRoot(this); // add this line
try {
fxmlLoader.load();
}
catch (IOException exception) {
throw new RuntimeException(exception);
}
}
I am trying to create my project but facing issue
main class
public class Controller extends AnchorPane {
public Controller() throws IOException {
System.out.println("is this called");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
FXML File
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="198.0" layoutY="129.0" mnemonicParsing="false" text="Button" />
<Button layoutX="198.0" layoutY="200.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
Controller class
public class FXMLDocumentController implements Initializable {
private Label label;
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Working on a Simple Invoice Software for self. Got stuck at the first point.
Unable to display rows in Tableview. I have tried every possible solution, gone through many tutorial videos, guides etc. Still not able to find the problem
Please help
NewInvoice.fxml
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<tabs>
<Tab text="CASH">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="additembutton" layoutX="412.0" layoutY="41.0" mnemonicParsing="false" text="ADD" />
<TableView fx:id="invoiceitemtableview" layoutX="190.0" layoutY="180.0" prefHeight="200.0" prefWidth="481.0">
<columns>
<TableColumn fx:id="amountcol" prefWidth="75.0" text="Amount" />
<TableColumn fx:id="gstcol" prefWidth="75.0" text="GST" />
<TableColumn fx:id="subtotalcol" prefWidth="75.0" text="Subtotal" />
<TableColumn fx:id="cgstcol" prefWidth="75.0" text="CGST" />
<TableColumn fx:id="sgstcol" prefWidth="75.0" text="SGST" />
</columns>
</TableView>
<TextField fx:id="itemcodetextbox" layoutX="216.0" layoutY="41.0" />
</children></AnchorPane>
</content>
</Tab>
<Tab text="BOBCARD">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
NewInvoiceController.java
public class NewInvoiceController implements Initializable {
#FXML
TableView<InvoiceItemBean> invoiceitemtableview;
#FXML
TableColumn<InvoiceItemBean,String> amountcol, gstcol, subtotalcol, cgstcol, sgstcol;
final ObservableList<InvoiceItemBean> data = FXCollections.observableArrayList(
new InvoiceItemBean("1049","5","999.10","22.5","22.5"),
new InvoiceItemBean("1800","12","1180.10","305.00","305.00")
);
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
amountcol.setCellValueFactory(new PropertyValueFactory<>("amount"));
gstcol.setCellValueFactory(new PropertyValueFactory<>("gst"));
subtotalcol.setCellValueFactory(new PropertyValueFactory<>("subtotal"));
cgstcol.setCellValueFactory(new PropertyValueFactory<>("cgst"));
sgstcol.setCellValueFactory(new PropertyValueFactory<>("sgst"));
invoiceitemtableview.setItems(data);}
}
InvoiceItemBean.java
public class InvoiceItemBean {
private final SimpleStringProperty amount;
private final SimpleStringProperty gst;
private final SimpleStringProperty subtotal;
private final SimpleStringProperty cgst;
private final SimpleStringProperty sgst;
public InvoiceItemBean( String amount, String gst, String subtotal, String cgst, String sgst) {
this.amount = new SimpleStringProperty(amount);
this.gst = new SimpleStringProperty(gst);
this.subtotal = new SimpleStringProperty(subtotal);
this.cgst = new SimpleStringProperty(cgst);
this.sgst = new SimpleStringProperty(sgst);
}
public String getAmount() {
return amount.get();
}
public String getGst() {
return gst.get();
}
public String getSubtotal() {
return subtotal.get();
}
public String getCgst() {
return cgst.get();
}
public String getSgst() {
return sgst.get();
}
public void setAmount(String amount_value) {
amount.set(amount_value);
}
public void setGst(String gst_value) {
gst.set(gst_value);
}
public void setSubtotal(String subtotal_value) {
subtotal.set(subtotal_value);
}
public void setCgst(String cgst_value) {
cgst.set(cgst_value);
}
public void setSgst(String sgst_value) {
sgst.set(sgst_value);
}
}
Main.java
public class ClimaxInvoice extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("NewInvoice.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Unable to get anything
Output Table view showing no content
PLEASE HELP!!!
Your problem is really simple, you haven't specified the controller in the fxml.
Just add fx:controller="packageName.NewInvoiceController"
at the end of the first line in your fxml file.
So the whole line should be:
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="packageName.NewInvoiceController">
Of course, replace packageName with the name of your package.
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.
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.