How can I get value from label text field which is dynamically changes JavaFX - javafx-2

<Label fx:id="lblLibrarianId" layoutX="82.0" layoutY="14.0" prefHeight="24.0" prefWidth="212.0" text="$librarianId" />
I have a controller name LibraryController. I set label value text to librarianId dynamically from another controller. Now, I want to access this librarianId to LibraryController.
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../fxmlfile/librarian.fxml"));
fxmlLoader.getNamespace().put("librarianId", librarianId);
This is the way how I set the value dynamically to Label text field. Now I want to retrieve that Label Text value to my LibraryController.

In your controller, create your label object, and call getText()
you'll need to use the #FXML to associate that object with the fx:id in your .fxml file
ex.
public class LibraryController{
#FXML public Label lblLibrarianId;
public String librarianID;
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../fxmlfile/librarian.fxml"));
fxmlLoader.getNamespace().put("librarianId", librarianId);
librarianId = lblLibrarianId.getText();
}

Related

JSF Custom component extending Primefaces SelectOneMenu

I need to create a custom component that either extend or include a Primefaces SelectOneMenu. This is done so that I can deliver the select items based on the field (for now, they are hardcoded in the example below and the tag is properly registered).
The component is rendered and the select items are also displayed fine. However, when I save, the record's field is not updated with the selected item's value. Is there some primeface method I should override to actually set the value?
Are there any tutorials on how to extend primeface (or atleast jsf) components? I could hardly find any. Thank you in advance
#FacesComponent(value = ComponentRegistry.INPUT_SELECTDROPDOWN_COMPONENT_TYPE)
public class InputSelectDropdown extends SelectOneMenu {
#Override
public void encodeBegin(FacesContext context) throws IOException {
this.setValueExpression("value", this.getValueExpression("value"));
UISelectItem select1 = new UISelectItem();
select1.setItemLabel("item 1");
select1.setItemValue("item1");
select1.encodeAll(context);
this.getChildren().add(select1);
UISelectItem select2 = new UISelectItem();
select2.setItemLabel("item 2");
select2.setItemValue("item2");
select2.encodeAll(context);
this.getChildren().add(select2);
super.encodeBegin(context);
}
}

Null Pointer trying to bind a stringproperty

I try to bind a String and a StringPoperty, I thought this would work :
#FXML private TextArea consoleTextArea;
StringProperty sp = new SimpleStringProperty();
consoleTextArea.textProperty().bind(sp);
But it return me a NULL Pointer Exception, Why ?
EDIT : After some answers it appears that my consoleTextArea is NULL, but I initialize it correctly :
<TextArea id="consoleTextArea" fx:id="consoleTextArea" prefHeight="309.0" prefWidth="600.0" VBox.vgrow="ALWAYS" /> , and I create the window before calling the controller, so I don't undertand why is it NULL ...
Is your consoleTextArea initialized? This is done by the FXML loader, so make sure you call it first before trying to use this field.
Did you set the fx:id of the TextArea to consoleTextArea in Scene Builder?
Does the TextArea contain any text?
Try initializing sp with new SimpleStringProperty("").
One of those should fix it.

Getting Null Pointer Exception while removing a layer in Javafx

I have three layers
Root layout, home, content (rootLayout.fxml, home.fxml and content.fxml)
FXMLLoader loader = new FXMLLoader();
loader.setLocation(GenerateReport.class
.getResource("/skin/rootLayout.fxml"));
rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(GenerateReport.class
.getResource("/skin/home.fxml"));
AnchorPane homeLayout = (AnchorPane) loader.load();
rootLayout.getChildren().addAll(homeLayout);
.
.
.
rootLayout.getChildren().addAll(contentLayout);
like this I am adding content layout. In rootLayout.fxml i have a home button. My requiredment is if a user clicks home button
then i want content layout to be removed and home layout to be visible.
content.fxml
<AnchorPane id="myContent" ... fx:controller="com.ReportController">
rootLayout.fxml
<AnchorPane id="AnchorPane" .. fx:controller="com.ReportController">
<children>
<Button fx:id="home" onAction="#homeBtn" .../>
</children>
</AnchorPane>
In my Controller (In all the fxml file i am pointing to the same controller) class i created
#FXML
private Button home;
#FXML
private AnchorPane myContent;
#FXML
protected void homeBtn(ActionEvent event) throws Exception {
System.out.println("click! homeBtn");
myContent.getChildren().clear();
}
The problem i am facing is i am getting NullPointerException. i.e. myContent.getChildren() is returning null. Could anyone help me in resolving this issue.
You're getting a Null Pointer Exception because Javafx doesn't associate your myContent with the AnchorPane stored in the fxml, and does not attach a reference to that object.
Nodes in fxml files are given their name identifiers by using fx:id. Note, for example, that your #FXML private Button home is marked in the fxml as <Button fx:id="home"...>.
In this case, your #FXML AnchorPane myContent is marked in fxml as <AnchorPane id="myContent"...>, not <AnchorPane fx:id="myContent"...>.
id="" is used only in CSS.

