简体   繁体   English

如何调整警报窗口的大小以匹配 JavaFX 8 中的内容

[英]How to resize an Alert window to match contents in JavaFX 8

Is there any way to resize an Alert, to show a trace of an exception, in JavaFX 8. My code is :有什么方法可以在 JavaFX 8 中调整警报大小以显示异常痕迹。我的代码是:

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.目前,我使用的 JRE 是 Java 8 update 144,我的 JDK 是 Windows 10 Home 64 位版本上的 Java 8 update 101。 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".有什么方法可以让我使用 Alert 框架来替代 JOptionPane,因为 Swing 基本上已经死了,这样我就可以避免编写侦听器和按钮并保留 Alert 类“提供”的十字图标。

This code is pulled from Exception Dialog .此代码是从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.我找到了一种使用 Java 反射来做到这一点的方法。 First have to set ( comment of Ervin Szilagyi) alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);首先必须设置(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);
        }));
}
//...

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

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