JavaFX: ListView not displaying in GUI - java

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

Related

FXML Load exception when inserting data of own class into tableview

I have a problem again.
I am trying to understand FXML and I am currently using the "Mastering FXML" guide of oracle "creating an address book with FXML".
I am pretty near at the end of the guide but i am not able to fix a problem I have since yesterday.
I want to write a Maintenance Program for my father but I am always getting the statement:javafx.fxml.LoadException: Maintenance is not a valid type.
there are 3 classes that are important for this problem I guess. That would be my "mainLayout.fxml", "FXMLDocumentController.java" and "Maintenance.java"
So I am just posting them in this order
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.collections.*?>
<Pane fx:controller="FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="858.0" prefWidth="1276.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.8687747035573122" layoutX="2.0" layoutY="25.0" prefHeight="839.0" prefWidth="1276.0">
<items>
<AnchorPane fx:id="anchorpaneLeft" minHeight="0.0" minWidth="0.0" prefHeight="811.0" prefWidth="1104.0">
<children>
<ScrollPane id="scrollpane" fx:id="scrollpane" layoutX="452.0" layoutY="319.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<TableView id="tableView" fx:id="tableView" prefHeight="834.0" prefWidth="1102.0">
<columns>
<TableColumn text="%tablecolumnName" >
<cellValueFactory><PropertyValueFactory property = "name"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnAddress" >
<cellValueFactory><PropertyValueFactory property = "address"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNumber" >
<cellValueFactory><PropertyValueFactory property = "number"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnMail" >
<cellValueFactory><PropertyValueFactory property = "mail"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnLast" >
<cellValueFactory><PropertyValueFactory property = "last"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNext" >
<cellValueFactory><PropertyValueFactory property = "next"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNote" >
<cellValueFactory><PropertyValueFactory property = "note"/>
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Maintenance name="jacob" address="fichtestraße 5" number="01515242" mail="jacob#jacob.com" last="10.10.2018" next="10.10.2019" note="leer jung"/>
</FXCollections>
</items>
</TableView>
</content>
</ScrollPane>
</children>
</AnchorPane>
<AnchorPane fx:id="anchorpaneRight" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<VBox fx:id="vboxNew" alignment="TOP_CENTER" layoutX="50.0" layoutY="52.0" prefHeight="85.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="726.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="26.0">
<children>
<Label fx:id="labelNew" prefHeight="17.0" prefWidth="192.0" text="%labelNew">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Button fx:id="buttonInsert" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonInsert" onAction="#handleInsertButton" textAlignment="CENTER" />
</children>
</VBox>
<VBox fx:id="vboxEdit" alignment="TOP_CENTER" layoutY="120.0" prefHeight="717.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="120.0">
<children>
<Label fx:id="labelEdit" prefHeight="17.0" prefWidth="166.0" text="%labelEdit">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Button fx:id="buttonEdit" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonEdit" textAlignment="CENTER" />
<Button fx:id="buttonDelete" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonDelete" />
<Button fx:id="buttonWeek" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonWeek" />
<Button fx:id="buttonMonth" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonMonth" />
<Button fx:id="buttonYear" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonYear" />
</children>
</VBox>
</children>
</AnchorPane>
</items>
</SplitPane>
<MenuBar fx:id="menubar" layoutX="2.0" layoutY="2.0" prefHeight="25.0" prefWidth="1276.0">
<menus>
<Menu fx:id="menuFile" mnemonicParsing="false" text="%menuFile">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu fx:id="menuEdit" mnemonicParsing="false" text="%menuEdit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu fx:id="menuHelp" mnemonicParsing="false" text="%menuHelp">
<items>
<MenuItem fx:id="itemAbout" mnemonicParsing="false" text="%itemAbout" />
</items>
</Menu>
<Menu fx:id="menuLanguage" mnemonicParsing="false" text="%menuLanguage">
<items>
<MenuItem fx:id="itemGer" mnemonicParsing="false" text="%itemGer" />
<MenuItem fx:id="itemEng" mnemonicParsing="false" text="%itemEng" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</Pane>
FXMLDocumentController.java:
import java.net.URL;
import java.util.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
public class FXMLDocumentController implements Initializable {
#FXML private Label label;
#FXML private TableView<Maintenance> table;
#FXML private TableColumn tablecolumnName;
#FXML private TableColumn tablecolumnAddress;
#FXML private TableColumn tablecolumnNumber;
#FXML private TableColumn tablecolumnMail;
#FXML private TableColumn tablecolumnLast;
#FXML private TableColumn tablecolumnNext;
#FXML private TableColumn tablecolumnNote;
#FXML private Button buttonInsert;
#FXML private Button buttonEdit;
#FXML private Button buttonDelete;
#FXML private Button buttonWeek;
#FXML private Button buttonMonth;
#FXML private Button buttonYear;
#FXML private TextField textfieldName;
#FXML private TextField textfieldAddress;
#FXML private TextField textfieldMail;
#FXML private TextField textfieldNumber;
#FXML private TextField textfieldLast;
#FXML private TextField textfieldNext;
#FXML private TextField textfieldNote;
#FXML private TableView<Maintenance> tableView;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void handleInsertButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("insertWindow.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Insert Window");
Scene scene = new Scene (root1);
stage.setScene(scene);
stage.show();
} catch(Exception e){
e.printStackTrace();
}
}
protected void handleEditButton() {
}
protected void handleDeleteButton() {
}
protected void handleWeekButton() {
}
protected void handleMonthButton() {
}
protected void handleYearButton() {
}
protected void handleGerButton() {
}
protected void handleEngButton() {
}
#FXML
protected void addMaintenance(ActionEvent event){
ObservableList <Maintenance> data = tableView.getItems();
data.add(new Maintenance(textfieldName.getText(), textfieldAddress.getText(),
textfieldMail.getText(), textfieldNumber.getText(),
textfieldLast.getText(), textfieldNext.getText(),
textfieldNote.getText()));
textfieldName.setText("");
textfieldAddress.setText("");
textfieldMail.setText("");
textfieldNumber.setText("");
textfieldLast.setText("");
textfieldNext.setText("");
textfieldNote.setText("");
}
}
and finally Maintenance.java
import javafx.beans.property.SimpleStringProperty;
public class Maintenance {
private final SimpleStringProperty name = new SimpleStringProperty("");
private final SimpleStringProperty address = new SimpleStringProperty("");
private final SimpleStringProperty number = new SimpleStringProperty("");
private final SimpleStringProperty mail = new SimpleStringProperty("");
private final SimpleStringProperty last = new SimpleStringProperty("");
private final SimpleStringProperty next = new SimpleStringProperty("");
private final SimpleStringProperty note = new SimpleStringProperty("");
public Maintenance(){
this("", "", "", "", "", "", "");
}
public Maintenance(String name, String address, String number, String mail, String last, String next, String note){
setName(name);
setAddress(address);
setNumber(number);
setMail(mail);
setLast(last);
setNext(next);
setNote(note);
}
public String getName() {
return name.get();
}
public String getAddress() {
return address.get();
}
public String getNumber() {
return number.get();
}
public String getMail() {
return mail.get();
}
public String getLast() {
return last.get();
}
public String getNext() {
return next.get();
}
public String getNote() {
return note.get();
}
public final void setName(String Name){
name.set(Name);
}
public final void setAddress(String Address){
address.set(Address);
}
public final void setNumber(String Number){
number.set(Number);
}
public final void setMail(String Mail){
mail.set(Mail);
}
public final void setLast(String Last){
last.set(Last);
}
public final void setNext(String Next){
next.set(Next);
}
public final void setNote(String Note){
note.set(Note);
}
}
insertwindow.fxml is existing and worked great so far so I won't post it to shorten my post same for main.java and my 4 property files.
Hope someone can tell me what I am doing wrong again.
Thanks for help!
Executing C:\Users\Schurke\Documents\NetBeansProjects\Maintenance\dist\run365389303\Maintenance.jar using platform D:\Program Files\Java\jdk1.8.0_202\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Schurke/Documents/NetBeansProjects/Maintenance/dist/run365389303/Maintenance.jar!/mainLayout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3116)
at Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 19 more
Exception running application Main
Java Result: 1
For custom FXML type you should add an import statement:
<?import Maintenance?>
In general it is bad practice to use default packages. Put the Maintenance.java into some package and import it as:
<?import yourPackageName.Maintenance?>

