From 226ac7cb14d09e039ce47251d4463b02493ea520 Mon Sep 17 00:00:00 2001 From: Tiago Bagni Date: Tue, 7 May 2024 17:11:39 -0500 Subject: [PATCH] Speed up the settings dialog There are 3 JFileChoosers being instantiated when the dialog is created, and apparently instantiating JFileChoosers is very slow, causing a delay to display the dialog. Defer the instantiation of these file choosers only when they are needed --- .../LogViewerPreferencesDialog.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/tibagni/logviewer/preferences/LogViewerPreferencesDialog.java b/src/main/java/com/tibagni/logviewer/preferences/LogViewerPreferencesDialog.java index 238981d..990fc28 100644 --- a/src/main/java/com/tibagni/logviewer/preferences/LogViewerPreferencesDialog.java +++ b/src/main/java/com/tibagni/logviewer/preferences/LogViewerPreferencesDialog.java @@ -85,9 +85,6 @@ public void windowClosing(WindowEvent e) { } private void initFiltersPathPreference() { - filterFolderChooser = new JFileChooserExt(userPrefs.getDefaultFiltersPath()); - filterFolderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - filtersPathBtn.addActionListener(e -> onSelectFilterPath()); filtersPathTxt.setText(userPrefs.getDefaultFiltersPath().getAbsolutePath()); @@ -108,9 +105,6 @@ private void initFiltersPathPreference() { } private void initLogsPathPreference() { - logsFolderChooser = new JFileChooserExt(userPrefs.getDefaultLogsPath()); - logsFolderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - logsPathBtn.addActionListener(e -> onSelectLogsPath()); logsPathTxt.setText(userPrefs.getDefaultLogsPath().getAbsolutePath()); } @@ -130,8 +124,6 @@ private void initLookAndFeelPreference() { } private void initPreferredEditorPathPreference() { - preferredEditorFileChooser = new JFileChooserExt(userPrefs.getPreferredTextEditor()); - preferredEditorPathBtn.addActionListener(e -> onSelectPreferredEditorPath()); File editorFile = userPrefs.getPreferredTextEditor(); String path = editorFile != null ? editorFile.getAbsolutePath() : null; @@ -150,6 +142,11 @@ public void onCancel() { } private void onSelectFilterPath() { + if (filterFolderChooser == null) { + filterFolderChooser = new JFileChooserExt(userPrefs.getDefaultFiltersPath()); + filterFolderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + } + int selectedOption = filterFolderChooser.showOpenDialog(this); if (selectedOption == JFileChooser.APPROVE_OPTION) { File selectedFolder = filterFolderChooser.getSelectedFile(); @@ -160,6 +157,11 @@ private void onSelectFilterPath() { } private void onSelectLogsPath() { + if (logsFolderChooser == null) { + logsFolderChooser = new JFileChooserExt(userPrefs.getDefaultLogsPath()); + logsFolderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + } + int selectedOption = logsFolderChooser.showOpenDialog(this); if (selectedOption == JFileChooser.APPROVE_OPTION) { File selectedFolder = logsFolderChooser.getSelectedFile(); @@ -195,6 +197,10 @@ private void onShowLineNumbersChanged() { } private void onSelectPreferredEditorPath() { + if (preferredEditorFileChooser == null) { + preferredEditorFileChooser = new JFileChooserExt(userPrefs.getPreferredTextEditor()); + } + int selectedOption = preferredEditorFileChooser.showOpenDialog(this); if (selectedOption == JFileChooser.APPROVE_OPTION) { File selectedFolder = preferredEditorFileChooser.getSelectedFile();