簡體   English   中英

如何在JavaFx中創建通用TableView?

[英]How do I create a generic TableView in JavaFx?

我正在為我的項目構建一個GUI,我需要顯示來自各種類型的數據結構的信息:表中的ArrayList,HashMap,TreeSet。

基本上我需要顯示我在視圖包中的模型包中獲得的查詢方法的信息。

我的實現現在包括一個控制器,用於我每次聲明TableView時使用指定的對象類型構建的每個表。

我想知道是否有一種方法來創建一個類,它的構造函數將構建我需要的表(具有此對象所需的列數)。 某種通用表構建器,可以讀取對象並找出需要表示的列數。 例如:提供具有4個字段/列的Reservation對象與具有6個字段/列的Member對象進行比較。 根據它將得到的數據結構。 因此,我將能夠創建此類的實例並實例化我需要的表並在屏幕上顯示它。

我還需要一種能夠從場景構建器控制它的方法,因為這是我以圖形方式構建GUI的主要工具。

我在這里添加了一個表格代碼,希望有人可以幫我完成這個繁瑣的任務:)

    package view;

import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import model.Reservation;



public class QRYClientReservationsTableController {

    private ObservableList<Reservation> ReservationsData = FXCollections.observableArrayList();


    @FXML
    private TableView<Reservation> ReservationforClient;
    @FXML
    private TableColumn<Reservation, String> ReservationIdColumn;
    @FXML
    private TableColumn<Reservation, String> LocationIdColumn;
    @FXML
    private TableColumn<Reservation, String> AddressColumn;
    @FXML
    private TableColumn<Reservation, String> DescriptionColumn;
    @FXML
    private TableColumn<Reservation, String> StartTimeAndDateColumn;
    @FXML
    private TableColumn<Reservation, String> EndTimeAndDateColumn;
    @FXML
    private TextField memId;


    /**
     * The constructor.
     * The constructor is called before the initialize() method.
     */
    public QRYClientReservationsTableController() {

    }

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        // Initialize the Location table with the tree columns.
        ReservationIdColumn.setCellValueFactory(cellData -> cellData.getValue().ReservationIdProperty());
        LocationIdColumn.setCellValueFactory(cellData -> cellData.getValue().LocationIdProperty());
        AddressColumn.setCellValueFactory(cellData -> cellData.getValue().LocationAddressProperty());
        DescriptionColumn.setCellValueFactory(cellData -> cellData.getValue().LocationDescriptionProperty());
        StartTimeAndDateColumn.setCellValueFactory(cellData -> cellData.getValue().StartDateProperty());
        EndTimeAndDateColumn.setCellValueFactory(cellData -> cellData.getValue().EndDateProperty());
    }
    @FXML
    Button CloseBtn = new Button();//close button inside AddLocation window
    @FXML
    public void handleCloseButtonAction(ActionEvent event) {//for all close Button's this method is called
        Stage stage = (Stage) CloseBtn.getScene().getWindow();
        stage.close();
    }
    /**
     * Is called by the main application to give a reference back to itself.
     *
     * @param Locaion
     */
    public void setQRYClientReservationsTableController(String memId) {
        this.memId.setEditable(true);
        this.memId.setText(memId);
        this.memId.setEditable(false);
        ArrayList<Reservation> reservationsAdd = new ArrayList<Reservation>();
        reservationsAdd.addAll(ViewLogic.controlLogic.Q_getAllReservationsForMember(memId));
        ReservationsData.addAll(reservationsAdd);
        // Add observable list data to the table
        ReservationforClient.setItems(ReservationsData);
        ReservationIdColumn.sortableProperty().setValue(false);
        LocationIdColumn.sortableProperty().setValue(false);
        AddressColumn.sortableProperty().setValue(false);
        DescriptionColumn.sortableProperty().setValue(false);
        StartTimeAndDateColumn.sortableProperty().setValue(false);
        EndTimeAndDateColumn.sortableProperty().setValue(false);
    }
}

下一個代碼在我的View包中的ViewLogic類中,這個代碼召喚FXML文件加載一個小窗口來選擇一個成員Id,由此我得到的信息是在上面的代碼上構建表。 ViewLogic中此方法的代碼如下:

