我是新手使用javafx我想在javafx中使用带有唯一元素的列表我如何管理它。显示List视图的内容应该是唯一的重复值,跳过。 谢谢
static ObservableList<String> items = FXCollections.observableArrayList();
static ObservableList<String> bamitems = FXCollections
.observableArrayList();
static ListView<String> list;
static ListView<String> listforbam;
public static void test() throws Exception {
HBox root = new HBox();
VBox yesaccordion = new VBox();
Accordion acc = new Accordion();
acc.getPanes().addAll(createPanes());
yesaccordion.getChildren().add(acc);
root.getChildren().addAll(yesaccordion);
jfxpanel.setScene(new Scene(root, 200, 100));
panel.add(jfxpanel);
items.addAll(LoadVCFFileToTrack.destitemlist);
bamitems.addAll(LoadVCFFileToTrack.destitemBamlist);
if (LoadVCFFileToTrack.destitemBamlist.size() > 0) {
listforbam = new ListView<String>();
listforbam.setItems(bamitems);
}
if (LoadVCFFileToTrack.destitemlist.size() > 0) {
list = new ListView<String>();
list.setItems(items);
}
答案 0 :(得分:2)
使用Set
或更好的ObservableSet
来存储您要在ListView中显示的项目。
要将该集添加到列表视图,您可以使用FXCollections.observableArrayList(Collection col)
方法。它会将ObservableSet
转换为ObservableArrayList
。
listView.setItems(FXCollections.observableArrayList(observableSet));
完整示例
public class Main extends Application {
@Override
public void start(Stage stage) {
ObservableSet<String> observableSet = FXCollections.observableSet();
//Item1 is repeated twice
observableSet.addAll(Arrays.asList("Item1","Item2","Item3", "Item1"));
ListView<String> listView = new ListView<>();
listView.setItems(FXCollections.observableArrayList(observableSet));
stage.setScene(new Scene(listView, 200, 200));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}