我正在为我的项目构建一个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,通过这个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>
谢谢
汤姆
答案 0 :(得分:3)
我偶然发现了类似的问题并创建了一个解决它的简单库。
它使用您的模型类来构建表示给定类的字段的TableView。
我published it on GitHub提供样本和说明。
如果您有任何建议,我想听听您的意见。
答案 1 :(得分:0)
你可以通过反思来做到这一点。这将为每个SimpleStringProperty
字段创建一个可编辑的字符串列。这样做意味着您无法在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...
}