I have created a custom component which is basically a vbox with more components inside. However when I include it on the HomeView.fxml the component's children are nowhere to be seen. Each individual internal component is null, causing a NullPointerException on the binding part on HomeController. What am I missing?
HomeView.fxml
<StackPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="views.home.HomeController">
<BorderPane>
<center>Placeholder</center>
</BorderPane>
<SidePane fx:id="sidePane" maxWidth="200.0" prefWidth="200.0" style="-fx-background-color: cyan;" translateX="200.0" StackPane.alignment="CENTER_RIGHT" />
</StackPane>
HomeController.java
public class HomeController implements Initializable {
private final HomeViewModel viewModel;
#FXML private SidePane sidePane;
public HomeController(HomeViewModel viewModel) {
this.viewModel = viewModel;
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// Bind side pane - NullPointerException here, that's why it is commented out
// sidePane.imageProperty().bindBidirectional(viewModel.selectedPlatform().image());
// sidePane.nameProperty().bindBidirectional(viewModel.selectedPlatform().name());
// sidePane.identifierProperty().bind(viewModel.selectedPlatform().id().asString());
}
}
SidePane.fxml
<fx:root type="javafx.scene.layout.Region"
fx:controller="viewscomponents.sidepane.SidePane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefWidth="200" maxWidth="200">
<childrenUnmodifiable>
<VBox>
<ImageView fx:id="image" />
<Label text="Name:" />
<TextField fx:id="name" />
<HBox>
<Label alignment="BOTTOM_RIGHT" text="id: " />
<Label fx:id="id" />
</HBox>
</VBox>
</childrenUnmodifiable>
</fx:root>
SidePane.java
public class SidePane extends Region {
#FXML private ImageView image;
#FXML private TextField name;
#FXML private Label id;
public ObjectProperty<Image> imageProperty() {
return image.imageProperty();
}
public StringProperty nameProperty() {
return name.textProperty();
}
public StringProperty identifierProperty() {
return id.textProperty();
}
}
As James_D stated in his comment, you never load the fxml file of your custom control. Also I think it does not work with Region and you should not specify a controller in the fxml file. It should work like this:
SidePane.java
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class SidePane extends VBox {
#FXML
private ImageView image;
#FXML
private TextField name;
#FXML
private Label id;
public SidePane() throws IOException {
FXMLLoader loader = new FXMLLoader(App.class.getResource("SidePane.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
public ObjectProperty<Image> imageProperty() {
return image.imageProperty();
}
public StringProperty nameProperty() {
return name.textProperty();
}
public StringProperty identifierProperty() {
return id.textProperty();
}
}
SidePane.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<fx:root type="VBox"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefWidth="200" maxWidth="200">
<ImageView fx:id="image"/>
<Label text="Name:"/>
<TextField fx:id="name"/>
<HBox>
<Label alignment="BOTTOM_RIGHT" text="id: "/>
<Label fx:id="id"/>
</HBox>
</fx:root>
Related
I've started a project requested by our instructor building an application in JavaFX.
I used SceneBuilder for FXML Part when I want to add buttons on gridpane, but it didn't work. The connection with the database is okay. On the first part of the application, the user logs in. After that he/she comes on UserWindow, but the buttons aren't displayed.
File Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Controller c;
ControllerB cB;
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
FXMLLoader f = new FXMLLoader();
FXMLLoader f2 = new FXMLLoader();
f.setLocation(Main.class.getResource("sample.fxml"));
f2.setLocation(Main.class.getResource("UserWindow.fxml"));
Parent root = f.load();
Parent root2 = f2.load();
Scene s = new Scene(root, 500, 340);
// Controllers
c = f.getController();
c.setMain(this);
c.setScene2(new Scene(root2, 900, 500));
cB = f2.getController();
cB.setMain(this);
cB.setScene(s);
window.setTitle("Remote Works");
window.setScene(s);
window.show();
}
public static void main(String[] args) {
launch(args);
}
public void setUserData(Object o) {
window.setUserData(o);
}
public void setScene(Scene scene) {
window.setScene(scene);
window.show();
}
public void setScene2(Scene scene) {
window.setScene(scene);
window.show();
}
}
File ControllerB.java:
package sample;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.*;
public class ControllerB implements Initializable {
#FXML
private GridPane Box;
#FXML
private Label nameUser;
#FXML
private Label matriculeUser;
#FXML
private Label fonctionUser;
#FXML
private Button deconnect;
private Scene scene1;
private Main main;
ObservableList<String> ecoles;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
try {
schoolretrieve();
int size = ecoles.size();
Node[] n = new Node[size];
for (int i = 0; i < n.length; i++) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Bloc.fxml"));
ControllerItem controller = new ControllerItem();
loader.setController(controller);
n[i] = loader.load();
controller.setButtonInst(ecoles.get(i)); // Until this part all works
Box.getChildren().add(n[i]); // But it doesn't add buttons to GridPane
}
} catch (IOException | NullPointerException | SQLException e) {
//System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
File ControllerItem.java:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class ControllerItem {
#FXML
private Button buttonInst;
public Button getButtonInst() {
return buttonInst;
}
public void setButtonInst(String s) {
buttonInst.setText(s);
}
}
File Bloc.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<Button fx:id="buttonInst" mnemonicParsing="false" prefHeight="132.0" prefWidth="201.0" stylesheets="#../styles.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" />
File UserWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="497.0" prefWidth="710.0" styleClass="pane" stylesheets="#../styles.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerB">
<left>
<Pane prefHeight="497.0" prefWidth="156.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="150.0" fitWidth="151.0" layoutX="11.0" pickOnBounds="true" preserveRatio="true" />
<Circle fill="#e1ebf5" layoutX="78.0" layoutY="88.0" opacity="0.18" radius="74.0" stroke="BLACK" strokeType="INSIDE" />
<Label fx:id="nameUser" layoutX="10.0" layoutY="196.0" prefHeight="50.0" prefWidth="136.0" text="Nom Utilisateur :" />
<Label fx:id="fonctionUser" layoutX="11.0" layoutY="259.0" prefHeight="37.0" prefWidth="136.0" text="Statut : " />
<Label fx:id="matriculeUser" layoutX="10.0" layoutY="306.0" prefHeight="37.0" prefWidth="136.0" text="Identifiant : " />
<Button fx:id="deconnect" layoutX="20.0" layoutY="364.0" mnemonicParsing="false" onAction="#onAction" prefHeight="27.0" prefWidth="117.0" text="deconnexion" />
</children>
</Pane>
</left>
<center>
<Pane opacity="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #f7cc3d;"
BorderPane.alignment="CENTER">
<GridPane fx:id="Box" alignment="TOP_CENTER" hgap="3.0" layoutX="70.0" layoutY="96.0"
nodeOrientation="LEFT_TO_RIGHT" opacity="0.95" prefHeight="305.0" prefWidth="415.0"
styleClass="gridpane" stylesheets="#../ss.css" vgap="3.0"/>
</Pane>
</center>
</BorderPane>
So you have file UserWindow.fxml and you have declared a GridPane in it. I suggest you to don't make a separate .fxml file for the button.
Just do it like this in your ControllerB:
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
schoolretrieve();
int size = ecoles.size();
//Node[] n = new Node[size];
for (int i = 0; i < size; i++) {
//FXMLLoader loader = new FXMLLoader(getClass().getResource("Bloc.fxml"));
//ControllerItem controller = new ControllerItem();
//loader.setController(controller);
//n[i] = loader.load();
//controller.setButtonInst(ecoles.get(i));
//Box.getChildren().add(n[i]);
Button btn = new Button(ecoles.get(i));
Box.add(btn, 0, i); // Button is added to next row every time.
}
}
i have tried all solution in youtube and google but none of them work. The problem is when i want to switch to new scene along sending data to another scene i got error saying
Null pointer Exception
public class FirstController implements Initializable {
#FXML private TextField textField;
#FXML private Button btn;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void onClick(ActionEvent event){
//switching to new scene
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/application/Second.fxml"));
try{
loader.load();
}catch(Exception e){ }
SecondController sn=loader.getController();
sn.setText(textField.getText());
Parent p=loader.getRoot();
Stage window=new Stage();
window.setScene(new Scene(p));
window.setTitle("dfd");
window.show();
}
}
main.fxml:
<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
<TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
</children>
</AnchorPane>
Second controller :
public class SecondController implements Initializable {
#FXML private Label labelField;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void setText(String name){
this.labelField.setText(name);
}
}
Second.fxml:
`<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
Your second FXML file is missing the fx:controller attribute. Therefore, when you load it, there is no controller, and so loader.getController() returns null.
Consequently, when you call sn.setText(...), you get a null pointer exception.
Just add the missing fx:controller attribute to the second FXML file. Assuming that SecondController is in the application package, this will look like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
Also note that it's very bad practice to silently "squash" exceptions, by using
try {
/* ... */
} catch (Exception e) { }
It's especially bad if the ensuing code depends on the code in the try block succeeding. In particular, in this case if your call to load() results in an exception (which may happen for a multitude of reasons, including the FXML not being found or being invalid, the fx:id and field names, or types,
not matching, method names for handlers not matching or having the wrong parameters, etc.), then the load method will silently fail and the controller will not be set in the loader. This will again result in sn being null.
Here's a complete example using your code, but with the other errors fixed as well as the missing fx:controller attribute inserted:
application/Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("First.fxml")));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
application/First.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FirstController">
<children>
<Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
<TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
</children>
</AnchorPane>
application/FirstController.java:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class FirstController implements Initializable {
#FXML private TextField textField;
#FXML private Button btn;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void onClick(ActionEvent event) throws Exception {
//switching to new scene
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/application/Second.fxml"));
loader.load();
SecondController sn=loader.getController();
sn.setText(textField.getText());
Parent p=loader.getRoot();
Stage window=new Stage();
window.setScene(new Scene(p));
window.setTitle("dfd");
window.show();
}
}
application/Second.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
application/SecondController.java
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class SecondController implements Initializable {
#FXML private Label labelField;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void setText(String name){
this.labelField.setText(name);
}
}
I'm trying to set up a simple example for my program using JavaFX.
What I want to have is a Controller with a main.fxml; then the main.fxml will have a TabbedPane as root, with 2 tabs (tab1.fxml and tab2.fxml), each with its controller (Tab1Controller and Tab2Controller).
Now this might be the prolem, yet I don't see why it would be a problem:
Tab1Controller and Tab2Controller both extend Controller; because they share various fields which are manipulated, for example a bottom status bar which needs constant updates.
So far I managed to have working all controllers and .fxml.
When I try to set up the text of the label in the Controller from one of the subclasses it just points to null, yet the label is initialised in the gui with its default text..
Compiles correctly and lblController seems to be correctly linked to its fx:id in main.fxml.
Any help/links are much appreciated.
I've been looking at various posts but the only one getting close to what I need it this one:
JavaFX 8, controllers inheritance
Main:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../view/main.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 700));
primaryStage.show();
}
}
Main controller:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller {
#FXML
public Label lblController;
public Controller() {
System.out.println("CONTROLLER MAIN - CONSTRUCTOR");
}
#FXML
public void initialize() {
System.out.println("CONTROLLER MAIN - INITIALIZER");
}
protected void setSts(String sts) {
lblController.setText(sts);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.60"
fx:controller="sample.Controller">
<BorderPane>
<center>
<TabPane tabClosingPolicy="UNAVAILABLE">
<Tab text="Tab 1">
<AnchorPane>
<!--FOR fx:id HAS TO HAVE THE SAME NAME BUT IN LOWER CASE AS THE .fxml-->
<fx:include fx:id="tab1" source="tab/tab1.fxml"/>
</AnchorPane>
</Tab>
<Tab text="Tab 2">
<AnchorPane>
<fx:include fx:id="tab2" source="tab/tab2.fxml"/>
</AnchorPane>
</Tab>
</TabPane>
</center>
<bottom>
<AnchorPane>
<Label fx:id="lblController" text="Controller Test"/>
</AnchorPane>
</bottom>
</BorderPane>
</AnchorPane>
Tab1Controller
package sample.ctrTab;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import sample.Controller;
public class Tab1Controller extends Controller {
#FXML
Label lbl1;
#FXML
TextField txt1;
#FXML
Button btn1Save, btn1Load;
public Tab1Controller() {
super();
System.out.println("CONTROLLER TAB 1 - CONSTRUCTOR");
}
#FXML
void btn1Save() {
System.out.println("CONTROLLER TAB 1 - SAVE CLICK");
// lblController.setText("ANYTHING"); //NULL POINTER HERE
// setSts(txt1.getText()); //OR HERE
}
#FXML
void btn1Load() {
System.out.println("CONTROLLER TAB 1 - LOAD CLICK");
}
protected void setSts(String sts) {
super.setSts(sts);
}
}
tab1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.60" fx:controller="sample.ctrTab.Tab1Controller">
<children>
<Label fx:id="lbl1" layoutX="100" layoutY="50" text="Def tab 1"/>
<TextField fx:id="txt1" layoutX="100" layoutY="70"/>
<Button fx:id="btn1Save" onAction="#btn1Save" layoutX="100" layoutY="140" mnemonicParsing="false"
text="Save text"/>
<Button fx:id="btn1Load" onAction="#btn1Load" layoutX="200.0" layoutY="140" mnemonicParsing="false"
text="Send to tab 2"/>
</children>
</AnchorPane>
The fields may be present in the controller classes, however FXMLLoader still creates new controller instances for every fxml loaded given these fxmls. Since tab1.fxml does not contain a Label tag with fx:id="lblController" as attribute, the lblController field is never injected to the Tab1Controller instance.
You would be better off not extending Controller, but passing a reference of it to the child controllers:
public class Controller {
#FXML
public Label lblController;
#FXML
private Tab1Controller tab1Controller;
#FXML
private Tab2Controller tab2Controller;
public Controller() {
System.out.println("CONTROLLER MAIN - CONSTRUCTOR");
}
#FXML
public void initialize() {
System.out.println("CONTROLLER MAIN - INITIALIZER");
tab1Controller.initParentController(this);
tab2Controller.initParentController(this);
}
public void setSts(String sts) {
lblController.setText(sts);
}
}
public class Tab1Controller {
#FXML
Label lbl1;
#FXML
TextField txt1;
#FXML
Button btn1Save, btn1Load;
public Tab1Controller() {
System.out.println("CONTROLLER TAB 1 - CONSTRUCTOR");
}
private Controller parentController;
public void initParentController(Controller controller) {
parentController = controller;
}
#FXML
void btn1Save() {
System.out.println("CONTROLLER TAB 1 - SAVE CLICK");
parentController.lblController.setText("ANYTHING");
parentController.setSts(txt1.getText());
}
#FXML
void btn1Load() {
System.out.println("CONTROLLER TAB 1 - LOAD CLICK");
}
}
Note that for the child controllers you could actually use a abstract superclass implement the initParentController method only once.
I am trying to create a custom control in JavaFX8. What I would like to achieve is an AnchorPane with a header and a footer that can each contain controls. I've noticed that any events I create on the control will not fire in "design mode". How would I implement dragging and dropping of new Nodes into my control using SceneBuilder?
Here is a simple example. I have this control.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root minHeight="300" minWidth="300" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane fx:id="test" prefHeight="209.0" prefWidth="191.0">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TextField fx:id="textField" layoutX="14.0" layoutY="14.0" />
<HBox fx:id="hbox" layoutX="14.0" layoutY="40.0" prefHeight="100.0" prefWidth="100.0" style="-fx-background-color: RED;" />
</children>
</AnchorPane>
</children>
</fx:root>
Here is the controller:
///*
// * To change this license header, choose License Headers in Project Properties.
// * To change this template file, choose Tools | Templates
// * and open the template in the editor.
// */
package javafxbeancontroltest;
//
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
//
///**
// *
// * #author ajmiro
// */
public class FXMLDocumentController extends AnchorPane
{
#FXML
StringProperty labelText = new SimpleStringProperty("Initial Value");
public String getLabelText() { return labelText.get(); }
public void setLabelText(String newText) {labelText.set(newText);}
public StringProperty labetTextProperty() { return labelText; }
public Button getButton() {return button;}
#FXML
private AnchorPane test;
public AnchorPane getTest() {return test;}
#FXML
private Label label;
#FXML
public Button button;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
//label.setText("Hello World!");
label.setText(getLabelText());
//label.setText(textField.getText());
}
public FXMLDocumentController() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/javafxbeancontroltest/FXMLDocument.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
How would I implement being able to drag a control onto the red HBOX at design time in SceneBuilder 2.0?
I am trying to create a tree with custom check boxes. There is no option in the Scenebuilder with such an option. I tried to write the code in the controller, but did not work.
Someone please help me out.
FXML and the controller code:
the CheckBoxTreeItem is not working.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="486.9609375" prefWidth="472.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="abcd.controller">
<!-- TODO Add Nodes -->
<children>
<Button id="button" fx:id="button1" layoutX="99.0" layoutY="51.0" mnemonicParsing="false" onAction="#processLogin" text="Button" />
<TextArea fx:id="textarea" layoutX="40.0" layoutY="83.0" prefHeight="76.0" prefWidth="129.0" wrapText="true" />
<TreeView fx:id="treeview" layoutX="69.0" layoutY="204.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.CheckBoxTreeItemBuilder;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.util.Callback;
public class controller implements Initializable {
// Binding with the FXML
#FXML
private Button taskBarButton1;
#FXML private TextArea textarea;
#FXML private TreeView<String> treeview;
#FXML private CheckBoxTreeItem<String> root;
//#FXML private CheckBoxTreeItem<String> rootitem ;
#FXML
private void processLogin(ActionEvent event) {
textarea.appendText("clicked");
}
public void loadonstart()
{
for (int i=0; i<5;i++)
{
//System.out.println("numbers" +i);
textarea.appendText("\n"+i);
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
loadonstart();
loadtreeitems();
}
private void loadtreeitems()
{
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("Source Root");
root.setExpanded(true);
TreeView<String> tree = new TreeView<String> (root);
//tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
for (int j=10; j<15; j++)
{
root.setExpanded(true);
root.getChildren().add(new CheckBoxTreeItem<String>("" +j));
}
treeview.setRoot(root);
}
}