I have a class of animal. Which has different attributes. I want to show all attributes in different cells. but the attributes with array are shown like below. How can i achieve to print an array in the cell.
Below is my code. Data is read from a file and added to an Arraylist of animal in the initialize method. All the functionality is done in the search method. The table columns are created dynamically on run time. and that Arraylist of animals is passed to observable list. At the end the the observable is set to table. But I dont know how to set the value of a cell as an array so that it can print the array in a cell.
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();
}
}
}