@FXML
Button ShowReservationsForClientBtn= new Button();/*Code for pressing the get reservations for client button to open window after pressing the button in Queries*/
@FXML
public void pressShowReservationsForClientBtn(ActionEvent event) throws Exception {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(ChooseAClientForReservationsQueryWindowController.class.getResource("/view/ChooseAClientForReservationsQueryWindow.fxml"));
        AnchorPane chooseMemberIdForQuery = (AnchorPane) fxmlLoader.load();
        ChooseAClientForReservationsQueryWindowController controller = fxmlLoader.getController();
        controller.setAddTripToReservationClass();
        Stage stage = new Stage();
        stage.setTitle("Please Choose a Member Id");
        stage.setScene(new Scene(chooseMemberIdForQuery));
        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

然后它會在您選擇成員的位置調用此類,然后啟動上面的表代碼,迷你窗口代碼為:

package view;

    import java.util.ArrayList;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Button;
    import javafx.scene.control.ButtonType;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Alert.AlertType;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;



    public class ChooseAClientForReservationsQueryWindowController {
        private String memIdChosen;
        @FXML
        private ComboBox<String> MemberIds;
        public void initialize() {
        }
        @FXML
        Button CloseBtn = new Button();//close button inside AddLocation window
        @FXML
        public void handleCloseButtonAction(ActionEvent event) {//for all close Button's this method is called
            Stage stage = (Stage) CloseBtn.getScene().getWindow();
            stage.close();
        }
        @FXML
        Button EraseBtn = new Button();//erase Button
        @FXML
        public void handleEraseButtonAction(ActionEvent event) {//erase Button code
            MemberIds.setValue(null);
        }
        @FXML
        Button GoBtn = new Button();//Go button
        @FXML
        public void handleGoBtn(ActionEvent event) throws Exception {//for all close Button's this method is called
            try{
                if(MemberIds.getValue()==null)
                    throw new Exception();
                FXMLLoader fxmlLoader = new FXMLLoader(QRYClientReservationsTableController.class.getResource("/view/QRYClientReservationsTableController.fxml"));
                AnchorPane qryShowAllReservationsForMember = (AnchorPane) fxmlLoader.load();
                QRYClientReservationsTableController controller = fxmlLoader.getController();
                controller.setQRYClientReservationsTableController(memIdChosen);
                Stage stage = new Stage();
                stage.setTitle("Query - Show all reservations for member Id: "+memIdChosen+".");
                stage.setScene(new Scene(qryShowAllReservationsForMember));
                stage.show();
                handleCloseButtonAction(event);
            }
            catch (Exception e){
                Alert alert = new Alert(AlertType.WARNING,"One of the fields is empty", ButtonType.OK);
                alert.setTitle("One of the fields is empty");
                alert.setHeaderText("One of the fields is empty");
                alert.setContentText("Please fill the empty fields");
                alert.showAndWait();
            }
        }
        /**
         * The constructor.
         * The constructor is called before the initialize() method.
         */
        public ChooseAClientForReservationsQueryWindowController() {

        }

        public void setAddTripToReservationClass(){
            ArrayList<String> memIds = new ArrayList<String>();
            memIds.addAll(ViewLogic.controlLogic.getMembers().keySet());
            MemberIds.getItems().setAll(memIds);
            MemberIds.valueProperty().addListener(new ChangeListener<String>(){

                @Override
                public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
                    memIdChosen=arg2;

                }
            });
        }
    }

如果它有幫助,那就是上面表格代碼的相關FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1450.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.QRYClientReservationsTableController">
   <children>
      <ButtonBar layoutX="44.0" layoutY="541.0" prefHeight="40.0" prefWidth="700.0" AnchorPane.bottomAnchor="19.0">
        <buttons>
            <Button fx:id="CloseBtn" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefHeight="31.0" prefWidth="212.0" text="Close" />
        </buttons>
      </ButtonBar>
      <TableView fx:id="ReservationforClient" layoutX="5.0" layoutY="55.0" prefHeight="486.0" prefWidth="893.0" AnchorPane.bottomAnchor="59.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="55.0">
        <columns>
          <TableColumn fx:id="ReservationIdColumn" prefWidth="121.0" text="Reservation Id" />
            <TableColumn fx:id="LocationIdColumn" prefWidth="128.0" text="Location Id" />
            <TableColumn fx:id="AddressColumn" prefWidth="276.0" text="Address" />
            <TableColumn fx:id="DescriptionColumn" prefWidth="382.0" text="Description" />
            <TableColumn fx:id="StartTimeAndDateColumn" prefWidth="282.0" text="Start Time and Date" />
            <TableColumn fx:id="EndTimeAndDateColumn" prefWidth="252.0" text="EndTime and Date" />
        </columns>
      </TableView>
      <Label layoutX="394.0" layoutY="8.0" prefHeight="40.0" prefWidth="350.0" text="Reservations for Client Id:" AnchorPane.leftAnchor="394.0" AnchorPane.rightAnchor="706.0" AnchorPane.topAnchor="8.0">
         <font>
            <Font name="System Bold" size="28.0" />
         </font>
      </Label>
      <TextField fx:id="memId" editable="false" layoutX="738.0" layoutY="10.0" prefHeight="40.0" prefWidth="191.0" />
   </children>
</AnchorPane>

謝謝

湯姆

我偶然發現了類似的問題並創建了一個解決它的簡單庫。

它使用您的模型類來構建表示給定類的字段的TableView。

我已經在GitHub上發布了一個示例和說明。

如果您有任何建議,我想聽聽您的意見。

你可以通過反思來做到這一點。 這將為每個SimpleStringProperty字段創建一個可編輯的String列。 這樣做意味着您無法在FXML中添加它們。

        for (Field f : TableInfo.class.getDeclaredFields()) {
            if (f.getType().equals(SimpleStringProperty.class)){
                TableColumn<TableInfo, String> tc = new TableColumn<>(f.getName());
                tv.getColumns().add(tc);
                tc.setCellValueFactory(new PropertyValueFactory<>(f.getName()));
                tc.setCellFactory(TextFieldTableCell.forTableColumn());
             }//else if more field types...
        }

暫無
暫無

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

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