Selecting exact row information and adding it to a text field from oblist

I have a program that is basically a task manager, it takes information from SQLite Database. it has 5 info fields (UID, Date, CreatedBy, Category, Task). In order to populate the table view I use class Model Table.
Controller
#FXML public TableView<ModelTable> taskManagerView;
#FXML public TableColumn<ModelTable, String> col_UID;
#FXML public TableColumn<ModelTable, String> col_Date;
#FXML public TableColumn<ModelTable, String> col_CreatedBy;
#FXML public TableColumn<ModelTable, String> col_Category;
#FXML public TableColumn<ModelTable, String> col_Task;
ObservableList<ModelTable> oblist = FXCollections.observableArrayList();
public FilteredList<ModelTable> filteredOblist = new FilteredList<>(oblist, s -> true);
try{
Connection conn =
DriverManager.getConnection("jdbc:sqlite://Location\\");
Statement statement = conn.createStatement();
ResultSet rs =statement.executeQuery("SELECT * FROM TaskManagerData");
while (rs.next()){
oblist.add(new ModelTable(rs.getString("UID"), rs.getString("Date"), rs.getString("CreatedBy"), rs.getString("Category"),rs.getString("Task")));
}
}catch (SQLException e){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("DB Error");
alert.setHeaderText("DB Connection Failed");
alert.setContentText(e.getMessage());
alert.showAndWait();
}
col_UID.setCellValueFactory(new PropertyValueFactory<>("UID"));
col_Date.setCellValueFactory(new PropertyValueFactory<>("Date"));
col_CreatedBy.setCellValueFactory(new PropertyValueFactory<>("CreatedBy"));
col_Category.setCellValueFactory(new PropertyValueFactory<>("Category"));
col_Task.setCellValueFactory(new PropertyValueFactory<>("Task"));
taskManagerView.setItems(filteredOblist);
ModelTable gettingTask =taskManagerView.getSelectionModel().getSelectedItem();
detailedTask.setText(gettingTask.getTask());
Model Table:
public class ModelTable {
String UID, Date, CreatedBy, Category, Task;
package Controllers;
public class ModelTable {
String UID, Date, CreatedBy, Category, Task;
public ModelTable(String UID, String date, String createdBy, String category, String task) {
this.UID = UID;
Date = date;
CreatedBy = createdBy;
Category = category;
Task = task;
}
public String getUID() {
return UID;
}
public void setUID(String UID) {
this.UID = UID;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getCreatedBy() {
return CreatedBy;
}
public void setCreatedBy(String createdBy) {
CreatedBy = createdBy;
}
public String getCategory() {
return Category;
}
public void setCategory(String category) {
Category = category;
}
public String getTask() {
return Task;
}
public void setTask(String task) {
Task = task;
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="488.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.TaskManagerController">
<children>
<AnchorPane layoutY="3.0" prefHeight="165.0" prefWidth="600.0" AnchorPane.bottomAnchor="320.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="3.0">
<children>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="600.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="2.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Add Entry" />
</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>
<TextField fx:id="filter_uid" alignment="CENTER" layoutX="14.0" layoutY="62.0" promptText="UID" />
<TextField fx:id="filter_created" alignment="CENTER" layoutX="14.0" layoutY="98.0" promptText="Created By" />
<DatePicker fx:id="filter_date" layoutX="172.0" layoutY="98.0" onAction="#dateFilter" prefHeight="25.0" prefWidth="167.0" promptText="Date" />
<TextArea fx:id="detailedTask" editable="false" layoutX="348.0" layoutY="27.0" prefHeight="137.0" prefWidth="252.0" AnchorPane.leftAnchor="348.0" AnchorPane.rightAnchor="0.0" />
<RadioButton fx:id="radioHomework" layoutX="172.0" layoutY="79.0" mnemonicParsing="false" onAction="#radioButtonOne" text="Homework" />
<RadioButton fx:id="radioMaintenance" layoutX="172.0" layoutY="62.0" mnemonicParsing="false" onAction="#radioButtonTwo" text="Maintenance" />
<RadioButton fx:id="radioOther" layoutX="262.0" layoutY="62.0" mnemonicParsing="false" onAction="#radioButtonThree" text="Other" />
<RadioButton fx:id="radioNull" layoutX="262.0" layoutY="79.0" mnemonicParsing="false" onAction="#radioButtonFour" text="Show All" />
<Label layoutX="14.0" layoutY="39.0" prefHeight="17.0" prefWidth="326.0" style="-fx-alignment: CENTER;" text="FILTERS" textAlignment="CENTER">
<font>
<Font size="15.0" />
</font>
</Label>
</children></AnchorPane>
<TableView fx:id="taskManagerView" layoutY="166.0" prefHeight="282.0" prefWidth="600.0" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="166.0">
<columns>
<TableColumn fx:id="col_UID" prefWidth="70.0" text="UID" />
<TableColumn fx:id="col_Date" prefWidth="100.0" text="Date" />
<TableColumn fx:id="col_CreatedBy" prefWidth="90.0" text="Created By" />
<TableColumn fx:id="col_Category" prefWidth="100.0" text="Category" />
<TableColumn fx:id="col_Task" prefWidth="239.0" text="Task" />
</columns>
</TableView>
<ButtonBar layoutY="360.0" prefHeight="40.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<buttons>
<Button fx:id="addTask" mnemonicParsing="false" onAction="#addTaskFunction" text="Add Task" />
<Button fx:id="exportBtn" mnemonicParsing="false" onAction="#export" text="Export" />
<Button fx:id="backToMainMenu" mnemonicParsing="false" onAction="#backButton" text="Back" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
My question is how can i have it so my program when i select for example UID 7 (when the program is running) from the table view the information from "Task" appears in a text area in the program. Currently
Hopefully this was understandable.
Thank you.
The selection happens after the scene is displayed. If retrieved before (e.g. in the initialize method), the selectedItem property is null. Add a listener to the property to do the updates instead:
#FXML
private void initialize() {
taskManagerView.getSelectionModel().selectedItemProperty().addListener((o, oldValue, newValue) -> {
if (newValue == null) {
textArea.setText("");
// TODO: clear other controls
} else {
textArea.setText(newValue.getTask());
// TODO: set values to other controls
}
});
}

JavaFX TableView - Unable to display rows

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.

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

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

Categories