Issue
I have some issue with getSelectedRow. I can't make method that puts text from table's row to textfield. I tried to make table.setOnMouseClicked((MouseEvent event) -> {getTextFromSelectedRow();});
but everytime got Cannot infer functional interface type
.
Help me please with that method. I need to get selected item from row when it's clicked.
And is it right that I do everything in Controller class?
Controller class
package com.cascado.application.application;
import static com.cascado.application.common.Constants.REGEX;
import com.cascado.application.common.User;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private TableView<User> table;
@FXML
private TableColumn<User, String> idColumn;
@FXML
private TableColumn<User, String> firstNameColumn;
@FXML
private TableColumn<User, String> lastNameColumn;
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField idField;
private ObservableList<User> users;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
idColumn.setCellValueFactory(new PropertyValueFactory<User, String>("ID"));
firstNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("firstName"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("lastName"));
users = table.getItems();
}
@FXML
private void addButton(ActionEvent actionEvent) {
users.add(newUser(0, 1, 2));
table.setItems(users);
clearFields();
}
private void selectRow(){
table.setOnMouseClicked((MouseEvent event) -> {
getTextFromSelectedRow();
});
getTextFromSelectedRow();
}
private void getTextFromSelectedRow(){
User selectedUser = table.getSelectionModel().getSelectedItem();
idField.setText(selectedUser.getID());
firstNameField.setText(selectedUser.getFirstName());
lastNameField.setText(selectedUser.getLastName());
}
@FXML
private void updateButton(ActionEvent actionEvent) {
}
@FXML
private void deleteButton(ActionEvent actionEvent) {
}
private String[] fieldsTextArray(){
return (idField.getText() + REGEX + firstNameField.getText() + REGEX + lastNameField.getText()).split(REGEX);
}
private void clearFields(){
firstNameField.clear();
lastNameField.clear();
idField.clear();
}
private User newUser(int id, int firstName, int lastName){
// values in textfields message array
return new User(fieldsTextArray()[id], fieldsTextArray()[firstName], fieldsTextArray()[lastName]);
}
}
FXML
<?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.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="389.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.cascado.application.application.Controller">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Open" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About Database" />
<MenuItem mnemonicParsing="false" text="Help" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="353.0" prefWidth="407.0" VBox.vgrow="ALWAYS">
<children>
<Label layoutX="14.0" layoutY="14.0" text="ID" />
<Label layoutX="14.0" layoutY="50.0" text="First name" />
<Label layoutX="14.0" layoutY="84.0" text="Last name" />
<TextField layoutX="87.0" layoutY="10.0" prefHeight="26.0" prefWidth="256.0" fx:id="idField"/>
<TextField layoutX="87.0" layoutY="47.0" prefHeight="26.0" prefWidth="256.0" fx:id="firstNameField"/>
<TextField layoutX="87.0" layoutY="84.0" prefHeight="26.0" prefWidth="256.0" fx:id="lastNameField"/>
<HBox layoutX="14.0" layoutY="120.0">
<children>
<Button mnemonicParsing="false" prefHeight="26.0" prefWidth="128.0" text="Add" onAction="#addButton"/>
<Button mnemonicParsing="false" prefHeight="26.0" prefWidth="125.0" text="Update" onAction="#updateButton"/>
<Button mnemonicParsing="false" prefHeight="26.0" prefWidth="125.0" text="Delete" onAction="#deleteButton"/>
</children>
</HBox>
<TableView layoutX="13.0" layoutY="151.0" prefHeight="200.0" prefWidth="378.0" fx:id="table">
<columns>
<TableColumn prefWidth="48.1036491394043" text="ID" fx:id="idColumn"/>
<TableColumn prefWidth="149.6151885986328" text="First name" fx:id="firstNameColumn"/>
<TableColumn prefWidth="179.3848114013672" text="Last name" fx:id="lastNameColumn"/>
</columns>
</TableView>
</children>
</AnchorPane>
</children>
</VBox>
Application class
package com.cascado.application.application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Application extends javafx.application.Application {
public static void run(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/resources.fxml"));
Parent parent = loader.load();
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.setTitle("Database");
primaryStage.show();
}
}
REGEX class
package com.cascado.application.common;
public class Constants {
public static final String REGEX = "@#!#@";
}
Solution
Import is wrong.
Don't use:
java.awt.event.MouseEvent
Use:
javafx.scene.input.MouseEvent
Answered By - jewelsea
Answer Checked By - Robin (JavaFixing Admin)