带有 fxml TableView 的 JavaFX 更新一些列而不是其他列

JavaFX with fxml TableView updating some columns but not others

在我的应用程序中,我的 table 中的某些列正在填充,就像它们应该是标记为 Name Priority 和 BurstTime 的列添加了正确的值,但由于某种原因,其他两个 ProcessID 和 State 没有使用我添加到 ArrayList 的值进行更新。

看起来我的代码应该可以工作,有人能看到我遗漏的东西吗?

这是我的控制器

package application;

import java.net.URL;
import java.util.ResourceBundle;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;



import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class Controller implements Initializable {

private ArrayList<String> buf = new ArrayList<>();

protected ArrayList<PCB> array = new ArrayList<>();
protected ArrayList<Process> arrayP = new ArrayList<>();

ObservableList<Process> processData = FXCollections.observableArrayList();


@FXML
private Button SubmitButton;

@FXML
private Button LoadButton;


@FXML
private TextArea textArea;

@FXML
private TextField inputBox;

@FXML
private TableView<Process> ProcessTable;

@FXML
private TableColumn<Process, String> processIDP;

@FXML
private TableColumn<Process, String> processTypeP;

@FXML
private TableColumn<Process, String> priorityCodeP;

@FXML
private TableColumn<Process, String> burstTimeP;

@FXML
private TableColumn<Process, String> StatusCodeP;



@Override
public void initialize(URL url, ResourceBundle rb) {
    processIDP.setCellValueFactory(new PropertyValueFactory<Process, String>("processIDP"));
    processTypeP.setCellValueFactory(new PropertyValueFactory<Process, String>("processTypeP"));
    priorityCodeP.setCellValueFactory(new PropertyValueFactory<Process, String>("priorityCodeP"));
    burstTimeP.setCellValueFactory(new PropertyValueFactory<Process, String>("burstTimeP"));
    StatusCodeP.setCellValueFactory(new PropertyValueFactory<Process, String>("StatusCodeP"));

    Process p1 = new Process();
    p1.setprocessIDP("22");
    p1.setProcessTypeP ("Apname");
    p1.setPriorityCodeP("1");
    p1.setBurstTimeP ("13");
    p1.setstatusCodeP("Tada");

    arrayP.add(p1);
    ProcessTable.getItems().addAll(arrayP.get(0));

    ProcessTable.setItems(FXCollections.observableArrayList(arrayP));

    Process p2 = new Process();
    p2.setprocessIDP("24");
    p2.setProcessTypeP ("Bpname");
    p2.setBurstTimeP ("15");
    p2.setPriorityCodeP("2");

    arrayP.add(p2);
    ProcessTable.getItems().addAll(arrayP.get(1));

    // edit existing cell ?
    arrayP.get(1).setPriorityCodeP("8");
    arrayP.get(1).setstatusCodeP("This");
    arrayP.get(1).setprocessIDP("TEST");

}

public ObservableList<Process> getProcessData() {
    return processData;
}


@FXML
private TextField LoadProgram;

@FXML
private void handleButtonAction() {
    textArea.appendText(inputBox.getText() + "\n");
    StringTokenizer st1 = new StringTokenizer(inputBox.getText(), " ");

    switch(st1.nextToken()) {
    // case "proc": proc(); break;
     case "mem": textArea.appendText("Memory: " + String.valueOf(Memory.getUsedMemory()) + "/" + String.valueOf(Memory.getTotalMemory()) + "\n"); break;
    // case "exe": exe(); break;
    // case "reset": reset(); break;
     case "load": buf.add(inputBox.getText()) ;
       // edit existing cell ?
       arrayP.get(1).setPriorityCodeP("9");

       ProcessTable.refresh();

       break;
     case "exit": System.exit(0); break;
     case "clear": textArea.clear(); break;
     default: break;
 }
}




@FXML
private void handleLoadAction() {
    File infile = new File("files/" + LoadProgram.getText() + ".txt");
    if (infile.exists() == true ) {
        textArea.appendText("Loading " + LoadProgram.getText() + "\n");
        }

    //call to read data here

    else {
    textArea.appendText("No Program named " + LoadProgram.getText() + " found \n");
}}



public class textLine {


    private String infile;
    private String cmd, value;
    private Scanner input;


    public void parseFile(String filename) {
        this.infile = "files/" + filename + ".txt";
        parseFile();
    }

    public void addbuf(String textline) {
        buf.add(textline);
    }

    private void parseFile() {

        buf.clear();
        try {
            File file = new File(infile);
            if (file.exists() == true)
            input = new Scanner(file);
            while (input.hasNext()) {
                buf.add(input.next());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        input.close();
    }

}

}

这是我的 class Process

package application;


public class Process {

String processTypeP = "";
String priorityCodeP = "0";
int lineCodeP = 0;
String burstTimeP = "0";
String processIDP = "0";
String StatusCodeP = "0";

public Process (){} 

public String getProcessTypeP() {
    return processTypeP;
}
public void setProcessTypeP(String processTypeP) {
    this.processTypeP = processTypeP;
}

public String getPriorityCodeP() {
    return priorityCodeP;
}
public void setPriorityCodeP(String priorityCodeP) {
    this.priorityCodeP = priorityCodeP;
}

public int getLineCodeP() {
    return lineCodeP;
}
public void setLineCodeP(int lineCodeP) {
    this.lineCodeP = lineCodeP;
}

public String getBurstTimeP() {
    return burstTimeP;
}
public void setBurstTimeP(String burstTimeP) {
    this.burstTimeP = burstTimeP;
}

     public String getprocessIDP() {
    return processIDP;
}
public void setprocessIDP(String processIDP) {
    this.processIDP = processIDP;
}

    public String getstatusCodeP() {
    return StatusCodeP;
}
public void setstatusCodeP(String StatusCodeP) {
    this.StatusCodeP = StatusCodeP;
}



}

这是我的主要应用程序

package application;



import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;

import javafx.scene.Scene;


import javafx.scene.layout.AnchorPane;



public class Main extends Application {

@Override
public void start(Stage primaryStage) {
    try {
        FXMLLoader loader = new FXMLLoader(Main.class.getResource("/Main.fxml"));
    //  Parent root = FXMLLoader.load(getClass().getResource("/Main.fxml"));
        AnchorPane root = (AnchorPane) loader.load(Main.class.getResource("/application/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }


}

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

和我的 fxml 文件

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

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

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<TabPane layoutX="4.0" layoutY="4.0" prefHeight="700.0" prefWidth="900.0" tabClosingPolicy="UNAVAILABLE">
    <tabs>
      <Tab text="Processes">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                 <children>
                    <TextField fx:id="inputBox" layoutX="14.0" layoutY="54.0" />
                    <Button fx:id="SubmitButton" layoutX="109.0" layoutY="94.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Submit" />
                    <TextArea fx:id="textArea" layoutX="197.0" layoutY="14.0" prefHeight="105.0" prefWidth="493.0" />
                    <TableView fx:id="ProcessTable" layoutX="36.0" layoutY="157.0" prefHeight="430.0" prefWidth="654.0">
                      <columns>
                        <TableColumn fx:id="processIDP" prefWidth="75.0" text="ProccessID" />
                        <TableColumn fx:id="processTypeP" prefWidth="101.0" text="Name" />
                          <TableColumn fx:id="priorityCodeP" prefWidth="94.0" text="Priority" />
                          <TableColumn fx:id="StatusCodeP" prefWidth="119.0" text="State" />
                          <TableColumn fx:id="burstTimeP" prefWidth="100.0" text="BurstTime" />
                      </columns>
                    </TableView>
                    <TextField fx:id="LoadProgram" layoutX="712.0" layoutY="492.0" />
                    <Button fx:id="LoadProgramButton" layoutX="725.0" layoutY="531.0" mnemonicParsing="false" onAction="#handleLoadAction" text="Load External Program" />
                 </children></AnchorPane>
        </content>
      </Tab>
      <Tab text="Scheduler">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
        </content>
      </Tab>
        <Tab text="Memory">
           <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
           </content>
        </Tab>
    </tabs>
  </TabPane>
</children>
</AnchorPane>

尝试将 getter 方法从 getprocessIDPgetstatusCodeP 分别重命名为 getProcessIDPgetStatusCodeP(注意大写 'S' 和 'P').为避免将来出现此类问题,使用 IDE 生成 getter、setter 和构造函数而不是手动生成是一个好习惯。