(首次发布问题) 我的TableView出现了一个奇怪的问题...
我正在使用JavaFX和SceneBuilder。
我有一个Package类型的TableView,并且我试图将搜索到的软件包及其属性显示到TableView中
除(接收日期)(rDate)一栏外,所有属性均正确显示。示例:
如果我替换
rDateCol.setCellValueFactory(new PropertyValueFactory <>(“ rDate”));
与
rDateCol.setCellValueFactory(new PropertyValueFactory <>(“ name”));
只是看看它是否有效。只需将名称放在两列中即可...
我也尝试过:
设置字符串rDate =“ test”;
或System.out.println(rDate);
确保变量不为空。
这是我的控制器代码:(试图在相关位置附近/// ***** ...进行评论)
package hermestracker;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import org.controlsfx.control.textfield.TextFields;
import static hermestracker.MainMenuController.*;
import static hermestracker.PackageReportMenuController.toTitleCase;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class CheckOutMenuController implements Initializable {
ArrayList<Package> packages = new ArrayList<>();
ArrayList<Package> checkedOut = new ArrayList<>();
ArrayList<Package> searchResults = new ArrayList<>();
ArrayList<Student> students = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
ObservableList<Package> listed = FXCollections.observableArrayList();
String inHouseFile = "bin/packages_in_house.bin";
String checkedOutFile = "bin/packages_checked_out.bin";
String[] shelf;
String letterFile = "bin/letter_shelf.bin";
String studentsFile = "bin/students.csv";
String directory = letterFile;
@FXML
private TextField nameField;
@FXML
private TextField notesField;
@FXML
private TextField proofField;
@FXML
private TextField whichPackField;
@FXML
private TextArea textArea1;
//************************************
@FXML
private TableView<Package> table;
@FXML
private TableColumn<Package, String> nameCol;
@FXML
private TableColumn<Package, String> locationCol;
@FXML
private TableColumn<Package, String> rDateCol;
@FXML
private TableColumn<Package, String> carrierCol;
@FXML
private TableColumn<Package, String> trackingCol;
@FXML
private TableColumn<Package, String> notesCol;
@FXML
private Label warnLabel;
@FXML // button that takes you back to the main menu
void backtoMain(ActionEvent event) throws IOException {
Parent main_menu_parent = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
Scene main_menu_scene = new Scene(main_menu_parent);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(main_menu_scene);
app_stage.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
readInHouse(inHouseFile);
readCheckedOut(checkedOutFile);
File file = new File(directory);
if (file.exists() == false) {
shelf = new String[]{"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty",
"empty", "empty", "empty", "empty", "empty"};
}
readLetters(letterFile);
readStudents(studentsFile);
for (Package pack : packages) {
String name = toTitleCase(pack.getOwner().getName());
if (!names.contains(name)) {
names.add(name);
}
}
TextFields.bindAutoCompletion(nameField, names);
//initialize 'list on enter'
nameField.setOnKeyPressed((KeyEvent keyEvent) -> {
if (keyEvent.getCode() == KeyCode.ENTER) {
ActionEvent e = new ActionEvent();
ListPackages(e);
}
});
//*******************
//initialize table columns
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
locationCol.setCellValueFactory(new PropertyValueFactory<>("location"));
rDateCol.setCellValueFactory(new PropertyValueFactory<>("rDate"));
carrierCol.setCellValueFactory(new PropertyValueFactory<>("carrier"));
trackingCol.setCellValueFactory(new PropertyValueFactory<>("tracking"));
notesCol.setCellValueFactory(new PropertyValueFactory<>("notes"));
}
@FXML
void ListPackages(ActionEvent event) {
if (nameField.getText().trim().isEmpty()) { //do not allow empty name
textArea1.clear();
textArea1.appendText("Please enter a name in the text field. ");
nameField.requestFocus();
return;
}
textArea1.clear();
searchResults.clear();
String search = nameField.getText();
if (search.contains("%")) {
search = idToName(search);
} else {
search = search.toLowerCase();
}
int count = 0;
for (int i = 0; i < packages.size(); i++) { //check each package
//if matching package is found
if (packages.get(i).getOwner().getName().equals(search)
|| packages.get(i).getLocation().toLowerCase().equals(search)
|| packages.get(i).getTracking().toLowerCase().equals(search)) {
searchResults.add(packages.get(i));
//************************************
listed.add(packages.get(i));
//list matching package for user to see
textArea1.appendText(searchResults.indexOf(packages.get(i)) + 1
+ ") " + packages.get(i).getOwner().getName()
+ "\t" + packages.get(i).getrDate()
+ "\t" + packages.get(i).getLocation()
+ "\t" + packages.get(i).getCarrier()
+ "\t" + packages.get(i).getNotes()
+ "\t" + packages.get(i).getTracking()
+ "\n");
}
}
//************************************
table.setItems(listed);
if (searchResults.isEmpty()) { //tell user that no package has been found
textArea1.appendText(("No packages found for " + search + "...\n"));
}
//check for letters
for (int i = 0; i < shelf.length; i++) {
if (shelf[i].equals(search)) {
textArea1.appendText(toTitleCase(search) + " has mail in shelf "
+ (i + 1) + ". (Check-Out in Letter Menu)");
}
}
nameField.requestFocus();
nameField.selectAll();
}
@FXML
void CheckOutAll(ActionEvent event) {
warnLabel.setText("");
String name = nameField.getText().trim();
String whichPack = whichPackField.getText().trim();
String proof;
if (name.isEmpty()) {
ListPackages(event);
return;
} else if (searchResults.isEmpty()) {
textArea1.appendText("Please find packages before check-out.\n");
nameField.requestFocus();
return;
} else if (name.contains("%")) {
proof = name;
name = idToName(name);
} else {
proof = proofField.getText().trim();
name = name.toLowerCase();
}
if (whichPack.contains("[^0-9,]")) {
warnLabel.setText("Invalid Choice Entry!");
return;
}
if (whichPack.isEmpty()) {
//check out all packages from searchResult
for (int j = 0; j < searchResults.size(); j++) {
if (proof.contains("%") && proof.contains("^")) {
packages.get(packages.indexOf(searchResults.get(j))).setdProof(idToName(proof));
} // nomag proof override
else if (proof.toLowerCase().equals("nomag")) {
packages.get(packages.indexOf(searchResults.get(j))).setdProof(name);
} // noid proof override
else if (proof.toLowerCase().equals("noid")) {
packages.get(packages.indexOf(searchResults.get(j))).setdProof(name);
} else if (proof.isEmpty()) {
warnLabel.setText("Proof Required!");
proofField.requestFocus();
return;
} else {
warnLabel.setText("Invalid Proof!");
proofField.requestFocus();
return;
}
//format dDate
Date date = Calendar.getInstance().getTime();
String today = date.toString();
String dDate = today.substring(4, today.length());
dDate = dDate.replace("EST ", "");
String temp = dDate.substring(16, 20);
dDate = dDate.substring(0, 12) + " " + temp;
//set dDate
packages.get(packages.indexOf(searchResults.get(j))).setdDate(dDate);
textArea1.clear();
//add this package to the 'checked out' list
checkedOut.add(packages.get(packages.indexOf(searchResults.get(j))));
//remove that package from packages list
packages.remove(packages.get(packages.indexOf(searchResults.get(j))));
textArea1.appendText(name + "'s packages have been checked out!\n");
}
} else {
String[] arrSplit2 = whichPack.split(",", 20);
int[] arrSplit3 = new int[arrSplit2.length];
for (int j = 0; j<arrSplit2.length; j++){
arrSplit3[j] = Integer.parseInt(String.valueOf(arrSplit2[j]))-1;
}
//check out specified packages from searchResult
for (int i = 0; i < arrSplit3.length; i++) {
if (proof.contains("%") && proof.contains("^")) {
packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))).setdProof(idToName(proof));
students.get(students.indexOf(packages.get(packages.indexOf(searchResults.get(i))).getOwner())).setScan(proof);
} // nomag proof override
else if (proof.toLowerCase().equals("nomag")) {
packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))).setdProof(name);
} // noid proof override
else if (proof.toLowerCase().equals("noid")) {
packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))).setdProof(name);
} else if (proof.isEmpty()) {
warnLabel.setText("Proof Required!");
proofField.requestFocus();
return;
} else {
warnLabel.setText("Invalid Proof!");
proofField.requestFocus();
return;
}
//format dDate
Date date = Calendar.getInstance().getTime();
String today = date.toString();
String dDate = today.substring(4, today.length());
dDate = dDate.replace("EST ", "");
String temp = dDate.substring(16, 20);
dDate = dDate.substring(0, 12) + " " + temp;
//set dDate
packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))).setdDate(dDate);
textArea1.clear();
//add this package to the 'checked out' list
checkedOut.add(packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))));
//remove that package from packages list
packages.remove(packages.get(packages.indexOf(searchResults.get(arrSplit3[i]))));
}
if (whichPack.replace(",", "").length() >= searchResults.size()) textArea1.appendText(name
+ "'s packages have been checked out!\n");
else {
searchResults.clear();
whichPackField.clear();
ListPackages(event);
}
}
proofField.clear();
nameField.requestFocus();
//rewrite 'checked-out' save file
saveCheckedOut(checkedOutFile);
//rewrite 'in house' save file
saveInHouse(inHouseFile);
nameField.requestFocus();
}
@FXML
private void changeNotes(ActionEvent event) {
warnLabel.setText("");
String search = nameField.getText().trim().toLowerCase();
String notes = notesField.getText().trim().toLowerCase();
String whichPack = whichPackField.getText().trim();
if (search.isEmpty()) {
ListPackages(event);
return;
} else if (searchResults.isEmpty()) {
textArea1.appendText("Please find packages before changing notes.\n");
return;
} else if (whichPack.contains("[^0-9]")) {
warnLabel.setText("Invalid Choice Entry!");
return;
}
if (whichPack.isEmpty()) {
for (int i = 0; i < searchResults.size(); i++) {
//change notes for each package
packages.get(packages.indexOf(searchResults.get(i))).setNotes(notes);
}
} else {
for (int i = 0; i < whichPack.replace(",", "").length(); i++) {
String[] arrSplit2 = whichPack.split(",", 20);
int num = Integer.parseInt(String.valueOf(arrSplit2[i])) - 1;
packages.get(packages.indexOf(searchResults.get(num))).setNotes(notes);
}
}
//reprint textArea
textArea1.clear();
for (int i = 0; i < searchResults.size(); i++) {
//list all packages for user to see
textArea1.appendText(packages.get(packages.indexOf(searchResults.get(i))).getOwner().getName()
+ "\t\t" + packages.get(packages.indexOf(searchResults.get(i))).getrDate()
+ "\t" + packages.get(packages.indexOf(searchResults.get(i))).getLocation()
+ "\t" + packages.get(packages.indexOf(searchResults.get(i))).getCarrier()
+ "\t" + packages.get(packages.indexOf(searchResults.get(i))).getNotes()
+ "\t" + packages.get(packages.indexOf(searchResults.get(i))).getTracking()
+ "\n");
}
//rewrite 'in house' save file
saveInHouse(inHouseFile);
}
public void readInHouse(String file) {
try { //read arraylist from .bin
ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
while (is.available() == 0) { // while there is more data to be read
//read each object and put in into the arraylist
packages.add((Package) is.readObject());
}
} catch (FileNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
}
}
public void readCheckedOut(String file) {
try { //read checked-out arraylist from .bin
ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
while (is.available() == 0) { // while there is more data to be read
//read each object and put in into the arraylist
checkedOut.add((Package) is.readObject());
}
} catch (FileNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
}
}
public void readLetters(String file) {
try { //read letter array from .bin
ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
//while (is.available() == 0) { // while there is more data to be read
//read the array from .bin
shelf = (String[]) is.readObject();
//}
} catch (FileNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
}
}
public void saveInHouse(String file) {
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
for (int i = 0; i < packages.size(); i++) {
os.writeObject(packages.get(i));
}
os.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void saveCheckedOut(String file) {
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
for (int i = 0; i < checkedOut.size(); i++) {
os.writeObject(checkedOut.get(i));
}
os.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void readStudents(String file) {
try(Scanner input = new Scanner(Paths.get(file))) {
input.useDelimiter(",");
// read record from file
input.nextLine();//skip header
while (input.hasNext()){
//read into students arraylist
students.add(new Student(input.next(), input.next(),
input.next()));
input.next();
}
}
catch (IOException | NoSuchElementException | IllegalStateException e) {
e.printStackTrace();
}
}
}
这是我的Package类代码:
package hermestracker;
import java.io.Serializable;
public class Package implements Serializable{
private Student owner;
private String name;
private String location;
private String rDate;
private String dDate;
private String dProof;
private String carrier;
private String notes;
private String tracking;
public Package(Student owner, String location, String carrier, String rDate, String tracking,
String notes) {
this.owner = owner;
this.name = owner.getName();
this.location = location;
this.rDate = rDate;
this.carrier = carrier;
this.notes = notes;
this.tracking = tracking;
this.dDate = "none";
this.dProof = "none";
}
public Package(){
this.owner = new Student();
this.name = owner.getName();
this.location = "None";
this.rDate = "None";
this.carrier = "None";
this.notes = "None";
this.tracking = "None";
this.dDate = "none";
this.dProof = "none";
}
public void setOwner(Student owner){
this.owner = owner;
this.name = owner.getName();
}
public Student getOwner(){
return owner;
}
public String getName() {
return name;
}
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return location;
}
public void setrDate(String rDate){
this.rDate = rDate;
}
public String getrDate(){
return rDate;
}
public void setCarrier(String carrier){
this.carrier = carrier;
}
public String getCarrier(){
return carrier;
}
public void setNotes(String notes){
this.notes = notes;
}
public String getNotes(){
return notes;
}
public void setTracking(String tracking){
this.tracking = tracking;
}
public String getTracking(){
return tracking;
}
public void setdDate(String dDate){
this.dDate = dDate;
}
public String getdDate(){
return dDate;
}
public void setdProof(String dProof){
this.dProof = dProof;
}
public String getdProof(){
return dProof;
}
}