簡體   English   中英

單擊TableView Java FXML時如何獲取數據

[英]How can I get data when click on TableView java FXML

我在使用FXML的JavaFX中的表格旁邊有一個表格。 當我單擊下一步時,能否請您告訴我如何獲取和加載數據以形成表格?

這是我的控制器

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() {

    }

}

我想我知道有什么問題。 通常,您可以在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);
     }
}

如果在表格視圖上單擊鼠標,它將執行您在方法中定義的操作。 我希望這有幫助。 干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM