在我的程序的一部分中,我有一个JList
它有一个位置列表,我有一个API,它应该使用JList
中的一个项目并打印走出那个地方的天气。所以现在我不能这样做,因为我用如何获取JList中选择的项目并使用铸造
WeatherAPI chosen = locList.getSelectedIndex();
,但有一个错误:类型不匹配:不能从int到WeatherAPI转换。
这是工作的API的例子:
LinkedList<WeatherAPI> stations = FetchForecast.findStationsNearTo("cityname");
for (WeatherAPI station : stations) {
System.out.println(station);
}
WeatherAPI firstMatch = stations.getFirst();
,所以我不希望得到的第一个选项,我想用户所选择的位置。这完全是关于铸造。 我也试过这个,没有工作。
WeatherAPI stations;
WeatherAPI firstMatch = stations.get(locList.getSelectedIndex());
我得到了代码的其余部分,它使用了“firstMatch,但只使用它时,它的类型是WeatherAPI
尝试.getSelectedValues()? –
类型不匹配:无法从Object转换为WeatherAPI –
Crosspost:http://www.coderanch.com/t/608471/GUI/java/selected-item-JList-casting。为什么你在浪费每个人时间发布一个已经回答的问题? – camickr