我在Scene Builder中创建了一个ComboBox,我想用文本文件(例如Text.txt)填充他:
public class ToDoListController实现Initializable {
@FXML
private ComboBox<?> eventsSelector;
怎么做?
非常感谢!
两种解决方案: 1。
@FXML private ComboBox eventsSelector;
@Override
public void initialize(URL location, ResourceBundle resources) {
List<String> myList;
try {
myList = Files.lines(Paths.get("path of my text file")).collect(Collectors.toList());
eventsSelector.setItems(FXCollections.observableArrayList(myList));
} catch (IOException e) {
System.out.println("Don t find file");
}
} 2。
//Read items from txt File
try {
BufferedReader br = new BufferedReader(new
FileReader("path of my text file"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
//Add Item
eventsSelector.getItems().add(line);
sb.append(line);
line = br.readLine();
}
br.close();
} catch (IOException e) {
System.out.println("Don t find file");
}
答案 0 :(得分:0)
由于您要添加.txt
文件中的内容,ComboBox
中的项目为String
,因此您可以更改为:
@FXML
private ComboBox<String> eventsSelector;
然后您需要一个要添加到ComboBox<String>
的元素列表,然后您可以简单地添加它们:
List<String> myList = Files.lines(path).collect(Collectors.toList());
comboBox.setItems(FXCollections.observableArrayList(myList));
答案 1 :(得分:0)
我为你写了一些代码,这应该适合你:
select duration, (case when host <10 then 'pool1' when host between 10 and 19 then 'pool2')
from Jobs
group by (case when host <10 then 'pool1' when host between 10 and 19 then 'pool2');
在这种情况下,txt文件必须位于项目的根目录中。