简体   繁体   English

在javafx中可变地设置Dialog的ContentText

[英]variably set ContentText of Dialog in javafx

For the user to confirm some changes I want a confirmation dialog to pop up, asking whether the selected Objects shall be deleted. 为了让用户确认某些更改,我想弹出一个确认对话框,询问是否应删除所选对象。 This is variable of course. 这当然是可变的。 The Text in the confirmation dialog should look like this: 确认对话框中的文本应如下所示:

Are you sure you want to delete   
Sample 1 from class 2  
Sample 3 from class 1  
Sample... and so on  
?

I have an ArrayList of Objects in which the information about samplename and class are stored. 我有一个对象的ArrayList,其中存储了有关samplename和class的信息。 What I got so far is: 到目前为止,我得到的是:

ObservableList<Samples> samples = table.getSelectionModel().getSelectedItems();
Alert info = new Alert(Alert.AlertType.CONFIRMATION);
info.setHeaderText("Confirm deletion");

if (samples.size() > 1) {
    List<String> strings= new ArrayList<>();
    samples.forEach(e ->
          strings.add(e.getSample().getSampleNr() +
                  " of class " +
                   e.getSample().getClass() +"\n")
            );
info.setContentText("Are you sure you want to delete \n" +
                    stridon't+ "?"
);

I dont know, maybe my thinking is blocked but I can't get it to work nicely. 我不知道,也许我的想法受阻了,但我无法使其正常运行。 It displays like this: 它显示如下:

Are you sure you want to delete  
[Sample 1 of class 3  
, Sample 3 of class 1  
, ..and so on  
]?

Does anyone know how to get rid of the [,] signs or maybe a nicer way o do this? 有谁知道如何摆脱[,]符号,或者这样做的更好方法? thank you in advance! 先感谢您!

您可以使用的一件事是String.join()

String.join("delimiterHere", listHere); 


info.setContentText("Are you sure you want to delete \n" + String.join("\n", strings) + "?");

Another thing you can do is use a string builder 您可以做的另一件事是使用字符串生成器

StringBuilder stringBuilder = new StringBuilder("Are you sure you want to delete \n");
samples.forEach(e ->
        stringBuilder.append(e.getSample().getSampleNr())
                .append(" of class ")
                .append(e.getSample().getClass())
                .append("\n"));

info.setContentText(stringBuilder.toString());

Test Output:(Sample Data) 测试输出:(样本数据)

Are you sure you want to delete
Sample 0 of class Class 0
Sample 1 of class Class 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM