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?>
Related
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
}
});
}
I'm trying to pass a String between scenes in JavaFX, I already followed the some answers but it seems that they don't work because the string is null in the new scene.
Here's my MainControler:
public class FXMLDocumentController implements Initializable {
#FXML
private Button btn_signup;
#FXML
private TextField et_username;
#FXML
private PasswordField et_password;
#FXML
private Hyperlink link_login;
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
#FXML
private void handleButtonAction(ActionEvent event) throws IOException {
if(btn_signup == event.getSource()){
Conector conn = new Conector();
if (conn.checkUser(et_username.getText(), et_password.getText())) {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("FXMLTasker.fxml"));
Pane myPane = (Pane)myLoader.load();
Scene scene = new Scene(myPane);
Stage stage = new Stage();
stage.setResizable(false);
stage.setScene(scene);
FXMLTaskerController controler = new FXMLTaskerController();
controler.setUser(et_username.getText());
myLoader.setController(controler);
prevStage.close();
stage.show();
}else {
JOptionPane.showMessageDialog(null, "Usuario o Contraseña incorrecta");
}
}else if (link_login == event.getSource()){
System.out.println("Registrate!");
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Here's the other class controler, the one where i want my String.
public class FXMLTaskerController implements Initializable {
#FXML private ListView list_todo;
#FXML private ListView list_done;
#FXML public String username;
private void handleButtonAction(ActionEvent event) {
}
#Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println(username);
list_todo.setCellFactory(new TaskCellFactory());
list_done.setCellFactory(new TaskCellFactory());
try {
getTasks();
} catch (IOException ex) {
Logger.getLogger(FXMLTaskerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setUser(String username){
this.username = username;
}
public void getTasks() throws IOException{
Conector con = new Conector();
for(Task task: con.obtenerTareas("pepin")){
System.out.println(task);
if(task.isState()){
list_done.getItems().add(task);
}else{
list_todo.getItems().add(task);
}
}
}
}
And here are the fxml files:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="500.0" style="-fx-background-color: #0288D1; -fx-border-color: #01579B; -fx-border-width: 5;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="desktop_tasker.FXMLDocumentController">
<children>
<Label layoutX="215.0" layoutY="34.0" text="Sign In" textFill="WHITE">
<font>
<Font size="23.0" />
</font>
</Label>
<Hyperlink fx:id="link_login" layoutX="150.0" layoutY="269.0" onAction="#handleButtonAction" text="Not registered yet? Click here." textFill="WHITE" />
<TextField fx:id="et_username" layoutX="21.0" layoutY="102.0" prefHeight="25.0" prefWidth="425.0" promptText="Username" style="-fx-prompt-text-fill: #ffffff; -fx-background-color: #0288D1;" />
<Separator layoutX="27.0" layoutY="126.0" prefHeight="3.0" prefWidth="425.0" />
<Separator layoutX="27.0" layoutY="192.0" prefHeight="3.0" prefWidth="425.0" />
<Button fx:id="btn_signup" layoutX="27.0" layoutY="222.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="23.0" prefWidth="425.0" style="-fx-background-color: #FFA000; -fx-prompt-text-fill: #ffffff;" text="SignUp" textFill="WHITE" />
<PasswordField fx:id="et_password" layoutX="21.0" layoutY="167.0" prefHeight="25.0" prefWidth="425.0" promptText="Password" style="-fx-background-color: #0288D1; -fx-prompt-text-fill: #ffffff;" />
</children>
</AnchorPane>
And the other one:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="557.0" prefWidth="1012.0" style="-fx-background-color: #0288D1;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="desktop_tasker.FXMLTaskerController">
<children>
<SplitPane dividerPositions="0.5811881188118811" layoutX="-8.0" layoutY="35.0" prefHeight="529.0" prefWidth="1027.0" style="-fx-background-color: #EEEEEE;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ListView fx:id="list_todo" layoutY="-8.0" prefHeight="527.0" prefWidth="584.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>
<ListView fx:id="list_done" layoutY="14.0" prefHeight="527.0" prefWidth="420.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children></AnchorPane>
</items>
</SplitPane>
<MenuBar prefHeight="30.0" prefWidth="1012.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>
</children>
</AnchorPane>
As you can see I tried to use a controler and using a set pasing the String, the problem is that when I open the new scene, his controler resets and the String is no longer there, any idea why or how to solve this?
If you want to set a Controller with myLoader.setController(controler); you must do it before calling Pane myPane = (Pane)myLoader.load(); and also remove fx:controller="desktop_tasker.FXMLTaskerController" from FXMLTasker.fxml, else you get a LoadException: Controller value already specified.
See JavaDoc FXMLLoader.setController(Object controller):
Sets the controller associated with the root object. The value passed to this method is used as the value of the fx:controller attribute. This method must be called prior to loading the document when using controller event handlers when an fx:controller attribute is not specified in the document.
Alternatively you can request the Controller from FXMLLoader:
FXMLTaskerController controler = (FXMLTaskerController)myLoader.getController();
controler.setUser(et_username.getText());
But thereby you can't assume that username is set in initialize(URL url, ResourceBundle rb) and should removed from there.
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
I'm trying to make a program to Log in or register a person.
But I keep getting the same Exception.(InvocationTargetException)
Exception in Application start method
file:/C:/Users/Eigenaar/Desktop/p1g04/dist/SokobanGroep4.jar!/gui/StartScherm.fxml:19
java.lang.reflect.InvocationTargetException
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1099983479.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at gui.StartSchermController.<init>(StartSchermController.java:73)
at StartUp.start(StartUp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/2006606717.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2063964656.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1108411398.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
... 1 more
Exception running application StartUp
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
This is the code from my StartSchermController(the controller from my fxml file)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import domein.DomeinController;
import java.io.IOException;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author Eigenaar
*/
public class StartSchermController extends SplitPane {
#FXML
private ChoiceBox chbTaal;
#FXML
private TextField txfMeldID;
#FXML
private TextField txfMeldWw;
#FXML
private TextField txfRegNaam;
#FXML
private TextField txfRegVNaam;
#FXML
private TextField txfRegWw2;
#FXML
private TextField txfRegWw;
#FXML
private TextField txfRegID;
#FXML
private Label lblMeldAan;
#FXML
private Label lblRegistreer;
#FXML
private Label lblMeldId;
#FXML
private Label lblMeldWw;
#FXML
private Label lblRegNaam,lblRegVNaam,lblRegId,lblRegWw,lblRegWw2;
private final DomeinController dc;
/**
* Initializes the controller class.
*/
public StartSchermController(DomeinController dc) {
this.dc=dc;
FXMLLoader loader=new FXMLLoader(getClass().getResource("StartScherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
try{
chbTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));
} catch(NullPointerException e){
System.out.println(e.getMessage());
}
}
public int geefKeuze(){
int s = chbTaal.getSelectionModel().getSelectedIndex();
return s;
}
#FXML
private void actionchbTaal(ActionEvent event) {
int keuzeTaal = chbTaal.getSelectionModel().getSelectedIndex();
update(dc.setTaal(keuzeTaal));
this.geefKeuze();
}
private void update(ArrayList<String> s) {
lblMeldAan.setText(s.get(0));
this.lblMeldId.setText(s.get(1));
this.lblMeldWw.setText(s.get(2));
this.lblRegistreer.setText(s.get(3));
this.lblRegNaam.setText(s.get(4));
this.lblRegVNaam.setText(s.get(5));
this.lblRegId.setText(s.get(6));
this.lblRegWw.setText(s.get(7));
this.lblRegWw2.setText(s.get(8));
}
}
this is the code from my fxml file generated by JavaFX Scene Builder 2.0
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root dividerPositions="0.19095477386934673" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" type="SplitPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="148.0" prefWidth="598.0">
<children>
<Label layoutX="384.0" layoutY="21.0" text="SOKOBAN" textFill="#d7300f">
<font>
<Font name="Colonna MT" size="24.0" />
</font>
</Label>
<ComboBox fx:id="chbTaal" layoutX="37.0" layoutY="24.0" onAction="#actionchbTaal" prefWidth="150.0" />
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.481421647819063" layoutY="-6.0" prefHeight="331.0" prefWidth="621.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label fx:id="lblMeldAan" layoutX="12.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblMeldId" layoutX="14.0" layoutY="42.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldID" layoutX="97.0" layoutY="38.0" />
<Label fx:id="lblMeldWw" layoutX="14.0" layoutY="83.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldWw" layoutX="97.0" layoutY="79.0" />
<TextField layoutX="30.0" layoutY="165.0" prefHeight="116.0" prefWidth="244.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label fx:id="lblRegistreer" layoutX="14.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblRegNaam" layoutX="82.0" layoutY="41.0" text="Naam:" />
<Label fx:id="lblRegWw" layoutX="51.0" layoutY="148.0" text="Wachtwoord:" />
<Label fx:id="lblRegId" layoutX="51.0" layoutY="112.0" text="GebruikersID:" />
<Label fx:id="lblRegVNaam" layoutX="58.0" layoutY="76.0" text="Voornaam:" />
<Label fx:id="lblRegWw2" layoutX="8.0" layoutY="185.0" text="Controle Wachtwoord:" />
<TextField fx:id="txfRegNaam" layoutX="128.0" layoutY="37.0" />
<TextField fx:id="txfRegVNaam" layoutX="128.0" layoutY="72.0" />
<TextField fx:id="txfRegWw2" layoutX="128.0" layoutY="181.0" />
<TextField fx:id="txfRegWw" layoutX="128.0" layoutY="144.0" />
<TextField fx:id="txfRegID" layoutX="128.0" layoutY="108.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children></AnchorPane>
</items>
</fx:root>
And this is the code from my domeinController where the code to set the language set.(The choice for a language is done with resourcebundels)
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
public class DomeinController {
public ArrayList<String> keuzeTaal(){
ArrayList<String> taal = new ArrayList<>();
taal.add("Nederlands");
taal.add("Français");
taal.add("English");
return taal;
}
public ArrayList<String> setTaal(int i){
ResourceBundle taal=Taal(i);
ArrayList<String> s= new ArrayList<>();
s.add(taal.getString("meldAan"));
s.add(taal.getString("gebruikersID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("registreer"));
s.add(taal.getString("naam"));
s.add(taal.getString("voornaam"));
s.add(taal.getString("gebruikersID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("herhalingWachtwoord"));
return s;
}
public static ResourceBundle Taal(int taalKeuze) {
Locale locale;
ResourceBundle taal;
if (taalKeuze==1) {
locale = Locale.FRENCH;
} else if (taalKeuze==2) {
locale = Locale.ENGLISH;
} else {
locale = new Locale("nl");
}
return ResourceBundle.getBundle("taal\\LabelsBundle", locale);
}
;
}
Thank you in advance. I hope somebody can help me. (sorry for my bad english)
Your controller is declaring chTaal as a ChoiceBox but your FXML is declaring it as a ComboBox. The FXMLLoader can't coerce a conversion between those types so it doesn't set chTaal to anything, leaving it as null. Make the types match in the two files and see if that corrects your issue.
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