0
我有一个表格下使用FXML JavaFX中的表格。您能否告诉我如何在下一次点击表格时获取并加载数据?我怎样才能得到数据时,单击TableView java FXML
这里是我的控制器
package com.songhuy.gui.controller;
import com.songhuy.dal.UserDAL;
import com.songhuy.dll.RoleDLL;
import com.songhuy.dll.UserDLL;
import com.songhuy.entity.User;
import com.songhuy.list.RoleList;
import com.songhuy.list.UserList;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.StringConverter;
/**
* FXML Controller class
*
* @author DUONGPHAM
*/
public class UserController implements Initializable {
/**
* Initializes the controller class.
*/
//Khai báo các thành phần FXML
@FXML
TableView<UserList> tableUser;
@FXML
TextField txtUsername;
@FXML
TextField txtFullname;
@FXML
TableColumn<UserList, String> colUsername;
@FXML
TableColumn<UserList, String> colFullname;
@FXML
TableColumn<UserList, String> colBirthday;
@FXML
TableColumn<UserList, String> colAddress;
@FXML
TableColumn<UserList, String> colPhonenumber;
@FXML
TableColumn<UserList, String> colEmail;
@FXML
TableColumn<UserList, String> colRole;
@FXML
ComboBox cbxRole;
User usr = new User();
UserDLL dll = new UserDLL();
UserDAL dal = new UserDAL();
//Các hàm chạy khi User được load
@Override
public void initialize(URL url, ResourceBundle rb) {
tableUser();
cbxRole();
}
//Load form User được gọi từ Main
public void User() {
try {
Parent root;
root = FXMLLoader.load(getClass().getResource("/com/songhuy/gui/fxml/User.fxml"));
Stage primaryStage = new Stage();
Scene scene = new Scene(root, 800, 600);
//scene.getStylesheets().add("/fxml/styles/main.css");
primaryStage.setScene(scene);
primaryStage.setTitle("USER");
primaryStage.show();
} catch (IOException ex) {
System.out.println("Error \n" + ex);
}
}
//xóa field
public void clearTextbox() {
txtUsername.clear();
txtFullname.clear();
}
//Load combobox Role
public void cbxRole() {
//load combobox
RoleDLL role = new RoleDLL();
cbxRole.setConverter(new StringConverter<RoleList>() {
@Override
public String toString(RoleList object) {
return object.getRole();
}
@Override
public RoleList fromString(String string) {
return null;
}
});
ObservableList<RoleList> datarole = role.getAll();
cbxRole.setItems(datarole);
}
//Load table
public void tableUser() {
//load table
UserDLL dll = new UserDLL();
tableUser.setItems(dll.getAllUser());
colUsername.setCellValueFactory(new PropertyValueFactory<>("username"));
colFullname.setCellValueFactory(new PropertyValueFactory<>("fullname"));
colBirthday.setCellValueFactory(new PropertyValueFactory<>("birthday"));
colAddress.setCellValueFactory(new PropertyValueFactory<>("address"));
colPhonenumber.setCellValueFactory(new PropertyValueFactory<>("phonenumber"));
colEmail.setCellValueFactory(new PropertyValueFactory<>("email"));
colRole.setCellValueFactory(new PropertyValueFactory<>("role"));
}
//Set giá trị field theo click
public void setselectView() {
UserList userlist = tableUser.getSelectionModel().getSelectedItem();
if (userlist != null) {
usr.username = userlist.getUsername();
dal.getUserId(usr);
txtUsername.setText(usr.username);
txtFullname.setText(usr.fullname);
}
}
//Click vào table hiện thông tin lên textbox
public void tableUserOnClick(ActionEvent evt) {
setselectView();
}
//Nút ADD
public void Add() {
}
}
有一个'User'类和一个'public void User()'方法是一个不健康的组合。我建议遵循命名约定并用小写字母开始方法名称。 – fabian
另外,你能否澄清这个问题。你想要发生什么,以及什么用户事件触发它?我不明白“当我点击下一张表格时如何获取和加载数据”。 –
我的意思是,当我点击用户窗体上的表格时,表格上的数据自动加载到窗体上的textField上以编辑,保存或删除 –