I'm fairly new to JavaFX. I've been hopelessly trying to get this to work for so long and have no idea why it's not working. The items are not showing up on the tableview. I created the UI using scene builder. I've looked at many similar questions and nothing seems to have helped.
Simplified for minimal code:
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 {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package sample;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
ObservableList<Person> people = FXCollections.observableArrayList();
#FXML private TableView<Person> table;
#FXML private TableColumn<Person, SimpleStringProperty> c1;
#FXML private TableColumn<Person, SimpleStringProperty> c2;
#Override
public void initialize(URL location, ResourceBundle resources) {
c1.setCellValueFactory(new PropertyValueFactory<>("age"));
c2.setCellValueFactory(new PropertyValueFactory<>("name"));
people.add(new Person("2", "Sam"));
people.add(new Person("1", "Met"));
table.setItems(people);
}
}
Person class:
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private SimpleStringProperty age;
private SimpleStringProperty name;
public Person(String age, String name) {
this.age = new SimpleStringProperty(age);
this.name = new SimpleStringProperty(name);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<TableView fx:id="table" layoutX="51.0" layoutY="31.0" prefHeight="338.0" prefWidth="498.0">
<columns>
<TableColumn fx:id="c1" prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="c1" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="c2" prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="c2" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</AnchorPane>
Thanks.
To make it work you need to implement a few changes. Read the comments in the code :
Person class must have public getters:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
private final SimpleStringProperty age;
private final SimpleStringProperty name;
public Person(String age, String name) {
this.age = new SimpleStringProperty(age);
this.name = new SimpleStringProperty(name);
}
//add getters to properties with the appropriate naming convention
public final StringProperty ageProperty() {
return age;
}
public final StringProperty nameProperty() {
return name;
}
}
In the fxml you must specify the controller. Also note the change in TableView :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="xml_tabel_1.Controller"> <!-- need to specify the controller of the fxml -->
<TableView items="${controller.people}"> <!-- need to specify the name of the observable list -->
<columns>
<TableColumn prefWidth="75.0" text="age">
<cellValueFactory>
<PropertyValueFactory property="age" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</AnchorPane>
All you need to do in the controller is to populate the observable list and have a public getter for it :
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
public class Controller implements Initializable {
private final ObservableList<Person> people = FXCollections.observableArrayList();
//all this was already set in fxml
//#FXML private TableView<Person> table;
//#FXML private TableColumn<Person, SimpleStringProperty> c1;
//#FXML private TableColumn<Person, SimpleStringProperty> c2;
#Override
public void initialize(URL location, ResourceBundle resources) {
people.add(new Person("2", "Sam"));
people.add(new Person("1", "Met"));
}
//add a getter to people
public ObservableList<Person> getPeople() {
return people ;
}
}
Related
New to JavaFx I created a view table using Scene builder with a table column named checkedCol that's supposed to contain chekboxes , on my controller i created those checkbox this way :
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
public class RecapController implements Initializable{
#FXML
private TableView<OMission> missionTable;
#FXML
private TableColumn<OMission, Boolean> checkedCol;
private ObservableList<OMission> UserMission =
FXCollections.observableArrayList();
private OMission mission=null;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
load_recap();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void load_recap(){
.....
checkedCol.setCellFactory(
CheckBoxTableCell.forTableColumn(checkedCol)
);
checkedCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
checkedCol.setEditable(true);
}
}
On class Model i have this code :
public class OMission {
private String ObjMission ;
private Boolean remark;
public OMission( String objMission ) {
This.objMission = ObjMission ;
remark = false;
}
public Boolean getRemark() {
return remark;
}
public void setRemark(Boolean remark) {
this.remark = remark;
}
public String getObjMission() {
return ObjMission;
}
public void setObjMission(String objMission) {
ObjMission = objMission;
}
}
fxml code :
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="pan" prefHeight="740.0" prefWidth="1258.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.RecapController">
<children>
<JFXButton layoutX="16.0" layoutY="22.0" onAction="#OpenMission" style="-fx-background-color: #0A4969;" text="Ajouter une nouvelle mission : " textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</JFXButton>
<TableView fx:id="missionTable" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">
<columns>
<TableColumn prefWidth="612.5" text="RAPPORT DE MISSION">
<columns>
<TableColumn fx:id="local" prefWidth="100.0" text="Localité" />
<TableColumn fx:id="objmission" prefWidth="243.0" text="Objet Mission" />
</columns>
</TableColumn>
<TableColumn fx:id="checkedCol" prefWidth="75.0" text="select" />
</columns>
</TableView>
<JFXButton layoutX="263.0" layoutY="25.0" onAction="#print_tab" text="print" />
<CheckBox fx:id="SelectAll" layoutX="1154.0" layoutY="41.0" mnemonicParsing="false" text="Select All" />
</children>
The problem is i don't get any erros but i can not even select/check the checkboxes i get on my view table , i can't figure out the issue :) any idea ? Thank's in advance .
Any idea ?
From your code is not clear if the Tableview is editable. Assuming the TableView is missionTable, try:
missionTable.setEditable(true);
Alternatively you can set the Tableview editable in your FXML by adding editable="true":
<TableView fx:id="missionTable" editable="true" layoutX="16.0" layoutY="64.0" prefHeight="659.0" prefWidth="1226.0">
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>
I know, this question has been asked hundreds of times and I spent the last 3 hours reading all of them. I am writing a little Application with JavaFX (and I have done so before...). Everything is working besides putting content into my column:
#FXML public void initialize() {
exerciseColumn.setCellValueFactory(new PropertyValueFactory<Exercise, String>("name"));
}
This is my initialize method and this:
public StringProperty nameProperty() {
return name;
}
is the Property Getter for "StringProperty name". The really funny thing is, that the code is the exact copy of another project I made a while ago (I double and triple checked everything of course) that I compiled again today and that is working perfectly. I read the doc for, as it feels, half JavaFX to find a solution but it just does not make any sense. If you think you need more code to help me, then I can provide it, of course.
edit: another example with the same issue (at least I hope so)
Main:
package test;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import test.model.Exercise;
import test.view.Controller;
public class Main extends Application {
private Stage primaryStage;
private AnchorPane pane;
private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();
public Main() {
activeSession.add(new Exercise("ednhzrd"));
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Test");
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/view.fxml"));
pane = (AnchorPane) loader.load();
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
Controller controller = new Controller();
controller.setMainApp(this);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Controller:
package test.view;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;
public class Controller {
#FXML private TableView<Exercise> exerciseTable;
#FXML private TableColumn<Exercise, String> exerciseColumn;
private Main main;
public Controller() {}
#FXML
public void initialize() {
exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
}
public void setMainApp(Main main) {
this.main = main;
}
}
Exercise:
enter codepackage test.model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Exercise {
private StringProperty name;
public Exercise(String name) {
this.name = new SimpleStringProperty(name);
}
public StringProperty nameProperty() {
return name;
}
}
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 prefHeight="400.0" prefWidth="209.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.view.Controller">
<children>
<TableView fx:id="exerciseTable" layoutX="200.0" layoutY="100.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="exerciseColumn" prefWidth="75.0" text="C1" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
There is no problem with your cellValueFactory. The only issue here is that you never put any items in the table. If you modify the controller to add a test item to the table:
package test.view;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;
public class Controller {
#FXML
private TableView<Exercise> exerciseTable;
#FXML
private TableColumn<Exercise, String> exerciseColumn;
private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();
private Main main;
public Controller() {
}
#FXML
public void initialize() {
exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
activeSession.add(new Exercise("hrrykane"));
exerciseTable.setItems(activeSession);
}
public void setMainApp(Main main) {
this.main = main;
}
}
then you see the desired effect:
I am new to JavaFx and I am trying to create a tableview and fxml is created using scene builder. If I run the program, the table is not getting the values. I found that this question is somehow matching with my requirement (javaFX 2.2 - Not able to populate table from Controller), but my code is matching with that too. But still I am not able to get the values in the table.
I have referenced the following video : http://www.youtube.com/watch?v=HiZ-glk9_LE
My code is as follows,
MainView.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainView extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane page = FXMLLoader.load(MainView.class.getResource("MainView.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("Sample Window");
primaryStage.setResizable(false);
primaryStage.show();
} catch (Exception e) {
}
}
}
MainView.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" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.MainViewController">
<children>
<SplitPane dividerPositions="0.535175879396985" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView fx:id="tableID" prefHeight="210.0" prefWidth="598.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" />
<TableColumn prefWidth="100.0" text="Date" fx:id="tDate" />
<TableColumn prefWidth="200.0" text="Name" fx:id="tName" />
<TableColumn prefWidth="75.0" text="Price" fx:id="tPrice" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
MainViewController.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import model.Table;
/**
* FXML Controller class
*
*/
public class MainViewController implements Initializable {
/**
* Initializes the controller class.
*/
#FXML
TableView<Table> tableID;
#FXML
TableColumn<Table, Integer> tID;
#FXML
TableColumn<Table, String> tName;
#FXML
TableColumn<Table, String> tDate;
#FXML
TableColumn<Table, String> tPrice;
int iNumber = 1;
ObservableList<Table> data =
FXCollections.observableArrayList(
new Table(iNumber++, "Dinesh", "12/02/2013", "20"),
new Table(iNumber++, "Vignesh", "2/02/2013", "40"),
new Table(iNumber++, "Satheesh", "1/02/2013", "100"),
new Table(iNumber++, "Dinesh", "12/02/2013", "16"));
#Override
public void initialize(URL url, ResourceBundle rb) {
// System.out.println("called");
tID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rID"));
tName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
tDate.setCellValueFactory(new PropertyValueFactory<Table, String>("rDate"));
tPrice.setCellValueFactory(new PropertyValueFactory<Table, String>("rPrice"));
tableID.setItems(data);
}
}
Table.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class Table {
SimpleIntegerProperty rID;
SimpleStringProperty rName;
SimpleStringProperty rDate;
SimpleStringProperty rPrice;
public Table(int rID, String rName, String rDate, String rPrice) {
this.rID = new SimpleIntegerProperty(rID);
this.rName = new SimpleStringProperty(rName);
this.rDate = new SimpleStringProperty(rDate);
this.rPrice = new SimpleStringProperty(rPrice);
System.out.println(rID);
System.out.println(rName);
System.out.println(rDate);
System.out.println(rPrice);
}
public Integer getrID() {
return rID.get();
}
public void setrID(Integer v) {
this.rID.set(v);
}
public String getrDate() {
return rDate.get();
}
public void setrDate(String d) {
this.rDate.set(d);
}
public String getrName() {
return rName.get();
}
public void setrName(String n) {
this.rDate.set(n);
}
public String getrPrice() {
return rPrice.get();
}
public void setrPrice(String p) {
this.rPrice.set(p);
}
}
Thanks in advance.
In a Table.java change all getters and setters names,
change getr* to getR*
change setr* to setR*
Thanks to #Uluk Biy. Whatever he mentioned, that needs to be changed and I found that I missed the <cellValueFactory> and <PropertyValueFactory> tags under the <TableColumn> tag for each columns in the FXML file which is as follows,
<TableColumn minWidth="20.0" prefWidth="40.0" text="ID" fx:id="tID" >
<cellValueFactory>
<PropertyValueFactory property="tID" />
</cellValueFactory>
</TableColumn>
Similar to this, all columns need to be updated like this.
After this change, the code is working fine. I am able to add the values into the row. :)
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);
}
}