简体   繁体   English

在javafx表中设置项目,我做错了什么?

[英]Setting the items in a javafx table, what am I doing wrong?

I have to initialize a javafx table and I have no idea what is going wrong. 我必须初始化一个javafx表,但不知道出了什么问题。 I used this: https://docs.oracle.com/javafx/2/ui_controls/table-view.htm tutorial and I am also using SceneBuilder and FXML to make the GUI. 我用了这个: https ://docs.oracle.com/javafx/2/ui_controls/table-view.htm教程,我也使用SceneBuilder和FXML制作GUI。

Main class: This sets the window up and creates the table, along with all of the columns and the like. 主类:这将设置窗口并创建表以及所有列等。 It also makes sure to open or create the file that will be read from and written to. 它还确保打开或创建将要读取和写入的文件。

public class Main extends Application {

    private AnchorPane rootLayout;

    public void initialize ()
    {
        System.out.println("Initialized");
        try {

            File masterFile = new File(("G:\\"),"masterfile.txt"); //I just needed to create the file to be able to access it at a later date


             if (!masterFile.exists()){

                 masterFile.createNewFile();

             }

             MainScreenController table = new MainScreenController();

            table.initializeTable();

             System.setProperty("glass.accessible.force", "false");

        } catch (Exception e) {

            System.out.println("INITIALIZE METHOD WENT HAYWIRE");

        }
    }
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader root = new FXMLLoader();

            root.setLocation(Main.class.getResource("view/MainScreen.fxml"));
            rootLayout = (AnchorPane) root.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setResizable(false);
            primaryStage.setScene(scene);
            primaryStage.show();
            initialize();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }




    public static void main(String[] args) {
        launch(args);
    }


}

the controller class of the table: This is my attempt at making the columns be populated by the data I have. 表的控制器类:这是我尝试用我拥有的数据填充列。

@FXML
public Button showAddNewPerson;
@FXML
public Button EditStudentFiles;
@FXML
public Button generateDebtorReport;
@FXML
public Button generateSeniorReport;
@FXML
public TableView<StudentData> dataTable = new TableView<StudentData>();
@FXML
private TableColumn<StudentData, String> firstNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> lastNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> schoolCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> stateCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> emailCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Integer> yearJoinedCol = new TableColumn<StudentData, Integer>();
@FXML
private TableColumn<StudentData, String> activeCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Double> debtCol = new TableColumn<StudentData, Double>();

public AnchorPane addNewPersonLayout;

@SuppressWarnings("unchecked")
public void initializeTable() {


    ObservableList<StudentData> observableData = FXCollections.observableArrayList(StudentData.retrieveDataFromFile());//retrieves the array list from the file

    //I think these statements pick out each instance variable of each object in the arrayList
    firstNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("firstName"));
    lastNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("lastName"));
    schoolCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("schoolName"));
    stateCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("stateAbbreviaion"));
    emailCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("studentEmail"));
    yearJoinedCol.setCellValueFactory(new PropertyValueFactory<StudentData, Integer>("yearJoined"));
    activeCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("areTheyActive"));
    debtCol.setCellValueFactory(new PropertyValueFactory<StudentData, Double>("amountOwed"));

    //sets observable list into the table
    dataTable.setItems(observableData);
    //this is what the tutorial says works
    dataTable.getColumns().addAll(firstNameCol, lastNameCol, schoolCol, stateCol, emailCol, yearJoinedCol, activeCol, debtCol);
}

I don't really understand how this table is supposed to work as the tutorial confuses me a little. 我真的不了解该表应该如何工作,因为本教程使我有些困惑。

First of all, make sure that you have defined the controller class in your FXML. 首先,请确保已在FXML中定义了控制器类。

<YourRootElement fx:controller="MyControllerClass">

Then, i'm not sure why you are creating new instances of your "visual elements" and why are you defining them as public, but if you are using JavaFX 8, you neet to define them like this: 然后,我不确定为什么要创建“可视元素”的新实例以及为什么要将它们定义为公共实例,但是如果您使用的是JavaFX 8,则可以像这样定义它们:

@FXML
private TableView<StudentData> dataTable;

about your Table, you have 3 options: 关于表,您有3种选择:

  1. (The best one) Initialize the Table inside the controller. (最好的一种)初始化控制器内部的表。 You should create your initialize methon on the controller to fill all your data before calling the "show" method on the main. 您应该在控制器上创建初始化方法来填充所有数据,然后再在主机上调用“ show”方法。 That method will be called by default by the JRE... And please be sure to have Getters for your Student Class. JRE默认会调用该方法。请确保您的学生班级有Getters。

     public ControllerClass{ //All your variables and Stuff @FXML private void initialize(){ //here is where you initialize your data } } 
  2. There's a little trick to force the table to update, but since I'm not 100% sure that you are collecting correctly your data, I can't guarantee it to work. 有一个小技巧可以强制更新表,但是由于我不能100%确定您正确地收集了数据,因此我不能保证它能正常工作。 Place this code after you add items to your ObservableList 将项目添加到ObservableList后放置此代码

     dataTable.getColumns().get(0).setVisible(false); dataTable.getColumns().get(0).setVisible(true); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM