Issue
It is necessary to draw a conclusion from MySQL in the tableView. Created an entity:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
//
@Entity
@Table(name = "users2") //создание бд и таблицы юзеров
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String login;
@Column(nullable = false, length = 16)
private String password;
@Column(nullable = false, length = 50)
private String surname;
@Column(nullable = false, length = 50)
private String name;
//equals, hashcode, toString
}
Based on the entity, I made a controller:
public class Controller implements Initializable
{
private Stage stage;
public void setStage(Stage stage)
{
this.stage = stage;
}
ObservableList<User> usersData = FXCollections.observableArrayList();
@Autowired
private UserRepository userRepository;
@FXML
private TableView<User> tableUsers;
@FXML
private TableColumn<User, Long> userIdColumn;
@FXML
private TableColumn<User, String> userLoginColumn;
@FXML
private TableColumn<User, String> userPassColumn;
@FXML
private TableColumn<User, String> userSurnameColumn;
@FXML
private TableColumn<User, String> userNameColumn;
@Override
public void initialize(URL location, ResourceBundle resources)
{
//id, login, password, surname, name
userIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
userLoginColumn.setCellValueFactory(new PropertyValueFactory<>("login"));
userPassColumn.setCellValueFactory(new PropertyValueFactory<>("password"));
userSurnameColumn.setCellValueFactory(new PropertyValueFactory<>("surname"));
userNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
}
@FXML
private void displayData() throws IOException
{
fillTable();
}
private void fillTable()
{
for (User usersDatum : userRepository.findAll())
{
usersData.add(new User(
usersDatum.getId(),
usersDatum.getLogin(),
usersDatum.getPassword(),
usersDatum.getSurname(),
usersDatum.getName()));
}
tableUsers.setItems(usersData);
}
}
Then I made a repository and a database query and tried to output it to the console. Happened!
@Repository
public interface UserRepository extends JpaRepository<User, Long>
{
@Query(value = "select * from users2", nativeQuery = true)
List<User> findAll();
}
<Button mnemonicParsing="false" prefHeight="30.0" prefWidth="75.0" text="Load" onAction="#displayData">
We are connecting to the database using Spring. Can you please tell me how in this situation, when you click on the button, display all the data in the tableview?
Solution
@Edit2 (clearing things and simplifying)
Inject the UserRepository
@Autowired
private UserRepository userRepository;
Create a method fillTable()
like the one below:
private void fillTable() throws IOException {
userIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
userLoginColumn.setCellValueFactory(new PropertyValueFactory<>("login"));
userPassColumn.setCellValueFactory(new PropertyValueFactory<>("password"));
userSurnameColumn.setCellValueFactory(new PropertyValueFactory<>("surname"));
userNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
for (User usersDatum : userRepository.findAll()){
usersData.add(new User(
usersDatum.getId(),
usersDatum.getLogin(),
usersDatum.getPassword(),
usersDatum.getSurname(),
usersDatum.getName()));
}
tableUsers.setItems(usersData);
}
Use initialize()
this way:
@Override
public void initialize(URL location, ResourceBundle resources){
fillTable();
}
Link the method with your button on the fxml
file , i.e:
<Button mnemonicParsing="false" onAction="#fillTable" text="Fill Table"/>
Answered By - Roger Alves
Answer Checked By - Terry (JavaFixing Volunteer)