JavaFX Alert ไม่สามารถใส่เนื้อหาได้

ฉันพยายามแสดงสตริงขนาดใหญ่เป็นการเตือนไม่สำเร็จ

public void customAlert(String header, String body, ButtonType customBtn) {
    clearAlert();
    alert.setHeaderText(header);
    //alert.setContentText(body);
    String content = "";
    int counter = 1;

    int y=0;
    for (int x = 0; x < body.length(); ++x) {
     //    System.err.println("concat");
        if (x > counter * 50 && body.charAt(x) == ' ') {
          //  System.err.println("concat");
            ++counter;
            for (; y < x; ++y) {
                content.concat(Character.toString(body.charAt(y)));

            }
            content.concat(Character.toString('\n'));
           // System.err.println(content);
        }
    }
    alert.getDialogPane().setContent(new Label(content));

    alert.getDialogPane().setMaxWidth(500);

    if (customBtn != null) {
        alert.getButtonTypes().clear();
        alert.getButtonTypes().add(customBtn);
    }
    alert.showAndWait();
}

ก่อนอื่นฉันลองใช้ setContentText() แต่มันไม่แสดงสตริงทั้งหมด จากนั้นฉันก็ใช้ alert.getDialogPane().setContent(new Label(body)); - แต่กล่องแจ้งเตือนกลับกว้างกว่าขนาดหน้าจอ จากนั้นฉันพยายามกรองสตริงที่เพิ่มอักขระขึ้นบรรทัดใหม่และตั้งค่า maxWidth เป็นการแจ้งเตือน แต่การกรองไม่ทำงาน


person Robert    schedule 02.05.2017    source แหล่งที่มา
comment
บางทีนี่อาจช่วยได้: stackoverflow.com/questions/28937392/   -  person Mark    schedule 02.05.2017
comment
ใช้กล่องโต้ตอบข้อยกเว้นจากที่นี่   -  person Sedrick    schedule 02.05.2017


คำตอบ (1)


สำหรับข้อความยาว คุณควรนำเสนอใน TextArea:

TextArea textArea = new TextArea(text);
textArea.setPrefColumnCount(40);
textArea.setPrefRowCount(20);
textArea.setEditable(false);
textArea.setWrapText(true);
alert.getDialogPane().setContent(textArea);
person VGR    schedule 02.05.2017
comment
ใช้งานได้ แต่ฉันใช้อ็อบเจ็กต์ Alert หนึ่งอินสแตนซ์สำหรับการแจ้งเตือนหลายรายการ เมื่อฉันต้องการลบพื้นที่ข้อความออกจากบานหน้าต่างโต้ตอบ ฉันไม่สามารถเพิ่ม setContentText() ใด ๆ ได้ - person Robert; 02.05.2017
comment
หากคุณโทร getDialogPane().setContent(null) ก่อน คุณก็สามารถโทร setContentText ได้ กฎสำหรับลำดับความสำคัญนี้อธิบายไว้ใน เอกสารประกอบสำหรับ contentText - person VGR; 02.05.2017