Can't read text from TextField input from DialogPane, JavaFX - javafx

I am trying to create a simple Contacts Application in JavaFX. It has a main window with TableView and a DialogPane opening when I want to create new contact. In the DialogPane there are several TextFields from which I want to collect text in order to create a Contacts List. My problem is that when I want to read the input from the DialogPane (from TextFields), which is the separate fxml file with separate controller (separate from main Controller) the application runs an error (java.lang.NullPointerException). And when I put a TextField in my main window FXML file, then I can access this text from the textField just fine. Why do I get an error when I want to read data from DialogPane (Error-->File: Controller.java, I commented the section where I get an error)?? I am stuck. Can anyone suggest what am I doing wrong? Here is my code:
Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("mainwindow.fxml"));
primaryStage.setTitle("Your Contacts");
primaryStage.setScene(new Scene(root, 900, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
public class Controller {
#FXML
private BorderPane mainPanel;
public ObservableList<Contact> contactsList = FXCollections.observableArrayList();
#FXML
public void showAddContactDialog() {
Dialog<ButtonType> dialog = new Dialog<>();
dialog.initOwner(mainPanel.getScene().getWindow());
dialog.setTitle("Add new contact");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("addContactDialog.fxml"));
try {
dialog.getDialogPane().setContent(fxmlLoader.load());
} catch (IOException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText("Couldn't load the dialog");
alert.showAndWait();
e.printStackTrace();
return;
}
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
Optional<ButtonType> result = dialog.showAndWait();
if(result.isPresent() && result.get() == ButtonType.OK) {
//========================================================================================
//here is the error, I cannot read values from addContactDialog controller
ContactController contactController = new ContactController();
String firstName = contactController.getNewContact().getFirstName();
String lastName = contactController.getNewContact().getLastName();
String phoneNumber = contactController.getNewContact().getPhoneNumber();
String emailAddress = contactController.getNewContact().getEmailAddress();
Contact newContact = new Contact(firstName, lastName, phoneNumber, emailAddress);
contactsList.add(newContact);
//or alternatively
// Contact newContact = contactController.getNewContact();
// contactsList.add(newContact);
//========================================================================================
}
}
}
ContactController.java
public class ContactController {
#FXML
private TextField firstNameField;
#FXML
private TextField lastNameField;
#FXML
private TextField phoneNumberFiled;
#FXML
private TextField emailField;
public Contact getNewContact() {
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String phoneNumber = phoneNumberFiled.getText();
String emailAddress = emailField.getText();
Contact newContact = new Contact(firstName, lastName, phoneNumber, emailAddress);
return newContact;
}
Contact.java
public class Contact {
private SimpleStringProperty firstName = new SimpleStringProperty("");
private SimpleStringProperty lastName = new SimpleStringProperty("");
private SimpleStringProperty phoneNumber = new SimpleStringProperty("");
private SimpleStringProperty emailAddress = new SimpleStringProperty("");
public Contact() {
}
public Contact(String firstName, String lastName, String phoneNumber, String emailAddress) {
this.firstName.set(firstName);
this.lastName.set(lastName);
this.phoneNumber.set(phoneNumber);
this.emailAddress.set(emailAddress);
}
public String getFirstName() {
return firstName.get();
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public SimpleStringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getPhoneNumber() {
return phoneNumber.get();
}
public SimpleStringProperty phoneNumberProperty() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber.set(phoneNumber);
}
public String getEmailAddress() {
return emailAddress.get();
}
public SimpleStringProperty emailAddressProperty() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress.set(emailAddress);
}
#Override
public String toString() {
return "Contact{" +
"firstName=" + firstName +
", lastName=" + lastName +
", phoneNumber=" + phoneNumber +
", emailAddress=" + emailAddress +
'}';
}
}
mainwindow.fxml
<BorderPane fx:id="mainPanel" fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml">
<top>
<MenuBar>
<menus>
<Menu text="Contacts">
<items>
<MenuItem text="Add new" onAction="#showAddContactDialog"/>
</items>
<items>
<MenuItem text="Edit" />
</items>
<items>
<MenuItem text="Delete"/>
</items>
<items>
<MenuItem text="Exit"/>
</items>
</Menu>
</menus>
<menus>
<Menu text="Info">
<items>
<MenuItem text="About"/>
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<TableView fx:id="contactsTable">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
</columnResizePolicy>
<columns>
<TableColumn text="First Name">
<cellValueFactory>
<PropertyValueFactory property="firstName"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory>
<PropertyValueFactory property="lastName"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="Phone Number">
<cellValueFactory>
<PropertyValueFactory property="phoneNumber"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="Email">
<cellValueFactory>
<PropertyValueFactory property="emailAddress"/>
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</center>
</BorderPane>
addContactDialog.fxml
<DialogPane fx:controller="sample.ContactController" xmlns:fx="http://javafx.com/fxml">
<headerText>
Fill in the information for the new Contact
</headerText>
<content>
<GridPane vgap="10" hgap="10">
<Label text="First Name: " GridPane.rowIndex="0" GridPane.columnIndex="0"/>
<TextField fx:id="firstNameField" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
<Label text="Last Name: " GridPane.rowIndex="1" GridPane.columnIndex="0"/>
<TextField fx:id="lastNameField" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
<Label text="Phone Number: " GridPane.rowIndex="2" GridPane.columnIndex="0"/>
<TextField fx:id="phoneNumberField" GridPane.rowIndex="2" GridPane.columnIndex="1"/>
<Label text="Notes: " GridPane.rowIndex="3" GridPane.columnIndex="0"/>
<TextField fx:id="notesField" GridPane.rowIndex="3" GridPane.columnIndex="1"/>
</GridPane>
</content>
</DialogPane>

Ok, I found the mistake - I made a typo in the ContactController.java file. It should have phoneNumberField instead of phoneNumberFiled String...

Did you tried putting .toString
like this
String firstName = contactController.getNewContact().getFirstName().toString();

Related

Null pointer exception between controllers on calling a method javaFX

Hello I am working on a little project which javaFX, basically, I have two windows. the first one is the user account interface (Account.FXML) which allow the user to access his account and it contains an edit button that shows up the second window (edit.FXML) once is clicked. the second window allows the user to update his infos. what I want to do is to update the tableView in the first window when the user click on edit button in the second window so I am trying to call the UserDetails() metodh from the first window through communication between controllers but I get a Null pointer exception in (EditController.java) at line controller.UserDetails(); can you tell me how to fix this error ?
accountController.java:
public class accountController implements Initializable {
#FXML
private TableColumn<User, Integer> id;
#FXML
private TableColumn<User, String> username;
#FXML
private TableColumn<User, String> email;
#FXML
private TableColumn<User, String> password;
#FXML
private TableView<User> table;
public void userDetails() {
try {
userDao dao = new userDao();
ObservableList<User> list = dao.LoadAll();
id.setCellValueFactory(new PropertyValueFactory<>("id"));
username.setCellValueFactory(new PropertyValueFactory<>("username"));
email.setCellValueFactory(new PropertyValueFactory<>("email"));
password.setCellValueFactory(new PropertyValueFactory<>("password"));
table.setItems(list);
table.setEditable(true);
} catch (Exception ex) {
Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
}
}
#FXML
void editUserDetails(ActionEvent event) {
UserModel item = table.getSelectionModel().getSelectedItem();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Edit.fxml"));
Parent root = loader.load();
editController controller = (editController) loader.getController();
controller.txtId.setText(String.valueOf(item.getId()));
controller.txtId.setEditable(false);
controller.txtUserName.setText(item.getUserName());
controller.txtEmail.setText(item.getEmail());
controller.txtPassword.setText(item.getPassword());
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
Stage stage = new Stage();
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.TRANSPARENT);
stage.showAndWait();
} catch (Exception ex) {
Logger.getLogger(ConsultationController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void initialize(URL url, ResourceBundle rb) {
UserDetails();
}
}
account.FXML
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.home.accountController">
<children>
<TableView fx:id="table" layoutX="52.0" layoutY="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="col_id" prefWidth="50.0" text="id" />
<TableColumn fx:id="col_username" prefWidth="75.0" text="USER NAME" />
<TableColumn fx:id="col_email" prefWidth="180.0" text="Email" />
<TableColumn fx:id="col_password" prefWidth="180.0" text="Password" />
</columns>
</TableView>
<Button mnemonicParsing="false" onAction="#editUserDetails" prefHeight="40.0" prefWidth="261.0" styleClass="btnAdd" stylesheets="#style.css" text="Edit" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" />
</padding>
</AnchorPane>
editController.java
public class editController implements Initializable {
#FXML
protected TextField txtId;
#FXML
protected TextField txtUserName;
#FXML
protected TextField txEmail;
#FXML
protected TextField txtPassowrd;
#FXML
void update(ActionEvent event) {
int id = Integer.parseInt(txtId.getTexT());
String username = txtUserName.getText();
String email = txtEmail.getText();
String password = txtPassword.getText();
User user = new User();
user.setId(id);
user.setUserName(username);
user.setEmail(email);
user.setPassword(password));
try {
int count = userDao.getInstance().update(user); // userDao is the class that contains the SQL statement.
if (count == 1) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("account.fxml"));
accountController controller = (accountController) loader.getController();
controller.userDetails();
} else {
Alert error = new Alert(Alert.AlertType.ERROR, "Faild", ButtonType.OK);
error.showAndWait();
}
} catch (Exception ex) {
Logger.getLogger(EditController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
edit.FXML
<AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="589.0" prefWidth="600.0" style="-fx-border-width: 1px; -fx-border-color: black;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.home.editController">
<HBox layoutX="137.0" layoutY="42.0" prefHeight="472.0" prefWidth="600.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="42.0">
<children> .
.
.
<Button fx:id="update" mnemonicParsing="false" onAction="#update" text="Update" />
<Button fx:id="exit" layoutX="10.0" layoutY="398.0" mnemonicParsing="false" onAction="#exit" text="Quitter" />
</children>
</HBox>
</children>
</AnchorPane>
user.java
public class User {
private final IntegerProperty id = new SimpleIntegerProperty();
private final StringProperty usename = new SimpleStringProperty();
private final StringProperty email = new SimpleStringProperty();
private final StringProperty password = new SimpleStringProperty();
public user(Integer id, String username, String email, String password) {
setId(id);
setUserName(username);
setEmail(email);
setPassword(Password);
}
public User() {
}
public final IntegerProperty IdProperty() {
return this.id;
}
public final java.lang.Integer getId() {
return this.IdProperty().get();
}
public final void setId(final java.lang.Integer id) {
this.idProperty().set(id);
}
public final StringProperty UserNameProperty() {
return this.username;
}
public final java.lang.String getUserNmae() {
return this.UserNameProperty().get();
}
public final void setUserName(final java.lang.String username) {
this.UserNameProperty().set(username);
}
public final StringProperty EmailProperty() {
return this.email;
}
public final java.lang.String getEmail() {
return this.EmailProperty().get();
}
public final void setEmail(final java.lang.String email) {
this.EmailProperty().set(email);
}
public final StringProperty PasswordProperty() {
return this.password;
}
public final java.lang.String getPassword() {
return this.PasswordProperty().get();
}
public final void setPassword(final java.lang.String Password) {
this.PasswordProperty().set(password);
}

how to declare date value property for tableview

I need to pass the date property to the setCellValueFactory , this is the code for the seters and geters of my Persona class, what is the correct method to do that, i think i having an issue whit the correct declaration, so i need a real direction here, i need to declare some date in the java.sql.date class, do some reference? any idea?. please a little help here.
package application;
import java.time.LocalDate;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Persona {
private StringProperty nombres;
private StringProperty apellidos;
private IntegerProperty id_cliente;
private ObjectProperty <LocalDate>fechacliente;
public Persona ( String nombres, String apellidos, Integer id_cliente, Object fechacliente) {
this.nombres= new SimpleStringProperty (nombres);
this.apellidos= new SimpleStringProperty ( apellidos);
this.id_cliente=new SimpleIntegerProperty (id_cliente);
this.fechacliente= new SimpleObjectProperty<LocalDate>();
}
public Object getFecha() {
return fechacliente.get();
}
public void setFecha(Object fechacliente) {
this.fechacliente=new SimpleObjectProperty<>();
}
public String getNombres() {
return nombres.get();
}
public void setNombres(String nombres) {
this.nombres=new SimpleStringProperty (nombres);
}
public String getApellidos() {
return apellidos.get();
}
public void setApellidos(String apellidos) {
this.apellidos=new SimpleStringProperty ( apellidos);
}
public Integer getId_cliente() {
return id_cliente.get();
}
public void setid_cliente(Integer id_cliente) {
this.id_cliente=new SimpleIntegerProperty (id_cliente);
}
}
Some of Controller here that set the values to the tableview
public void initialize(URL arg0, ResourceBundle arg1) {
clienteid.setCellValueFactory(new PropertyValueFactory <Persona, Integer>("id_cliente"));
nombrescol.setCellValueFactory(new PropertyValueFactory <Persona, String>("nombres"));
apellidoscol.setCellValueFactory(new PropertyValueFactory <Persona, String>("apellidos"));
fechacli.setCellValueFactory(new PropertyValueFactory <Persona, LocalDate>("fechacliente"));
seleccionaregistros();
seleccionanombre();
seleccionapellido();
}
this is the method i am using to retrieve the data in the tableview but the date does not show
public void seleccionaregistros() {
ObservableList <Persona> data =FXCollections.observableArrayList();
Connection conn=null;{
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
Statement mostrar=conn.createStatement();
ResultSet rs;
rs= mostrar.executeQuery("select * from cliente");
while ( rs.next() )
{
data.add(new Persona(
rs.getString("nombre"),
rs.getString("apellido"),
rs.getInt("id"),
rs.getDate(4)
));
tablacliente.setItems(data);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
The date does not show in the tableview , i need to format the date to pass that value in the tableview i think. please some orientation here be helpful.
this is my FXML Code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="497.0" prefWidth="943.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConexionController">
<children>
<Pane layoutY="-3.0" prefHeight="605.0" prefWidth="1084.0">
<children>
<Button fx:id="btn" layoutX="145.0" layoutY="109.0" mnemonicParsing="false" onAction="#cargarconexion" prefHeight="46.0" prefWidth="117.0" text="Prueba Conexion" />
<Button fx:id="mtn" layoutX="15.0" layoutY="183.0" mnemonicParsing="false" onAction="#cargarregistro" prefHeight="46.0" prefWidth="117.0" text="Inserta Registro" />
<Label layoutX="14.0" layoutY="279.0" prefHeight="17.0" prefWidth="105.0" text="NOMBRES" />
<Label layoutX="15.0" layoutY="327.0" prefHeight="17.0" prefWidth="79.0" text="APELLIDOS" />
<TextField fx:id="nm" layoutX="159.0" layoutY="275.0" prefHeight="25.0" prefWidth="149.0" />
<TextField fx:id="ap" layoutX="159.0" layoutY="323.0" />
<Button fx:id="lmp" layoutX="159.0" layoutY="484.0" mnemonicParsing="false" onAction="#borrarcasillatexto" prefHeight="25.0" prefWidth="150.0" text="Limpiar Texto" />
<TableView fx:id="tablacliente" layoutX="355.0" layoutY="15.0" prefHeight="383.0" prefWidth="696.0">
<columns>
<TableColumn fx:id="clienteid" prefWidth="159.0" text="ID" />
<TableColumn fx:id="nombrescol" prefWidth="159.0" text="NOMBRES" />
<TableColumn fx:id="apellidoscol" minWidth="0.0" prefWidth="169.0" text="APELLIDOS" />
<TableColumn fx:id="fechacli" prefWidth="235.0" text="FECHA DE NACIMIENTO" />
</columns>
</TableView>
<Button fx:id="mts" layoutX="15.0" layoutY="109.0" mnemonicParsing="false" onAction="#mostrartodo" prefHeight="46.0" prefWidth="117.0" text="Mostrar" />
<TextField fx:id="bq" layoutX="554.0" layoutY="417.0" prefHeight="25.0" prefWidth="149.0" />
<Button fx:id="bqd" layoutX="758.0" layoutY="417.0" mnemonicParsing="false" onAction="#buscanm" prefHeight="25.0" prefWidth="155.0" text="BUSCAR NOMBRE" />
<Button fx:id="bqape" layoutX="758.0" layoutY="458.0" mnemonicParsing="false" onAction="#buscaape" prefHeight="25.0" prefWidth="155.0" text="BUSCAR POR APELLIDO" />
<TextField fx:id="bqa" layoutX="554.0" layoutY="458.0" />
<ComboBox layoutX="159.0" layoutY="430.0" prefWidth="150.0" />
<Label layoutX="15.0" layoutY="434.0" prefHeight="17.0" prefWidth="55.0" text="GENERO" />
<MenuBar fx:id="menucombo" layoutY="3.0">
<menus>
<Menu mnemonicParsing="false" text="Agregar">
<items>
<MenuItem mnemonicParsing="false" onAction="#inicializacombo" text="Datos Cliente" />
</items>
</Menu>
</menus>
</MenuBar>
<Button fx:id="botonborrar" layoutX="758.0" layoutY="507.0" mnemonicParsing="false" onAction="#borraregistroid" prefHeight="25.0" prefWidth="155.0" text="BORRAR REGISTRO" />
<TextField fx:id="borrar" layoutX="554.0" layoutY="507.0" />
<DatePicker fx:id="mifecha" layoutX="158.0" layoutY="371.0" prefHeight="25.0" prefWidth="150.0" />
<Label layoutX="15.0" layoutY="375.0" prefHeight="17.0" prefWidth="150.0" text="FECHA DE NACIMIENTO" />
</children>
</Pane>
</children>
</AnchorPane>
You're using JavaFX properties incorrectly. A property for a JavaFX Bean needs a getter, setter (if writable), and a property getter. Also, the setter should not be creating a new property each time; it should be setting the value of the existing property.
To simplify your example, if we have a Person with a single property, name, it would look like this:
public class Person {
private final StringProperty name = new SimpleStringProperty(this, "name");
public Person(String name) {
setName(name);
}
public final void setName(String name) { // setter
this.name.set(name);
}
public final String getName() { // getter
return name.get();
}
public final StringProperty nameProperty() { // property getter
return name;
}
}
Note: You can still initialize the properties in the constructor, instead of at the field declaration, if you want.
To use the name property in a TableView or TreeTableView you would simply return it in the cellValueFactory. Example using a TableView:
TableView<Person> table = new TableView<>();
TableColumn<Person, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(features -> features.getValue().nameProperty());
table.getColumns().add(nameCol);
Another issue with your code is that you're using a raw ObjectProperty. Don't use raw types. Instead, you should be using an ObjectProperty<Date> where Date is java.sql.Date. For example:
public class Person {
private final ObjectProperty<Date> clientDate = new SimpleObjectProperty<>(this, "clientDate");
public final void setClientDate(Date clientDate) {
this.clientDate.set(clientDate);
}
public final Date getClientDate() {
return clientDate.get();
}
public final ObjectProperty<Date> clientDateProperty() {
return clientDate;
}
}
Note: It'd probably be better to use one of the java.time classes (e.g. LocalDate, OffsetDateTime, etc...).
And again, to add it to a TableView it'd look like:
TableView<Person> table = new TableView<>();
TableColumn<Person, Date> clientDateCol= new TableColumn<>("Client Date");
clientDateCol.setCellValueFactory(features -> features.getValue().clientDateProperty());
table.getColumns().add(clientDateCol);

JAVAFX. How make TableViewCell editable using FXML?

I want to make TableViewCell using only fxml. How can I do it.
Now I have a model class DuplicateFileInfo
class DuplicateFileInfo(var id: Long, var path: String, var editableField: String?) {}
And I have TableView
<TableView AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"
editable="true"
layoutX="121.0" layoutY="6.0" fx:id="duplicatesList">
<columns>
<TableColumn prefWidth="300.0" text="%file.filename" fx:id="fileNameColumn" editable="false">
<cellValueFactory>
<PropertyValueFactory property="path" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="150.0" text="%file.EditableField" fx:id="editableColumn">
<cellValueFactory>
<PropertyValueFactory property="editableField" />
</cellValueFactory>
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn" />
</cellFactory>
</TableColumn>
</columns>
</TableView>
In this case I have editable table view. But the value doesn't set to model after editing is finish.
Is it possible to make this work without codding?
Thanks for James_D
I could get the result.
The model class with kotlin should be like this
class DuplicateFileInfo(id: Long, path: String, shouldBeDeleted: Boolean) {
private val id: LongProperty
private val path: StringProperty
private val shouldBeDeleted: BooleanProperty
init {
this.id = SimpleLongProperty(id)
this.path = SimpleStringProperty(path)
this.shouldBeDeleted = SimpleBooleanProperty(shouldBeDeleted)
}
fun getId(): Long {
return id.get()
}
fun idProperty(): LongProperty {
return id
}
fun setId(id: Long) {
this.id.set(id)
}
fun getPath(): String {
return path.get()
}
fun pathProperty(): StringProperty {
return path
}
fun setPath(path: String) {
this.path.set(path)
}
var isShouldBeDeleted: Boolean
get() = shouldBeDeleted.get()
set(shouldBeDeleted) = this.shouldBeDeleted.set(shouldBeDeleted)
fun shouldBeDeletedProperty(): BooleanProperty {
return shouldBeDeleted
}
override fun toString(): String {
val sb = StringBuffer("DuplicateFileInfo{")
sb.append("id=").append(id.get())
sb.append(", path=").append(path.get())
sb.append(", shouldBeDeleted=").append(shouldBeDeleted.get())
sb.append('}')
return sb.toString()
}
}

javafx-TableView as combobox popup (Tried and able to achieve partially. need help further)

What i need:
Need an editable combobox which filters the data on the popup upon typing and first matching item should be highlighted and should set as the text in the combo upon pressing enter.
The popup should be a tableview with 2 or 3 columns. (screen shot attached.)(in the image it is a textfield but i prefer combo so that if the user is not sure about the values, he can click the combo button and see the entire list and select one.)
I will be binding some data as a source to the tableview which should act as the popup for the combobox.
I know i should go for a custom control but dont know where to start?
Reference URL: enter link description here
Thanks in Advance.
What i have tried so far:
Guys,
With the idea you guys gave, I have tried this so far and able to achieve.
(For now, i am not showing the tableview dynamically below the text field (eventually dats wat i want))
1. A tableview with static data is already loaded and added to the scene.
2. Having a text field below the tableview. (this is named as txt)
3. Having another text field below the first text field(this is named as txt1) (when i press tab from the previous text field cursor should come here)
i have a predicate set for the table and i am updating my predicate as the user types in the txt. (working)
Able to filter the table and the first matching row will highlighted. (working)
When the user press either "Tab" or "Enter" the highlighted row in the tableview should be set as the value in the txt.(working)
Needed:
1. When i press "Tab" or "Enter", the highlighted row will be set as the value in the text field (txt) and also the cursor should move to next focusable node. in my case it is the 2nd text field (txt1). I dont want to say txt1.requestFocus() bcoz in realtime, i have many text fields in the scene and this control will be a user control. so cant hardcode anything.
When the user types some text in the text field(not full text. eg: "do" where my table contains "Dom", "Don"), if there are multiple matches, currently both the records will be displayed in the table but the first one will be highlighted. User should be able to select the 2nd row if he wants by pressing the down arrow from the text field itself.
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
VBox root = FXMLLoader.load(this.getClass().getResource("MainView.fxml"));
Scene scene = new Scene(root,500,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Popup;
public class MainController implements Initializable
{
private #FXML TableView<Person> table;
private #FXML TableColumn<Person, String> firstNameCol;
private #FXML TableColumn<Person, String> lastNameCol;
private #FXML TableColumn<Person, String> emailCol;
private #FXML TableColumn<Person, Integer> ageCol;
private #FXML TextField txt;
private #FXML TextField txt1;
private #FXML Button btn;
#Override
public void initialize(URL location, ResourceBundle resources)
{
Platform.runLater(new Runnable() {
#Override
public void run() {
txt.requestFocus();
}
});
ObservableList<Person> obsList =FXCollections.observableArrayList();
obsList.add(new Person("Sam", "P1LasttName", "P1Email#gmail.com", 20));
obsList.add(new Person("Dom", "P2LasttName", "P2Email#gmail.com", 30));
obsList.add(new Person("Ken", "P3LasttName", "P3Email#gmail.com", 40));
obsList.add(new Person("Don", "P4LasttName", "P4Email#gmail.com", 50));
obsList.add(new Person("Tom", "P5LasttName", "P5Email#gmail.com", 60));
FilteredList<Person> filteredList = new FilteredList<>(obsList, p->true);
table.setItems(filteredList);
txt.textProperty().addListener((obs, oldValue, newValue) ->{
filteredList.setPredicate(person-> {
if(newValue == null || newValue.isEmpty())
return true;
if(person.getFirstName().trim().toLowerCase().contains(newValue.toLowerCase()))
return true;
return false;
});
Platform.runLater(new Runnable() {
#Override
public void run()
{
// we don't want repeated selections
table.getSelectionModel().clearSelection();
//get the focus
table.requestFocus();
//select first item in TableView model
table.getSelectionModel().selectFirst();
//set the focus on the first element
table.getFocusModel().focus(0);
//render the selected item in the TableView
//tableClickHandler(null);
}
});
Platform.runLater(new Runnable() {
#Override
public void run()
{
txt.requestFocus();
txt.end();
}
});
});
table.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if(event.getCode() == KeyCode.ENTER)
{
txt.setText(table.getSelectionModel().getSelectedItem().getFirstName());
}
}
});
txt.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if(event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB)
//if(event.getCode() == KeyCode.ENTER)
{
txt.setText(table.getSelectionModel().getSelectedItem().getFirstName());
/*Platform.runLater(new Runnable() {
public void run() {
txt1.requestFocus();
}
});*/
}
}
});
/*txt.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if(event.getCode() == KeyCode.TAB)
{
//txt.setText(table.getSelectionModel().getSelectedItem().getFirstName());
if(txt.getSkin() instanceof BehaviorSkinBase)
{
//((BehaviorSkinBase)txt.getSkin()).getBehavior().traverseNext();
BehaviorBase x = ((BehaviorSkinBase)txt.getSkin()).getBehavior();
((TextFieldBehavior)x).callAction("TraverseNext");
}
event.consume();
}
}
});*/
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
/*
Popup popup = new Popup();
popup.getContent().add(new TableView());
//popup.show(txt, txt.localToScreen(0, 0).getX() + txt.getWidth()/2, txt.localToScreen(0, 0).getY() + txt.getHeight());
popup.show(txt, txt.localToScreen(0, 0).getX(), txt.localToScreen(0, 0).getY() + txt.getHeight() + 2);
*/
Parent vbox = null;
try {
vbox = FXMLLoader.load(this.getClass().getResource("TableView.fxml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Popup popup = new Popup();
popup.getContent().add(vbox);
//popup.show(txt, txt.localToScreen(0, 0).getX() + txt.getWidth()/2, txt.localToScreen(0, 0).getY() + txt.getHeight());
//popup.show(txt, txt.localToScreen(0, 0).getX(), txt.localToScreen(0, 0).getY() + txt.getHeight() + 2);
popup.show(txt, txt.localToScreen(0, 0).getX(), txt.localToScreen(0, 0).getY() + txt.getHeight() + 2);
}
});
}
}
Person.java
package application;
public class Person
{
private String firstName;
private String lastName;
private String email;
private Integer age;
public Person(){}
public Person(String firstName, String lastName, String email, Integer age)
{
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Integer getAge()
{
return age;
}
public void setAge(Integer age)
{
this.age = age;
}
}
application.css
.table-row-cell:selected
{
-fx-background-color: lightgreen;
/* the below style will remove the border lines of the selected row */
-fx-table-cell-border-color: transparent;
}
MainView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.VBox?>
<!-- <?import application.Person?> -->
<VBox spacing="10.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<TableView fx:id="table" prefHeight="250.0" prefWidth="437.0">
<columns>
<TableColumn fx:id="firstNameCol" prefWidth="120.0" text="First Name">
<cellValueFactory><PropertyValueFactory property="firstName" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="lastNameCol" prefWidth="120.0" text="Last Name">
<cellValueFactory><PropertyValueFactory property="lastName" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="emailCol" prefWidth="120.0" text="Email">
<cellValueFactory><PropertyValueFactory property="email" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="ageCol" prefWidth="75.0" text="Age">
<cellValueFactory><PropertyValueFactory property="age" /></cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Button text="Button" fx:id="btn"/>
<TextField fx:id="txt" promptText="Type to Filter" />
<TextField fx:id="txt1" promptText="Focus should be here when tab is pressed from pervious txt" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
TableView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.VBox?>
<?import application.Person?>
<?import javafx.collections.*?>
<!--
<VBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65">
<children>
-->
<!-- <TableView xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:id="table" prefHeight="160.0" prefWidth="440.0"> -->
<TableView xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:id="table" prefHeight="140.0">
<columns>
<TableColumn fx:id="firstNameCol" prefWidth="120.0" text="First Name">
<cellValueFactory><PropertyValueFactory property="firstName" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="lastNameCol" prefWidth="120.0" text="Last Name">
<cellValueFactory><PropertyValueFactory property="lastName" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="emailCol" prefWidth="120.0" text="Email">
<cellValueFactory><PropertyValueFactory property="email" /></cellValueFactory>
</TableColumn>
<TableColumn fx:id="ageCol" prefWidth="75.0" text="Age">
<cellValueFactory><PropertyValueFactory property="age" /></cellValueFactory>
</TableColumn>
</columns>
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
<items>
<FXCollections fx:factory="observableArrayList">
<Person firstName="P1FirstName" lastName="P1LasttName" email="P1Email#gmail.com" age="20"/>
<Person firstName="P2FirstName" lastName="P2LasttName" email="P2Email#gmail.com" age="30"/>
<Person firstName="P3FirstName" lastName="P3LasttName" email="P3Email#gmail.com" age="40"/>
<Person firstName="P4FirstName" lastName="P4LasttName" email="P4Email#gmail.com" age="50"/>
<Person firstName="P5FirstName" lastName="P5LasttName" email="P5Email#gmail.com" age="60"/>
</FXCollections>
</items>
</TableView>
<!--
</children>
</VBox>
-->
Any help is appreciated. Thanks!

JavaFX, why is TableColumn null?

As you will see I am a massive noob when it comes to Java and Javafx. I have spent a lot of time reading around (various forum posts, and tuts) and trying to figure out myself where I am getting this issue but it has come to the point for me to post for feedback from someone who knows their business.
When replying, please could you take the time to also explain why something isn't working and some general pointers? Here is what I have so far (my FXML and my two classes) any pointers would be fantastic!!
My FXML;
<Pane id="myScene" fx:id="myScene" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.TestController">
<children>
<TableView id="employeesTable" fx:id="employeesTable" layoutX="131.0" layoutY="64.0" prefHeight="200.0" prefWidth="360.0">
<columns>
<TableColumn id="colFirstName" fx:id="colFirstName" prefWidth="75.0" text="First Name" />
<TableColumn id="colLastName" fx:id="colLastName" prefWidth="75.0" text="Last Name" />
<TableColumn id="colEmail" fx:id="colEmail" prefWidth="75.0" text="email" />
</columns>
</TableView>
</children>
</Pane>
Now the Employee class I have;
public class Employee {
private StringProperty firstName;
private StringProperty lastName;
private StringProperty email;
public Employee(String a, String b, String c) {
this.firstName = new SimpleStringProperty(a);
this.lastName = new SimpleStringProperty(b);
this.email = new SimpleStringProperty(c);
}
public Employee() {
}
public String getFirstName() {
return firstName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getEmail() {
return email.get();
}
public StringProperty emailProperty() {
return email;
}
public void setEmail(String email) {
this.email.set(email);
}
}
And finally the class for my test controller is
public class TestController {
public Label login;
public TextField loginUserName;
public PasswordField loginPassword;
public TextField testOutput;
#FXML TableView<Employee> employeesTable;
#FXML TableColumn<Employee, String> colFirstName;
#FXML TableColumn<Employee, String> colLastName;
#FXML TableColumn<Employee, String> colEmail;
#FXML Pane myScene;
//public javafx.scene.control.TableView employeesTable;
private ObservableList<Employee> myData;
private MainController MainController;
public void loadEmployeeForm(ActionEvent actionEvent) throws IOException, SQLException, ClassNotFoundException {
myData = FXCollections.observableArrayList(DBCON.getEmployees());
System.out.println(myData.size());
Parent root = FXMLLoader.load(getClass().getResource("frmEmployees.fxml"));
Scene myScene = new Scene( root );
sample.MainController.setScene(myScene);
colFirstName.setCellValueFactory(new PropertyValueFactory<Employee, String>("firstName"));
colLastName.setCellValueFactory(new PropertyValueFactory<Employee, String>("lastName"));
colEmail.setCellValueFactory(new PropertyValueFactory<Employee, String>("email"));
employeesTable.setItems(null);
employeesTable.setItems(myData);
employeesTable.setVisible(true);
}
I get a null pointer exception when I go to set colFirstName to the property value factory which make me think I haven't initialized something somewhere but I am utterly clueless on how to go about adding that.
If I add in lines such as;
TableColumn colFirstName = new TableColumn("firstName");
for each of my columns and the tablename it works (ie it doesn't throw a load of error messages at me) that way but then I don't get any data loading into the tableview because I think that's me creating a new tableView not using the one generated from the FXML?
I have a feeling it will be very simple, but as I said I am a massive noob and any points would be much obliged.
Thanks
Mark
Update 1;
The method for load employee form is called from a button on myMain.fxml;
<GridPane alignment="CENTER" hgap="10" prefHeight="300" prefWidth="300" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="sample.TestController" stylesheets="/sample/myFirst.css">
<children>
<Button onAction="#login" text="Login" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER" />
<Button text="GoEmployees" onAction="#loadEmployeeForm" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" />
<Label fx:id="login" GridPane.rowIndex="1" />
<Label text="UserName" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label text="Password" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="loginUserName" GridPane.rowIndex="1" GridPane.columnIndex="1" />
<PasswordField fx:id="loginPassword" GridPane.rowIndex="2" GridPane.columnIndex="1" blendMode="OVERLAY" />
<TextField fx:id="testOutput" GridPane.rowIndex="4" GridPane.columnIndex="0" GridPane.columnSpan="3" />
</children>
<columnConstraints>
<ColumnConstraints prefWidth="125.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="50.0" />
</rowConstraints>
<padding>
<Insets bottom="10.0" left="9.0" right="10.0" top="10.0" />
</padding>
</GridPane>
Is having my testController control two different FXMLs a problem/a no go?
When the FXMLLoader tries to load an fxml file, it will create new instance of the controller class defined with fx:controller in fxml file. Then it creates and maps the #FXML annotated fields with fx:id components in fxml file. Finally, it calls the controller's initialize() method. You can get the instantiated controller with fxmlloader.getController() after fxmlloader.load().
According to this basic work flow, the pitfall in your code is:
myMain.fxml's controller is TestController, but myMain.fxml does not contain TableColumns with fx:id colFirstName etc. So these fields are null, when the myMain.fxml has been loaded. As a result, there will be NPE in loadEmployeeForm() while trying to use these fields.
Move the TableView and TableColumns to frmEmployees.fxml's controller, and configure them (setCellValueFactory, initial data etc.) in this controller's initialize() method.
You never use your Employee(String, String, String) constructor. This constructor initializes the firstName, lastName, and email. Otherwise, your references will point to nothing.

Resources