Is there any way to resize an Alert, to show a trace of an exception, in JavaFX 8. My code is :
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setResizable(true);
alert.setTitle(e.getLocalizedMessage());
alert.setHeaderText(e.toString());
alert.setContentText(message); // I previously made this using StackTraceElement[] java.lang.Exception.getStackTrace()
Then I've tried both :
alert.getDialogPane().getScene().getWindow().sizeToScene();
and
alert.getDialogPane().autosize();
and neither has worked.
Currently, I'm using my JRE is Java 8 update 144 and my JDK is Java 8 update 101 on Windows 10 Home 64-bit edition. Are there any ways for me to use the Alert framework as a replacement for JOptionPane seeing as Swing is essentially dead so that I can avoid having to code listeners and buttons and keep the cross icon which the Alert class "provides".
This code is pulled from Exception Dialog .
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");
Exception ex = new FileNotFoundException("Could not find file blabla.txt");
// Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();
Label label = new Label("The exception stacktrace was:");
TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);
// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);
alert.showAndWait();
I found a way to do this using Java reflection. First have to set ( comment of Ervin Szilagyi) alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
and then use this method:
void sizeToScene(Alert alert) {
try {
final Field field = Dialog.class.getDeclaredField("dialog");
field.setAccessible(true);
final Object object = field.get(alert);
final Method method = object.getClass().getMethod("sizeToScene");
method.setAccessible(true);
method.invoke(object);
} catch (SecurityException | IllegalArgumentException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
// logger.catching(e);
}
}
but it is better to use the Exception Dialog to display the trace of the exception.
Usage example:
MainThread
private static Alert alert;
public static Optional<Alert> getAlert(){
return Optional.ofNullable(alert);
}
void alertMethod(){
alert = new Alert(AlertType.ERROR);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setTitle("title");
alert.setHeaderText(null);
alert.setContentText("message");
alert.showAndWait();
}
Another thread
//...
void addAlertText(String text){
MainThread.getAlert()
.ifPresent(
al->Platform.runLater(()->{
al.setContentText(al.getContentText() + '\n' + text);
sizeToScene(al);
}));
}
//...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.