Display last opened items in menu bar

I want to create a window with Java Swing. The window will have a menu bar with the File-> Open button, from which the user can select a file from the hard drive. The File menu should also have a list of the most recently opened items, as shown in many other applications. Does anyone know what works best?

+4
source share
1 answer

I would suggest using the Preferences class to keep the most recent open items. Thus, if the user restarts the application, the elements will still be available.

Note that on Windows, the Preferences class stores data in the registry, namely, how many native Windows applications store and retrieve recently opened file names.

Also note that the Preferences class simply acts as an API for storing and retrieving (key, value pairs). You still need to decide how you want to store the information, and be responsible for dynamically creating / updating JMenu when accessing the new file. For this, I would suggest implementing Action (extend AbstractAction ) to decide when the user will try to open the file. When Action starts, it must save the newly accessible file name in the Preferences class and dynamically rebuild the JMenu file (in addition to opening the file).

+6
source

Source: https://habr.com/ru/post/1313085/


All Articles