我有一类动物。具有不同的属性。 我想在不同的单元格中显示所有属性。但是带有array的属性如下所示。我如何实现在单元格中打印数组。
下面是我的代码。 从文件中读取数据,并在initialize方法中将其添加到动物的Arraylist中。 所有功能均通过搜索方法完成。 表列在运行时动态创建。 并将动物的Arraylist传递到可观察列表。 最后,可观察对象设置为表。但是我不知道如何将单元格的值设置为数组,以便它可以在单元格中打印该数组。
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class FindByNoun implements Initializable {
private ArrayList<Animal> animals = new ArrayList<>();
@FXML
private TextField searchText;
@FXML
private Button search;
@FXML
private Button back;
@FXML
private TableView<Animal> table;
ObservableList<Animal> obList = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("animals.csv"));
reader.readLine();
String line = reader.readLine();
while (line != null) {
String[] tokens = line.split(",");
String type = tokens[0];
if (!type.equals("Bird")){
String noun = tokens[1];
String snoun = tokens[2];
double minSize = Double.parseDouble(tokens[3]);
double maxSize = Double.parseDouble(tokens[4]);
double minWeight = Double.parseDouble(tokens[5]);
double maxWeight = Double.parseDouble(tokens[6]);
String[] color = tokens[7].split(";");
String[] locomotion = tokens[8].split(";");
String[] countriesFoundIn = tokens[9].split(";");
String[] naturalHabitat = tokens[10].split(";");
animals.add(new Animal(type, noun, snoun, minSize, maxSize, minWeight, maxWeight, color, locomotion, countriesFoundIn, naturalHabitat));
}else{
String noun = tokens[1];
String snoun = tokens[2];
double minSize = Double.parseDouble(tokens[3]);
double maxSize = Double.parseDouble(tokens[4]);
double minWeight = Double.parseDouble(tokens[5]);
double maxWeight = Double.parseDouble(tokens[6]);
String[] color = tokens[7].split(";");
String[] locomotion = tokens[8].split(";");
String[] countriesFoundIn = tokens[9].split(";");
String[] naturalHabitat = tokens[10].split(";");
String shape = tokens[11];
String[] colorsOfFeathers = tokens[12].split(";");
double minLengthOfWings = Double.parseDouble(tokens[13]);
double maxLengthOfWings = Double.parseDouble(tokens[14]);
animals.add(new Bird(type, noun, snoun, minSize, maxSize, minWeight, maxWeight, color, locomotion, countriesFoundIn, naturalHabitat, shape, colorsOfFeathers, minLengthOfWings, maxLengthOfWings));
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}// initialize
public void search(){
table.getColumns().clear();
String searchValue = searchText.getText();
boolean bird = false;
boolean found = false;
Animal animalFound = null;
for (Animal animal: animals){
String noun = animal.getNoun();
String snoun = animal.getScientificNoun();
animalFound = animal;
if (searchValue.equalsIgnoreCase(noun) || searchValue.equalsIgnoreCase(snoun)) {
if (animal instanceof Bird) {
bird = true;
}
found = true;
break;
}
}
if (found){
if (bird){
ArrayList<String> columns = new ArrayList<String>();
columns.add("col1");
TableColumn [] tableColumns = new TableColumn[15];
int columnIndex = 0;
for(String columName : columns) {
tableColumns[columnIndex++] = new TableColumn(columName);
}
table.getColumns().removeAll();
table.getColumns().addAll(tableColumns);
}else{
TableColumn<Animal, String> type = new TableColumn<>("Type");
TableColumn<Animal, String> noun = new TableColumn<>("Noun");
TableColumn<Animal, String> snoun = new TableColumn<>("Sci.Noun");
TableColumn<Animal, Double> minSize = new TableColumn<>("Min Size");
TableColumn<Animal, Double> maxSize = new TableColumn<>("Max Size");
TableColumn<Animal, Double> minWeight = new TableColumn<>("Min Weight");
TableColumn<Animal, Double> maxWeight = new TableColumn<>("Max Weight");
TableColumn<Animal, String[]> colors = new TableColumn<>("Colors");
TableColumn<Animal, String[]> locomotion = new TableColumn<>("Locomotion");
TableColumn<Animal, String[]> countries = new TableColumn<>("Countries");
TableColumn<Animal, String[]> habitats = new TableColumn<>("Natural Habitats");
type.setPrefWidth(100);
noun.setPrefWidth(100);
snoun.setPrefWidth(100);
minSize.setPrefWidth(100);
maxSize.setPrefWidth(100);
minWeight.setPrefWidth(100);
maxWeight.setPrefWidth(100);
colors.setPrefWidth(100);
locomotion.setPrefWidth(100);
countries.setPrefWidth(100);
habitats.setPrefWidth(100);
type.setCellValueFactory(new PropertyValueFactory<>("type"));
noun.setCellValueFactory(new PropertyValueFactory<>("noun"));
snoun.setCellValueFactory(new PropertyValueFactory<>("scientificNoun"));
minSize.setCellValueFactory(new PropertyValueFactory<>("minSize"));
maxSize.setCellValueFactory(new PropertyValueFactory<>("maxSize"));
minWeight.setCellValueFactory(new PropertyValueFactory<>("minWeight"));
maxWeight.setCellValueFactory(new PropertyValueFactory<>("maxWeight"));
colors.setCellValueFactory(new PropertyValueFactory<>("colors"));
locomotion.setCellValueFactory(new PropertyValueFactory<>("locomotions"));
countries.setCellValueFactory(new PropertyValueFactory<>("countriesFoundIn"));
habitats.setCellValueFactory(new PropertyValueFactory<>("naturalHabitat"));
TableColumn [] tableColumns = new TableColumn[11];
tableColumns[0] = type;
tableColumns[1] = noun;
tableColumns[2] = snoun;
tableColumns[3] = minSize;
tableColumns[4] = maxSize;
tableColumns[5] = minWeight;
tableColumns[6] = maxWeight;
tableColumns[7] = colors;
tableColumns[8] = locomotion;
tableColumns[9] = countries;
tableColumns[10] = habitats;
table.getColumns().addAll(tableColumns);
obList.add(animalFound);
table.setItems(obList);
}
}else{
final Stage dialog = new Stage();
dialog.setTitle("Search Results");
dialog.initModality(Modality.APPLICATION_MODAL);
Stage primaryStage = (Stage) search.getScene().getWindow();
dialog.initOwner(primaryStage);
VBox dialogVbox = new VBox(10);
dialogVbox.setAlignment(Pos.CENTER);
dialogVbox.getChildren().add(new Text("No such animal found"));
Scene dialogScene = new Scene(dialogVbox, 300, 100);
dialog.setScene(dialogScene);
dialog.show();
}
}
}