JavaFX TableView - Unable to display rows - java

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.

Related

JavaFX: ListView not displaying in GUI

My problem is that my ListView is not showing anything on the GUI,
I have added a placeholder to test and it does show the placeholder text but other than that I cannot add to the list.
My aim is to have a list of all my clients accounts, I have included the Account class, I want to display all my accounts ID's in the ListView.
For some reason it will not work, I have tried just inputting random data just to see if thats the issue but still not luck.
I have a static Set called allAccounts in my Main.class file.
Any help would be greatly appreciated, I have searched near and far and since I am new to this struggle to understand why this is not working.
Controller.class
public class Controller implements Initializable{
#FXML
private TableView<?> tableView;
#FXML
private TableColumn<?, ?> tableAccount;
#FXML
private TableColumn<?, ?> tableDate;
#FXML
private TableColumn<?, ?> tableTime;
#FXML
private TableColumn<?, ?> tableTotal;
#FXML
private TableColumn<?, ?> tableNotes;
#FXML
private ListView<String> list;
#FXML
private TextArea invoiceView;
#FXML
private TextField amountField;
#FXML
private Button addAmount;
#FXML
private Button newInvoice;
#FXML
private Button saveInvoice;
#FXML
private Button deleteInvoice;
#FXML
private Label messageBar;
#Override
public void initialize(URL location, ResourceBundle resources) {
//list.setPlaceholder(new Label("No Content In List"));
ObservableList<String> names = FXCollections.observableArrayList(
"Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");
list = new ListView<>();
list.setItems(names);
//tableView = new TableView<>(names);
}
#FXML
public void newInvoiceFired() {
System.out.println("new invoice button has been clicked");
}
#FXML
public void deleteInvoiceFired() {
System.out.println("delete invoice button has been clicked");
}
#FXML
public void saveInvoiceFired() {
System.out.println("save invoice button has been clicked");
}
public void setAccountsList(){
for (Account account : Main.getAllAccounts()){
}
}
}
GUI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="477.0" prefWidth="641.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<children>
<SplitPane dividerPositions="0.24249999999999972" layoutY="50.0" prefHeight="427.0" prefWidth="641.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ListView fx:id="list" layoutX="-24.0" layoutY="62.0" prefHeight="425.0" prefWidth="151.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="445.0" prefWidth="411.0">
<children>
<SplitPane dividerPositions="0.30023640661938533" layoutX="143.0" layoutY="123.0" orientation="VERTICAL" prefHeight="445.0" prefWidth="480.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 layoutX="128.0" layoutY="45.0" prefHeight="208.0" prefWidth="478.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="tableAccount" prefWidth="75.0" text="Account" />
<TableColumn fx:id="tableDate" prefWidth="75.0" text="Date" />
<TableColumn fx:id="tableTime" prefWidth="75.0" text="Time" />
<TableColumn fx:id="tableTotal" prefWidth="75.0" text="Amount" />
<TableColumn fx:id="tableNotes" prefWidth="75.0" text="Notes" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="286.0" prefWidth="478.0">
<children>
<SplitPane dividerPositions="0.4474789915966387" layoutX="168.0" layoutY="48.0" prefHeight="292.0" prefWidth="478.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="160.0" prefWidth="100.0">
<children>
<TextArea fx:id="invoiceView" layoutX="5.0" layoutY="14.0" prefHeight="290.0" prefWidth="209.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TextField fx:id="amountField" layoutX="82.0" layoutY="86.0" prefHeight="27.0" prefWidth="104.0" promptText="0.00" />
<Button fx:id="addAmount" layoutX="195.0" layoutY="86.0" mnemonicParsing="false" text="Add" />
<Label layoutX="14.0" layoutY="91.0" text="Amount: : " />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</items>
</SplitPane>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="14.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Icon/Logo.png" />
</image>
</ImageView>
<HBox alignment="CENTER_RIGHT" layoutX="462.0" layoutY="14.0" spacing="15.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
<children>
<Button fx:id="newInvoice" mnemonicParsing="false" onAction="#newInvoiceFired" text="New" />
<Button fx:id="saveInvoice" mnemonicParsing="false" onAction="#deleteInvoiceFired" text="Save" />
<Button fx:id="deleteInvoice" mnemonicParsing="false" onAction="#deleteInvoiceFired" text="Delete" />
</children>
</HBox>
</children>
</AnchorPane>
Account.class
public class Account {
private String id;
private String name;
private String address1;
private String address2;
private String postCode;
private Set<Invoice> invoiceArchive = new HashSet<>();
public Account(String id, String name, String address1, String address2, String postCode) {
this.id = id;
this.name = name;
this.address1 = address1;
this.address2 = address2;
this.postCode = postCode;
Main.addAccount(this);
}
public Account(String id, String name, String address1, String postCode) {
this.id = id;
this.name = name;
this.address1 = address1;
this.postCode = postCode;
}
public Account(String name, String id) {
this.name = name;
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public Set<Invoice> getInvoiceArchive() {
return invoiceArchive;
}
public Invoice newInvoice(){
Invoice i = new Invoice(this);
invoiceArchive.add(i);
return i;
}
#Override
public String toString(){
return this.getId();
}
}
You create a new instance of list in Controller, though your listview is initialized in the fxml file. Then you add the items to the new instance, but that one is not added to the scenegraph, hence your original listView remains empty

Populating TableView in JavaFX From Data in SQLite

I am trying to populate a TableView from data in SQLite database but I am experiencing a very weird scenario that I cannot understand what is causing it.
The tableview only populates two columns and does not populate the rest. The Tablecolumns with 'NO' and 'Date Created' do not get populated when the TableView is finally displayed.
This code however displays data from SQLite database in 'Title' and 'Description' TableView columns.
Please someone with a hawk eye help me identify where I am going wrong on this code. I have spent the better part of the day trying to figure out where I am going wrong but I do not seem to figure out what it is that I am not doing it right. I will gladly appreciate any help on this.
Here is my code for
Main class
Blockquote
public class Notedb extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("ListNotesUI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Blockquote
FXML
Blockquote
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<SplitPane dividerPositions="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="notedb.test.ListNotesUIController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.5" layoutX="186.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="196.0" prefWidth="598.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>
<Button alignment="TOP_CENTER" contentDisplay="TEXT_ONLY" layoutX="484.0" layoutY="22.0" mnemonicParsing="false" onAction="#newNote" prefHeight="54.0" prefWidth="66.0" text="New Note" textAlignment="CENTER" wrapText="true" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="126.0" layoutY="2.0" prefHeight="94.0" prefWidth="596.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="441.0" minWidth="10.0" prefWidth="441.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="292.0" minWidth="10.0" prefWidth="155.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="m_search" onAction="#searchNotes" />
<Label fx:id="labelNOs" alignment="CENTER" prefHeight="17.0" prefWidth="94.0" text="4 Notes" GridPane.columnIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="181.0" layoutY="98.0" prefHeight="196.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Pane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<Button layoutX="95.0" layoutY="24.0" mnemonicParsing="false" prefHeight="54.0" prefWidth="100.0" text="Delete" />
<Button fx:id="btn_medit" layoutX="389.0" layoutY="24.0" mnemonicParsing="false" onAction="#editNoteRow" prefHeight="54.0" prefWidth="94.0" text="Edit" />
</children>
</Pane>
<TableView id="tableNotes" fx:id="tableNotes" editable="true" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn id="noCol" fx:id="noCol" text="NO">
</TableColumn>
<TableColumn id="titleCol" fx:id="titleCol" text="Title">
</TableColumn>
<TableColumn id="dateCreatedCol" fx:id="dateCreatedCol" text="Date Created">
</TableColumn>
<TableColumn id="descriptionCol" fx:id="descriptionCol" text="Description">
</TableColumn>
</columns>
</TableView>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
Blockquote
Controller class
Blockquote
public class ListNotesUIController implements Initializable {
#FXML
private Label label;
#FXML
private Label labelNOs;
#FXML
private Button newNote;
#FXML
private Button btn_medit;
#FXML
private TextField m_search;
#FXML
private TableView tableNotes;
#FXML
private TableColumn titleCol;
#FXML
private TableColumn descriptionCol;
#FXML
private TableColumn dateCreatedCol;
#FXML
private TableColumn noCol;
//START | SQLITE
private static Connection con;
private static Statement stat;
private PreparedStatement prep;
//END | SQLITE
private ObservableList <Note> dataNotes;
DataBank dbank = new DataBank();
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#FXML
private void editNoteRow(ActionEvent event) {
}
#FXML
private void newNote(ActionEvent event) throws IOException {
}
#FXML
private void searchNotes(ActionEvent event){
}
#Override
public void initialize(URL url, ResourceBundle rb) {
dataNotes = FXCollections.observableArrayList();
noCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("idno")
);
dateCreatedCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("datecreated")
);
titleCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("title")
);
descriptionCol.setCellValueFactory(
new PropertyValueFactory<Note, String>("description")
);
try {
SQLiteConfig config = new SQLiteConfig();
con = DriverManager.getConnection("jdbc:sqlite:Note.db");
stat = con.createStatement();
stat.executeUpdate("CREATE TABLE IF NOT EXISTS NotesDB (idno INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Title VARCHAR(500), Description VARCHAR(1000), DateCreated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);");
ResultSet rs = con.createStatement().executeQuery("SELECT idno, Title, DateCreated, Description FROM NotesDB");
while (rs.next()) {
Note nt = new Note();
nt.idno.set(rs.getString("idno"));
nt.title.set(rs.getString("Title"));
nt.datecreated.set(rs.getString("DateCreated"));
nt.description.set(rs.getString("Description"));
dataNotes.add(nt);
}
tableNotes.setItems(dataNotes);
} catch (SQLException ex) {
Logger.getLogger(ListNotesUIController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Blockquote
DataModel class
Blockquote
public class Note {
public SimpleStringProperty title = new SimpleStringProperty();
public SimpleStringProperty description = new SimpleStringProperty();
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
public String getTitle() {
return title.get();
}
public void setTitle(String titleStr) {
title.set(titleStr);
}
public String getDescription() {
return description.get();
}
public void setDescription(String descriptionStr) {
description.set(descriptionStr);
}
public String getDateCreated() {
return datecreated.get();
}
public void setDateCreated(String datecreatedStr) {
datecreated.set(datecreatedStr);
}
public String getIdNO() {
return idno.get();
}
public void setIdNO(String idnoStr) {
idno.set(idnoStr);
}
}
Blockquote
There is a mistake regarding your property naming.
Your function for getDateCreated and IdNO don't correlate with
the naming convention.
Replace
public SimpleStringProperty datecreated = new SimpleStringProperty();
public SimpleStringProperty idno = new SimpleStringProperty();
with
public SimpleStringProperty dateCreated = new SimpleStringProperty();
public SimpleStringProperty idNO = new SimpleStringProperty();
And have a look at the naming conventions for properties

JavaFXML include FXML change upper controller

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.

Edit the values of the TableView from TextField

Hi I am trying in the spirit of the example in http://code.makery.ch/java/javafx-2-tutorial-intro/ to make a similar application where the TableView values can be edited by a series of TextFields instead of a popup form. The reason why I would wannt do it like that is that I am having many fields in a similar application I want to develop and I would like to avoid the user editing them on the TableView
The TableView shows a list of Persons where for each I record name, surname and country. I have tried to create a bidirectionalBinding between the name property of the Person bean and the textProperty() of the TextField but this doesn't work.
I also tried to add to the textProperty a ChangeListener so when it changes to update the ObservableList of propoerties and this also didn't work
Apparently I am doing something wrong and so far I have the following code:
FXDocumentController.java
public class FXMLDocumentController implements Initializable {
private static final Logger logger = Logger.getLogger(FXMLDocumentController.class.getName());
private ObservableList<Person> data;
#FXML
private TableView<Person> tableview;
#FXML
private TableColumn<Person, String> colName;
#FXML
private TableColumn<Person, String> colSurname;
#FXML
private TableColumn<Person, String> colCountry;
#FXML
private TextField name;
#Override
public void initialize(URL url, ResourceBundle rb) {
assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
colName.setCellValueFactory(
new PropertyValueFactory<Person,String>("name"));
colSurname.setCellValueFactory(
new PropertyValueFactory<Person,String>("surname"));
colCountry.setCellValueFactory(
new PropertyValueFactory<Person,String>("country"));
DBClass objDbClass = new DBClass();
try{
con = objDbClass.getConnection();
buildData();
tableview.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 1) {
Person p = tableview.getSelectionModel().getSelectedItem();
//name.textProperty().setValue(p.getName());
name.textProperty().bindBidirectional(p.name);
//name.textProperty().bind(p.name);
}
}
}
});
name.textProperty().addListener(new ChangeListener(){
#Override
public void changed(ObservableValue ov, Object t, Object t1) {
System.out.println("Value changed!");
/** Tried also this but this wont work
name.textProperty().setValue((String)t1);
int index = tableview.getSelectionModel().getSelectedIndex();
data.get(index).name.setValue( (String) t1);
tableview.setItems(data);
* */
}
}
);
}
catch(ClassNotFoundException ce){
logger.info(ce.toString());
}
catch(SQLException ce){
logger.info(ce.toString());
}
} //initialize
private void buildData() {
data = FXCollections.observableArrayList();
try{
data.add(new Person("Jon", "Doe", "USA"));
data.add(new Person("Lars", "Andersson", "Sweden"));
tableview.setItems(data);
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
Person.java
public class Person {
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty surname = new SimpleStringProperty();
public SimpleStringProperty country = new SimpleStringProperty();
public Person(String name, String surname, String country){
this.name.set(name);
this.surname.set(surname);
this.country.set(country);
}
public String getName(){
return name.get();
}
public String getSurname(){
return surname.get();
}
public String getCountry(){
return country.get();
}
}
And for the sake of completeness I am giving the fxml file (very ugly but I am experimenting with the functionality;)) and the launcher
FXMLDocument.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" blendMode="SRC_OVER" cache="false" disable="false" focusTraversable="false" prefHeight="576.0" prefWidth="1024.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="javafxdemoproject.FXMLDocumentController">
<children>
<SplitPane dividerPositions="0.3336594911937378" focusTraversable="true" layoutX="0.0" layoutY="55.0" prefHeight="521.0" prefWidth="1024.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="410.0" prefWidth="297.0">
<children>
<TableView fx:id="tableview" editable="true" prefHeight="520.0" prefWidth="338.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="178.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="Name" fx:id="colName" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="154.0" text="Surname" fx:id="colSurname" />
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="112.0" text="Country" fx:id="colCountry" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="404.0" prefWidth="460.0">
<children>
<Accordion layoutX="0.0" layoutY="0.0" prefHeight="520.0" prefWidth="262.0">
<expandedPane>
<TitledPane fx:id="personalTp" animated="false" text="Personal Details">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="name" layoutX="44.0" layoutY="27.0" prefWidth="200.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</expandedPane>
<panes>
<fx:reference source="personalTp" />
<TitledPane fx:id="x2" animated="false" text="Positions held">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ListView prefHeight="621.0" prefWidth="454.0" AnchorPane.bottomAnchor="-2.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="74.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</children>
</AnchorPane>
</items>
</SplitPane>
<MenuBar layoutY="0.0" AnchorPane.rightAnchor="908.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<ToolBar layoutY="24.0" AnchorPane.leftAnchor="0.0">
<items>
<Button mnemonicParsing="false" text="Button" />
</items>
</ToolBar>
</children>
</AnchorPane>
JavaFXDemoProject.java
public class JavaFXDemoProject extends Application {
#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();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Questions
1) Is it at all possible what I am trying to do?
2) Why isn't this working with bidirectionalBinding
Any help appreciated
I changed your controller class like this
public void initialize(URL url, ResourceBundle rb) {
assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
colName.setCellValueFactory(
new PropertyValueFactory<Person,String>("name"));
colSurname.setCellValueFactory(
new PropertyValueFactory<Person,String>("surname"));
colCountry.setCellValueFactory(
new PropertyValueFactory<Person,String>("country"));
buildData();
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() {
#Override
public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) {
if (oldValue !=null) name.textProperty().unbindBidirectional(oldValue.nameProperty());
if (newValue !=null) name.textProperty().bindBidirectional(newValue.nameProperty());
}
});
} //initialize
and your person class a bit
public class Person {
private StringProperty name = new SimpleStringProperty();
private StringProperty surname = new SimpleStringProperty();
private StringProperty country = new SimpleStringProperty();
public Person(String name, String surname, String country){
this.name.set(name);
this.surname.set(surname);
this.country.set(country);
}
public StringProperty nameProperty(){return name;}
public StringProperty surnameProperty(){return surname;}
public StringProperty countryProperty(){return country;}
}
Now the TextField can bind to the property in Person

Why member is not injected in JavaFX controller?

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.

Categories