2016-09-01 39 views
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() { 

    } 

} 
+1

有一個'User'類和一個'public void User()'方法是一個不健康的組合。我建議遵循命名約定並用小寫字母開始方法名稱。 – fabian

+0

另外,你能否澄清這個問題。你想要發生什麼,以及什麼用戶事件觸發它?我不明白「當我點擊下一張表格時如何獲取和加載數據」。 –

+0

我的意思是,當我點擊用戶窗體上的表格時,表格上的數據自動加載到窗體上的textField上以編輯,保存或刪除 –

回答

0

我想我知道什麼是問題。通常情況下,您可以在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); 
    } 
} 

如果鼠標點擊表格視圖,它將完成你在方法中定義的內容。我希望這有幫助。乾杯