到目前为止,我正在尝试传递一个数组,这样我就可以将它传递给ComboBox,但目前我正试图找出为什么它似乎没有出现而不给我一个错误,我是一个新手,很难理解我在代码中做错了什么。
package lab2;
import java.io.BufferedReader;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import static java.nio.file.Files.lines;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import static java.util.Arrays.sort;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Label stateLabel;
@FXML
private Label zipcodeLabel;
@FXML
private Label timezoneLabel;
@FXML
private ComboBox<CityData> selectCityCombo;
@FXML
private ArrayList<String> CityData;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@FXML // handles the action to read csv file when read button is clicked
private List<List<String>> readFromFile(ActionEvent event){
List<List<String>> listOfCityData = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader (new File("CityData2.csv")))) {
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
String[] values = line.split(",");
listOfCityData.add(Arrays.asList(values));
}
} catch (IOException e )
{
e.printStackTrace();
System.out.println("Error File Not Correctly Read");
}
return listOfCityData;
};
@FXML//Grabs the data from the csv data array created and sends it to the comboBox to display
private void selectCityCombo(ActionEvent event) {
CityData sel = selectCityCombo.getValue();
stateLabel.setText("State: " + sel.getState());
zipcodeLabel.setText("Zipcode: " + sel.getZipcode());
timezoneLabel.setText("Timezone: " + sel.getTimezone());
}
CityData listOfCityData[] = {
new CityData()
};
ObservableList<String> list = FXCollections.observableArrayList(Arrays.toString(listOfCityData));
@Override
public void initialize(URL url, ResourceBundle rb) {
for (int i = 0; i < listOfCityData.length; i++)
selectCityCombo.getItems().add(listOfCityData[i]);
}
}这就是我到目前为止提出的方法,但我似乎不知道如何将listOfCityData作为数组传递给CityData listOfCityData[]数组,以便将其传递给selectCityCombo
发布于 2020-09-24 11:50:22
您已经拥有了大部分正确的代码,但您需要以正确的顺序执行这些操作,并将ObservableList链接到ComboBox yourComboBox.setItems(...)。
这一行看起来是你最大的问题:因为listOfCityData是空的,所以当你的代码第一次启动时,它将是空的。一旦你修复了它,并链接了上面提到的盒子,那么你就应该回到业务中来了。
例如,下面显示了一个正确的顺序并链接到ComboBox:
//Create empty list to be used for ComboBox selection
ObservableList<String> list = FXCollections.observableArrayList();
//Link empty ObservableList to the ComboBox
selectCityCombo.setItems(list);
//Then the readFromFile method could look something like this
private List<String> readFromFile(){
//Create intermediate list
List<String> listOfCityData = new ArrayList<>();
//Read the data in
BufferedReader reader = new BufferedReader(new FileReader (new File("CityData2.csv")))) {
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
String[] values = line.split(",");
for (int i = 0; i <values.length; i++)
{
//Add each item to the intermediate list
listOfCityData.add(line[i]);
}
}
return listOfCityData;
}
//Finally none of this will work unless you call the readFromFile method during initialize, or you could call it from a button event
public void initialize(URL url, ResourceBundle rb) {
//Call the readFromFile method and get the data
List<String> listOfCityData = readFromFile();
//Now update the ObservableList:
list = FXCollections.observableArrayList(Arrays.toString(listOfCityData));
//Alternatly you could link the list to the ComboBox here
selectCityCombo.setItems(list);
} https://stackoverflow.com/questions/64038279
复制相似问题