Accessing scene from controller class - javafx-2

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.

Related

How to add a custom component from encodebegin?

I've created a custom component which extends the UIComponentBase abstract class, so when I use this component: <test:mycomponent /> It works as espected.
I'm creating another custom component and I want to use the previously created component in this one, so I tried:
#Override
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("mycomponent", this);
writer.endElement("mycomponent");
}
I new this was a long shot, since startElement just creates a tag with the given component name i.e mycomponent, so I searched around and found:
UIComponentBase mycomponent =
(UIComponentBase)context.getApplication().createComponent("mycomponent");
If this is correct, how does one add the component ? I'm using JSF 2.2
A link to where I can find more on this would be greatly apreciated also.

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.

While typing in a text field enable a keystroke ENTER to activate a button in JavaFX

Such as typing into the Google search box the hitting ENTER activates the search
I've just been introduced to JavaFX and Scene Builder a few days ago so I'm learning the basics here. I have the latest version of JavaFX and am using Scene Builder to facilitate action events. Also, any pointers to relevant tutorials would be helpful. At one point in the day I was focused on the Keyboard section of the coding panel of Scene Builder, especially with the "On Key Released" event with no results. Thanks in advance
Here's a rough idea of what I'm trying to do:
#FXML
Text Field theTextField;
#FXML
Button theButton;
#FXML
void ButtonPressed() {
//do stuff here
}
#FXML
//when ENTER is pressed the button is activated
void textFieldEnterPressed() {
ButtonPressed();
}
In your FXML file, add a onKeyPressed handler
<TextField fx:id="yourTextField" onKeyPressed="#handleEnterPressed">
Implement the handler in you Controller
#FXML
public void handleEnterPressed(KeyEvent event)
if (event.getCode() == KeyCode.ENTER) {
// do some actions
}
}
In TextField, when you press Enter, you get notification through onAction. In your Java code you can add:
#FXML
private void handleTFAction(ActionEvent event) {
TextField source = (TextField)event.getSource();
System.out.println("You entered: "+source.getText());
}
In your FXML (or through JavaFX SceneBuilder designer) hook it up to your TextField's OnAction event. In FXML it looks something like this:
<TextField onAction="#handleTFAction" ... />

Hot to update dynamically font size

I have this css file which sets the default font size and type in JavaFX application:
.root {
-fx-font: 13px Tahoma;
}
scene.getStylesheets().add(getClass().getResource("/styles/Styles.css").toExternalForm());
I want to update the size and the font type from the Java code dynamically of the root component (all components). How I can do this?
Note:
This code updates the Font type and size of all components into the JavaFX application.
Please consider taking a look at the official JavaFX documentation. There you find the code example which answers your question:
Text t = new Text("That's the text");
t.setFont(Font.font ("Verdana", 20));
UPDATE
In your application controller get an instance of your root pane, e.g. AnchorPane and use the setId("") function to set new style for the whole pane (my actionChange is connected with a button on the pane, which triggers the event/change):
public class AppController implements Initializable {
#FXML
private AnchorPane mainPane;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
#FXML
public void actionChange() {
mainPane.setId("fancytext");
}
}
When pressing the button, the style for the pane is changed. I just used the font-size as an example. Before, you need to specify the new style in your CSS file:
.root {
-fx-font: 12px Tahoma;
}
#fancytext {
-fx-font: 20px Tahoma;
}
That's before:
That's after the button was pressed:

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