2727import java .util .List ;
2828import java .util .Objects ;
2929import java .util .Set ;
30+ import java .util .prefs .BackingStoreException ;
3031import java .util .prefs .Preferences ;
3132import java .util .stream .Collectors ;
3233import java .util .stream .IntStream ;
3334
3435import javax .swing .JComponent ;
3536import javax .swing .JMenuItem ;
3637
38+ import org .slf4j .Logger ;
39+ import org .slf4j .LoggerFactory ;
40+
3741import com .google .auto .service .AutoService ;
3842
3943/**
@@ -47,6 +51,7 @@ public class LoadRecentProject extends Load {
4751 /** The number of menu items used for recent files */
4852 private static final int NUMBER_OF_MENU_ITEMS = 9 ;
4953 private static final Set <String > commands = new HashSet <>();
54+ private static final Logger log = LoggerFactory .getLogger (LoadRecentProject .class );
5055
5156 static {
5257 commands .add (ActionNames .OPEN_RECENT );
@@ -89,6 +94,11 @@ private static File getRecentFile(ActionEvent e) {
8994 * @return a List of JMenuItem, representing recent files. JMenuItem may not be visible
9095 */
9196 public static List <JComponent > getRecentFileMenuItems () {
97+ try {
98+ prefs .sync ();
99+ } catch (BackingStoreException e ) {
100+ log .warn ("Unable to sync preferences for recent files" , e );
101+ }
92102 List <JComponent > menuItems = new ArrayList <>();
93103 // Get the preference for the recent files
94104 for (int i = 0 ; i < NUMBER_OF_MENU_ITEMS ; i ++) {
@@ -223,6 +233,11 @@ public static String getRecentFile(int index) {
223233 */
224234 private static void setRecentFile (int index , String fileName ) {
225235 prefs .put (USER_PREFS_KEY + index , fileName );
236+ try {
237+ prefs .flush ();
238+ } catch (BackingStoreException e ) {
239+ log .warn ("Unable to flush preferences for recent files" , e );
240+ }
226241 }
227242
228243 private static void removeRecentFile (int index ) {
0 commit comments