如何显示数据从选定的Tableview到另一个阶段JFXTextField

时间:2019-04-18 11:23:17

标签: java mysql javafx

我一直在StackOverFlow中搜索此问题,但没有一个可以解决我的问题。我的问题是如何将具有选定表视图的数据库中的数据显示到涉及JFXTextfield,JFXCombobox,JFXRadioButton和DatePicker的另一个阶段。

这是我的代码

DashboardController.java


package Controller;

import DAO.KampusDAO;
import Database.DBConfig;
import Model.KampusModel;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
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.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author raviMukti at Aftwork
 */
public class DashboardController implements Initializable {

    @FXML
    private TableView<KampusModel> dashboardTable;
    @FXML
    private TableColumn<KampusModel, String> nama_col;
    @FXML
    private TableColumn<KampusModel, String> npm_col;
    @FXML
    private TableColumn<KampusModel, String> pob_col;
    @FXML
    private TableColumn<KampusModel, String> dob_col;
    @FXML
    private TableColumn<KampusModel, String> gender_col;
    @FXML
    private TableColumn<KampusModel, String> alamat_col;
    @FXML
    private TableColumn<KampusModel, String> prodi_col;
    @FXML
    private TableColumn<KampusModel, String> jenjang_col;
    @FXML
    private Button addBtn;
    @FXML
    private Button editBtn;
    @FXML
    private Button delBtn;
    @FXML
    private Button btnExit;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initCol();
        loadData();
        dashboardTable.refresh();
    }    

    @FXML
    private void addBtnAction(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/AddMahasiswa.FXML"));
        Stage stage = new Stage();
        stage.initOwner(addBtn.getScene().getWindow());
        stage.setScene(new Scene((Parent) loader.load()));

        stage.showAndWait();
    }

    @FXML
    private void editBtnAction(ActionEvent event) throws IOException {
      KampusModel item = dashboardTable.getSelectionModel().getSelectedItem();
      String selected = item.getNpm_mhs();
      FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/EditMahasiswa.FXML"));
      Stage stage = new Stage();
      stage.initOwner(editBtn.getScene().getWindow());
      stage.setScene(new Scene((Parent) loader.load()));
      stage.showAndWait();
    }

    @FXML
    private void delBtnAction(ActionEvent event) throws ClassNotFoundException, SQLException {
       KampusModel item = dashboardTable.getSelectionModel().getSelectedItem();
       String selected = item.getNpm_mhs();
        // Membuat dialog box konfirmasi
        Alert alertBatal = new Alert(Alert.AlertType.CONFIRMATION);
        alertBatal.setTitle("Kampus App - Konfirmasi Hapus Data");
        alertBatal.setHeaderText("Hapus Data??");
        alertBatal.setContentText("Apakah anda yakin akan hapus data ini???");
        Optional<ButtonType> konfirmasiHapus = alertBatal.showAndWait();
        if(konfirmasiHapus.get() == ButtonType.OK){
            try {
                KampusDAO.deleteMhs(selected);
            } catch (SQLException e) {
                System.out.println("Ada kesalahan "+ e);
            }
        }
        DashboardController.getInstance().loadData();
    }

    @FXML
    private void btnExitAction(ActionEvent event) {
        // Membuat dialog box konfirmasi
        Alert alertBatal = new Alert(Alert.AlertType.CONFIRMATION);
        alertBatal.setTitle("Kampus App - Konfirmasi Keluar");
        alertBatal.setHeaderText("Batal Simpan Bio");
        alertBatal.setContentText("Apakah anda yakin akan Keluar?");
        Optional<ButtonType> konfirmasiBatal = alertBatal.showAndWait();
        if(konfirmasiBatal.get() == ButtonType.OK){
            System.exit(1);
        }
    }

   //Inisialisasi Retrieve data ke Tableview   
  void initCol(){
       nama_col.setCellValueFactory(new PropertyValueFactory<>("nama_mhs"));
       npm_col.setCellValueFactory(new PropertyValueFactory<>("npm_mhs"));
       pob_col.setCellValueFactory(new PropertyValueFactory<>("tempat_mhs"));
       dob_col.setCellValueFactory(new PropertyValueFactory<>("tanggal_mhs"));
       gender_col.setCellValueFactory(new PropertyValueFactory<>("jk_mhs"));
       alamat_col.setCellValueFactory(new PropertyValueFactory<>("alamat_mhs"));
       prodi_col.setCellValueFactory(new PropertyValueFactory<>("prodi_mhs"));
       jenjang_col.setCellValueFactory(new PropertyValueFactory<>("jenjang_mhs"));
   }

   //Untuk retrieve data dari database dan menampilkannya ke tableview
   void loadData(){
       ObservableList<KampusModel> oblist = FXCollections.observableArrayList();

       oblist.removeAll(oblist);

        try {
            Connection conn = DBConfig.getConnection();
            ResultSet result = conn.createStatement().executeQuery("SELECT * FROM bio_mhs");

            while (result.next()) {                
                oblist.add(new KampusModel(result.getString("nama_mhs"), result.getString("npm_mhs"), 
                        result.getString("tempat_lahir"), result.getString("tgl_lahir"), 
                        result.getString("jenis_kelamin"), result.getString("alamat_mhs"),
                        result.getString("prodi_mhs"), result.getString("jenjang_mhs")));
            }
        } catch (SQLException e) {
            System.out.println("Ada Kesalahan "+ e);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(KampusController.class.getName()).log(Level.SEVERE, null, ex);
        }

        dashboardTable.setItems(oblist);
   }


//   Membuat instance agar variable dan method pada DashboardController bisa di akses di Controller Lain
   private static DashboardController instance;
   public DashboardController(){
       instance = this;
   }
//   Method getInstance()
   public static DashboardController getInstance(){
       return instance;
   }
}

EditMahasiswaController.java


package Controller;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;

/**
 * FXML Controller class
 *
 * @author raviMukti at Aftwork
 */
public class EditMahasiswaController implements Initializable {

    @FXML
    private JFXTextField fieldEditNama;
    @FXML
    private JFXTextField fieldEditNpm;
    @FXML
    private JFXTextField pobEdit;
    @FXML
    private DatePicker dobEdit;
    @FXML
    private JFXTextArea alamatEdit;
    @FXML
    private JFXComboBox<?> comboProdiEdit;
    @FXML
    private JFXComboBox<?> comboJenjangEdit;
    @FXML
    private JFXRadioButton radioPriaEdit;
    @FXML
    private JFXRadioButton radioWanitaEdit;
    @FXML
    private Button updateBtn;
    @FXML
    private Button batalBtn;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
       //Whats code that i must put in here to display data that currently selected in previous windows (DashboardController.java)
    }    

    @FXML
    private void updateBtnAction(ActionEvent event) {
    }

    @FXML
    private void batalBtnAction(ActionEvent event) {
    }

}

我希望人们可以例如举一些链接,或为我提供建议,谢谢。

1 个答案:

答案 0 :(得分:0)

您的实际问题似乎是:“如何在这两个控制器之间进行通信?”

您将从EditController中的新字段开始,可以通过getter和setter进行访问。

FXMLLoader类具有一个返回其控制器的方法(在FXML加载之后)。这样,您就可以到达EditController:

  FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/EditMahasiswa.FXML"));
  // Loading your fxml here
  ....
  // get your EditController, cast it to the right class and use the setter to pass your variable 
  EditMahasiswaController editController = (EditMahasiswaController ) loader.getController();
  editController.setKampusModel(yourModel);