如何在单击TableView java FXML时获取数据

时间:2016-09-01 17:03:00

标签: java javafx tableview fxml

我在JavaFX中使用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() {

    }

}

1 个答案:

答案 0 :(得分:1)

我想我知道这是什么问题。通常,您可以在FXML中添加事件。如果您想点击它,则必须使用标记onMouseClicked=""添加标签 以下是一个示例:<TableView fx:id="pl2" onMouseClicked="#handleClickTableView" GridPane.rowIndex="1"> 要获得TableView的值,您可以添加类似的内容:

@FXML
private void handleClickTableView(MouseEvent click) {
        UserList userlist = tableUser.getSelectionModel().getSelectedItem();
        if (userlist != null) {
            usr.username = userlist.getUsername();
            dal.getUserId(usr);
            txtUsername.setText(usr.username);
            txtFullname.setText(usr.fullname);
     }
}

如果在表视图上单击鼠标,它将执行您在方法中定义的操作。我希望这有帮助。干杯