I want to create a window with Java Swing. The window will have a menu bar with a File->Open button from where the user can select a file from hid hard drive. The File menu should also have a list of the most recent opened items, like many other applications show. Does anyone know what is the best approach?
I would suggest using the Preferences class to persist the most recently opened items. That way, if the user restarts the application the items will still be available.
Note that on Windows the Preferences class stores data in the registry, which is how many native Windows applications store and retrieve recently open file names.
Also, note that the Preferences class simply acts as an API for storing and retrieving (key, value) pairs. You will still need to decide how you wish to store the information, and be responsible for dynamically building / updating the JMenu when a new file is accessed. To accomplish this I would suggest implementing a Action (extend AbstractAction) to deal with when the user attempts to open a file. When the Action runs it should persist the newly accessed file's name to the Preferences class and dynamically rebuild the File JMenu (in addition to opening the file).
Related
I have an android app that uses a properties file that is currently stored on the sdcard.
I want to allow the user to manually edit this properties file.
Is there a preferred editor that people use or do you roll your own for your app?
Is there a preferred editor that people use
I am not aware of any Android device that ships with a text editor, let alone a dedicated properties file editor. I would be fairly surprised if there are any.
The Android SDK does not ship with a properties file editor, unless you count EditText. I am not aware of any libraries that implement a properties file editor.
do you roll your own for your app?
Most Android developers do not use properties files on-device. Instead, they use preferences, which come with their own UI (PreferenceScreen and kin) and backing store (SharedPreferences, plus PreferenceDataStore on Android O).
You would need to create your own UI for editing your properties. Or, depending upon your use case, you might implement a more traditional preference-based system, using the properties file for import/export.
I have two options to backup my code. One is default path(home/workingDirectory/backup) and another one is custom path. This custom path will be selected by user. If user clicks custom path button or text box the local file selector dialog box is open and get the user's path. I completed the functionality for backup option but I dont know how to get the user selecting path.
For security reasons browsers do not allow access to a file path for a selected file.
You can ask a user to choose where to save a file, and then ask a user to upload a file, but you cannot access random directory on a user's hard drive from your app.
I've created an application that is going to be run on Windows, Mac OX and Linux. I need to be able to store and read user settings on the fly.
User settings take the form of strings and a key and value pair would work well.
I'm currently using a properties file however this can't be written too on the fly within a JAR.
I'm struggling to find alternatives, what are the options to be able to do this?
You can save the properties file in the user home directory using System.getProperty("user.home") assuming the security manager, if any, allows it. Using properties files to save preferences allows editing the preferences from outside the application.
Another option is to use the Java preferences API for a transparent and platform-independent way of persisting the preferences.
How do I link the drop down lists with files in hard disk, so that on selecting an option in drop down list (which is actually a folder in one of the drive of the hard disk) must get selected and it must display the files present in the folder in the same list?
I think what you need is a JFileChooser. You can find a tutorial on how to use it here.
You should get the directories you want to show (java.io.File instances) and use these directories as the items of the combo box. Then add an action listener of this combo box. This listener will be called each time the user selects one of the directories in the combo box. Then just ask the combo box its selected item, and list the files of the selected directory.
You may customize how the directories are displayed inside the combo box using a ListCellRenderer.
I suggest you read the Swing tutorial, where all this is well explained.
I have a plugin where for a particular type of file i will enable some options to perform. so now i have requirement where i need to decide the type of a file dynamically and apply a particular options for that type of file.
Is it possible to do that? if yes, need some help about the same.
Thanks.
So I guess you want to show the context menu for something that represents a file in a view. Determine the file type based on custom rules (file extension is the easiest rule, looking inside the file the most complex) and create the popup or distribute content to an existing one.
If it's your own view, maybe a list or tree that shows a folder structure, then you have to register the Viewer as a SelectionProvider, listen to selection events, evaluate the selection (maybe a String or a File object) and create the popup menu.
Contributing actions to existing popup menus (like the navigator view context menus) is possible as well but a bit more challenging.