Properly dispose component config dialog (for dark theme UI)

This commit is contained in:
SiboVG 2023-10-23 19:23:37 +02:00
parent 8ca59685ed
commit 3e33c8fceb
3 changed files with 36 additions and 16 deletions

View File

@ -303,14 +303,21 @@ public class RocketComponentConfig extends JPanel {
}
// Yes/No dialog: Are you sure you want to discard your changes?
JPanel msg = createCancelOperationContent();
int resultYesNo = JOptionPane.showConfirmDialog(RocketComponentConfig.this, msg,
trans.get("RocketCompCfg.CancelOperation.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (resultYesNo == JOptionPane.YES_OPTION) {
ComponentConfigDialog.clearConfigListeners = false; // Undo action => config listeners of new component will be cleared
disposeDialog();
document.undo();
}
SwingUtilities.invokeLater(() -> {
JPanel msg = createCancelOperationContent();
int resultYesNo = JOptionPane.showConfirmDialog(RocketComponentConfig.this, msg,
trans.get("RocketCompCfg.CancelOperation.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (resultYesNo == JOptionPane.YES_OPTION) {
ComponentConfigDialog.clearConfigListeners = false;
// Need to execute after delay, otherwise the dialog will not be disposed
GUIUtil.executeAfterDelay(100, () -> {
disposeDialog();
document.undo();
});
}
});
}
});
buttonPanel.add(cancelButton, "split 2, right, gapleft 30lp");

View File

@ -365,15 +365,10 @@ public class GeneralPreferencesPanel extends PreferencesPanel {
trans.get("generalprefs.ImportWarning.title"),
JOptionPane.WARNING_MESSAGE);
// Introduce a small delay before showing the PreferencesDialog
Timer timer = new Timer(100, new ActionListener() { // 100ms delay
@Override
public void actionPerformed(ActionEvent arg0) {
PreferencesDialog.showPreferences(parent.getParentFrame()); // Refresh the preferences dialog
}
// Need to execute after delay, otherwise the dialog will not be disposed
GUIUtil.executeAfterDelay(100, () -> {
PreferencesDialog.showPreferences(parent.getParentFrame()); // Refresh the preferences dialog
});
timer.setRepeats(false); // Only execute once
timer.start(); // Start the timer
}
});
}

View File

@ -51,6 +51,7 @@ import javax.swing.RootPaneContainer;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeListener;
@ -761,5 +762,22 @@ public class GUIUtil {
}
}
/**
* Executes the given code after a specified delay.
*
* @param delayMillis the delay in milliseconds.
* @param runnable the code to be executed after the delay.
*/
public static void executeAfterDelay(int delayMillis, Runnable runnable) {
Timer timer = new Timer(delayMillis, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
runnable.run();
}
});
timer.setRepeats(false);
timer.start();
}
}