Accessing scene from controller class

I'm new to and trying to learning JavaFX and FXML. Most of my application logic is in the FXMLController class and the base class is pretty much empty except the basic code that was generated by NetBeans IDE such as below
#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();
}
I have an element with ID input1 that is of type TextField. How can i access this (or any
other) control by its ID? (keeping in mind that I am in the controller class and not the main class).
I found this question below which is exactly what I'm looking for but that situation is different because they are in the main class where scene is defined. How can i access scene from the controller class and use the code in the question below.
How to find an element with an ID in JavaFX?
This is my first time answering a question on Stack Overflow so please go easy on me.
I am new to JavaFX as well and I too had a problem with this.
This is what if found.
Your TextField in your FXMLDocument.fxml must have a fx:id assigned to it, as in:
<TextField fx:id="input1" layoutX="0.5" layoutX="0.5" />
If you are using the JavaFX SceneBuilder then you can find the fx:id under "Code: TextField" on the right side.
Then in your controller class you can access it but using.
#FXML public TextField input1;
You can use an ArrayList to loop through all of your TextFields.
Here is an example.
#FXML public TextField input1;
#FXML public TextField input2;
#FXML public TextField input3;
#FXML public TextField input4;
#FXML public TextField input5;
#FXML public TextField input6;
#FXML public TextField input7;
#FXML public Button button;
List<TextField> inputs = new ArrayList<TextField>();
public void displayText(ActionEvent event) {
inputs.add(input1);
inputs.add(input2);
inputs.add(input3);
inputs.add(input4);
inputs.add(input5);
inputs.add(input6);
inputs.add(input7);
for (int x = 0; x < 7; x++) {
System.out.println(inputs.get(x).getText());
}
}
There might be a simpler way, but this way works for me.

javafx - updating values in fxml object which was dynamically loaded on button click

I am creating a simple Weather Application
Here goes the details of my question :-
I have a main Controller (GeoWeatherMain.java). When Appl is run, this class(GeoWeatherMain.class) gets loaded which loads an fxml file.
Below is code for it :-
Parent root = FXMLLoader.load(getClass().getResource("GeoWeatherMainUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Now, GeoWeatherMainUI.fxml has BorderPane object implementation. It consists of a button(on the left pane) which onclick loads another fxml file inside center pane. The skeleton of GeoWeatherMainUI.fxml looks like below:-
<BorderPane fx:id="MainBody" prefHeight="736.0" prefWidth="1140.0" xmlns:fx="http://javafx.com/fxml" fx:controller="geoweather.GeoWeatherUIActionHandlers">
.
.
<Button layoutX="91.0" layoutY="67.0" mnemonicParsing="false" onAction="#getCurrAndForecastWeatherCond" text="Get Weather Details" />
.
.
<BorderPane>
Now GeoWeatherUIActionHandlers.java is another controller which takes care of different button action events.Below is its complete code
public class GeoWeatherUIActionHandlers implements Initializable{
#FXML
BorderPane MainBody;
#FXML
Label LocName;/*LocName is fx id for a label*/
#FXML
private void getCurrAndForecastWeatherCond(ActionEvent event){
try{
Pane centerPane = FXMLLoader.load(getClass().getResource("WeatherDetails.fxml"));
MainBody.setCenter(centerPane);
/*LocName.setText("xyz");*/
}catch (IOException ex){
TextArea excep = new TextArea(ex.toString());
MainBody.setCenter(excep);
}catch(Exception e){
TextArea excep = new TextArea("Inside Exception : \n" + e.toString());
MainBody.setCenter(excep);
}
}
#Override
public void initialize(URL url, ResourceBundle rb){}
}
Now, if I want to update the loaded WeatherDetails.fxml file with new values, how to do that? I tried as in the above commented code.(LocName.setText("xyz")). But, it didn't work (giving NullPointerException).
I went through complete documentation of javafx # docs.oracle.com. No luck. Here also I didn't get the answer. Please guide.
If the LocName is located inside WeatherDetails.fxml then it is excepted behavior that the LocName will be null. Because #FXML Label LocName; is defined in GeoWeatherUIActionHandlers.java which is a controller of GeoWeatherMainUI.fxml. Move LocName from WeatherDetails to GeoWeatherMainUI FXML file and see where you are still getting the error or not.
If your aim is to set the text of the label that is inside WeatherDetails.fxml, then do this work
in the controller of WeatherDetails similar to current GeoWeatherUIActionHandlers, or
in GeoWeatherUIActionHandlers, after loading the WeatherDetails.fxml
get the label by id (not fx:id) from centerPane with ((Label)centerPane.lookup("#myLabel")).setText("xyz"), or
get the controller of WeatherDetails and call getLocName().setText("xyz"). Assuming getter method exists in a controller class.

Resources