将滚动条添加到警报中
Add Scrollbar into Alert
我有一种情况,我必须在 javafx 中输出一个巨大的字符串 Alert
public static Optional<ButtonType> showAlertDialog(AlertType type, String title, String content) {
TextArea textArea = new TextArea(content);
textArea.setEditable(false);
textArea.setWrapText(true);
GridPane gridPane = new GridPane();
gridPane.setMaxWidth(Double.MAX_VALUE);
gridPane.add(textArea, 0, 0);
Alert alert = new Alert(type);
alert.setTitle(title);
alert.setContentText(content);
return alert.showAndWait();
}
当字符串大小超过屏幕大小时,我无法读取字符串的隐藏部分,如何将Scrollbar
添加到Alert
目前,您没有将 TextArea
或 GridPane
添加到 Alert
。您创建了它们,但随后什么都不做;相反,您只是设置 Alert
的 contentText
。您实际上需要将具有内置滚动功能的 TextArea
添加到 Alert
.
一种方法是设置 DialogPane.content
属性 而不是 contentText
属性.
private Optional<ButtonType> showAlert(AlertType type, String title, String content) {
Alert alert = new Alert(type);
alert.setTitle(title);
TextArea area = new TextArea(content);
area.setWrapText(true);
area.setEditable(false);
alert.getDialogPane().setContent(area);
alert.setResizable(true);
return alert.showAndWait();
}
另一种方法是将 TextArea
添加为 expandableContent
并让 contentText
成为更短的消息。
private Optional<ButtonType> showAlert(AlertType type, String title, String shortMessage, String fullMessage) {
Alert alert = new Alert(type);
alert.setTitle(title);
alert.setContentText(shortMessage);
TextArea area = new TextArea(fullMessage);
area.setWrapText(true);
area.setEditable(false);
alert.getDialogPane().setExpandableContent(area);
return alert.showAndWait();
}
我有一种情况,我必须在 javafx 中输出一个巨大的字符串 Alert
public static Optional<ButtonType> showAlertDialog(AlertType type, String title, String content) {
TextArea textArea = new TextArea(content);
textArea.setEditable(false);
textArea.setWrapText(true);
GridPane gridPane = new GridPane();
gridPane.setMaxWidth(Double.MAX_VALUE);
gridPane.add(textArea, 0, 0);
Alert alert = new Alert(type);
alert.setTitle(title);
alert.setContentText(content);
return alert.showAndWait();
}
当字符串大小超过屏幕大小时,我无法读取字符串的隐藏部分,如何将Scrollbar
添加到Alert
目前,您没有将 TextArea
或 GridPane
添加到 Alert
。您创建了它们,但随后什么都不做;相反,您只是设置 Alert
的 contentText
。您实际上需要将具有内置滚动功能的 TextArea
添加到 Alert
.
一种方法是设置 DialogPane.content
属性 而不是 contentText
属性.
private Optional<ButtonType> showAlert(AlertType type, String title, String content) {
Alert alert = new Alert(type);
alert.setTitle(title);
TextArea area = new TextArea(content);
area.setWrapText(true);
area.setEditable(false);
alert.getDialogPane().setContent(area);
alert.setResizable(true);
return alert.showAndWait();
}
另一种方法是将 TextArea
添加为 expandableContent
并让 contentText
成为更短的消息。
private Optional<ButtonType> showAlert(AlertType type, String title, String shortMessage, String fullMessage) {
Alert alert = new Alert(type);
alert.setTitle(title);
alert.setContentText(shortMessage);
TextArea area = new TextArea(fullMessage);
area.setWrapText(true);
area.setEditable(false);
alert.getDialogPane().setExpandableContent(area);
return alert.showAndWait();
}