This question already has answers here:
Detect first time user in java app
(3 answers)
Closed 6 years ago.
I would like to pop up a window to select a file location when the user launch the software for the first time. I'm new to javafx and I looked for an answer on the web but no success...
Thanks in advance
I think I found it.
primaryStage.setOnShowing(event -> {
//Code here
});
It does action on first startup. I don't know if it's the best way to do it but that's how I did it. I already have a file with about 3 lines. I just added a 4th one with a random word and when the user launch the app it check if the word exist in the file. If so, it does nothing. If not, it ask the user to select the folder and if the selection is successful, it write the word.
Firstly, I would want to point out that your phrasing probably isn't clear enough for most people to understand exactly what you need. I am going to assume you have some kind of settings (like default application storage directory) which you need the user to specify at the first time the JAR is run. If the JAR file is run subsequently, it should not prompt for that again and use the settings previously specified.
Typically, when the user runs the JAR file, all the data would be isolated within that session. If the user closes the application and opens the application again, it would behave just like the previous run.
If you need to persist these data or settings, you can use Properties. This will save data in a separate file. The normal convention is to save it in the same folder as the JAR file, and named as config.properties.
At the start of the application, you should check if this file exists, if it does not exist, it means that this is the first run. Subsequently, when the user set the data (e.g. file folder), you would save it to the file.
You can find an example here.
Background
In order to implement a file selector, we can make use of JavaFX’s FileChooser. This will open a window giving us the opportunity to select a file.
What you’re asking for is for a FileChooser to open prior to entering the actual application. Let’s have a look at the implementation for something like that!
Implementation
At first, we’re going to need a JavaFX Application class that will open a window if we were to create a new instance out of it:
public class App extends Application
{
private final File file;
public App(File file)
{
this.file = file;
// Optionally provide ‘launch’ with some arguments
Application.launch();
}
#Override
public void start(final Stage stage)
{
// ...
stage.setScene(new Scene(insertNodeHere));
}
}
As I’m certain you’re already aware of—a class like this will open up a new window. This is the separate application class we’ll be calling once we’ve retrieved a File using the FileChooser in our main class.
In our main class, we’ll put this:
File file = fileChooser.showOpenDialog(stage);
if (file != null)
new App(file);
This will launch your application if the selected file didn’t turn out as null.
Moreover
With the implementation above in mind, you can complicate things as much as you feel like. Perhaps you’d like the application to start even if the file were null? In that case, there’s no need for the if statement.
Related
This question already has answers here:
How to programmatically clear application data
(10 answers)
Closed 8 years ago.
am making a app that clears the internet cache when you click a button. In which i want it to have the permission you clear other app data. I have tried many different codes and cant find a one that is used for this purpose. Is there a special permission i need or is it even possible.
I have tried this code in MainActivity.java but cant seem to get it to work:
private void clearPreferences() {
try {
// clearing app data
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear com.android.browser");
} catch (Exception e) {
e.printStackTrace();
}
}
Any help would be great. Thanks!
To clear it you simply delete the contents of it's data directory. There are 2 locations one on the SD, and another internally. You can clear the one on SD no problem, just use Apache FileUtils from Apache Commons, it has a function to delete a directory recursively. The internal dir requires root access. Or to be of same user. If you made both apps then it should be no problem just make sure they are the same user by setting sharedUserId parameter. If not, then your app could only work as root. See this answer to get data directory.
An alternative to rooting, you can open up the app info page for the app in question and tell the user to clear the app data manually by tapping on the clear data & clear cache buttons.
I want to find a way to display a notification (like a JOptionpane, a JLabel or any other type) only one time after a user launches my application that is formed in a.jar file.
By only one time, I mean that the user gets a one-time notification after the first use, then for every following times my application runs, this notification should not appear.
My application uses Java Swing. Is there a hint how to make a message pops up from the main JFrame for example?
You simply need to know whether this application has already been running in that environment before or not. A simple way to do that is to:
Check whether some file with a particular name exists in the working directory
if it doesn't: show your notification, then create the file
if it does: don't show your notification
Sample Java code:
private static void notify() {
final File file = new File(".launched");
if(!file.exists()) {
// show your notification HERE
file.createNewFile();
}
}
Check for stored value on disk indicating the message has been shown
If not, show the message and store the value on disk.
You can do that by setting up one preference. I think is the most straight forward way to do it. Use the preferences class.
The preferences are loaded at starting, then you ask if your "boolean_first_use" is false or true. After that you set it to false, as you know that the user is having that first time message. So next time, it will not fire the notification.
http://www.vogella.com/tutorials/JavaPreferences/article.html
http://www.javaranch.com/journal/2002/10/preferences.html
Currently my application using this path for taking images:
D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\webapps\patternImages
So inside of this folder might be n number of folders may contains n number folders and inside of the folders it may contain n number of images,
I need to get all the image names.
In this case there are 2 scenario we go for usually getting image path
Using the static string path to get the image names
Using system.getproperty()
But I need more dynamic way of approach:
By this "patternImages" folder can be placed anywhere in the web and I should have to get all the folders name and image names without any issue and also should know which folder contains which image? any way?
It's hard to say what would be a perfect fit for your application since we don't know everything you're doing, but since it's a GUI application, I imagine presenting the user with a GUI to pick the file path is your best option. What class you use for that depends on what GUI library you're using, but here's a tutorial for JFileChooser: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html. The basic code you need is this:
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
} else {
// Do nothing or log it
}
where fc is an instance of JFileChooser. Just make sure you configure the object for picking directories and not files. You would probably wire this up to a button. Whatever library you use should provide some kind of dialog element to allow the user to pick a directory.
If a graphical directory picker is not an option, then I think you're stuck with a configuration file. Even if you can use a directory picker, you might want to consider a configuration file to save the user's last choice.
Following on from my previous question, my program doesn't detect the 300 images that have just been created in a particular directory; instead, it only detects desktop.ini, which is not the case as I can physically see that the files have been created within said directory and do exist.
Can somebody please explain why this happens as when I run the program the next time, it seems to work just fine?
The only way that something is detected within the directory on the first run is when there is at least one file which exists in the directory before the program is compiled and executed.
Many thanks.
UPDATE: Files are detected as follows:
//Default greyscale image directory (to convert from greyscale to binary).
static File dirGrey = new File("test_images\\Greyscale");
//Array of greyscale image filenames.
static File imgListGrey[] = dirGrey.listFiles();
without knowing how you create the images, this question is akin to 'How many kittens are under my desk right now?'
Are you creating the files yourself? If so, are you closing any file handles referring to those files once they are created?
You're creating the file list in a static array, and it's created when the class containing the array is loaded by the Java class loader, which is probably before you create the image files. That's why the array contains an outdated list.
static is rarely needed, mostly useful for constants (things that never change, such as 42), for pure functions (Math.sqrt()) and a few other special cases. When you use it, you have to learn all the tricky initialization order stuff. Otherwise, just stick with non-static variables.
I have to develop a small app to compare automatically generated folders. It must compare the folders, sub-folders and file contents. The problem is that this app needs to be launched either from a user on his computer to manually check for changes, or automatically along with the ANT nightlies. In the first case the results are displayed as a table in the Swing GUI. But in the other case, it must create a file to put the results in (format doesn't matter, XML, CSV, ...).
Anyone got some tips, or a link to a tutorial ?
You might want to add some command line option that switches between ui and file export, e.g. --gui or --export=[filename]. You could use Apache Commons CLI for that.
The other method is to create a set of classes that performs the task, and returns a set of values, which can then be either written to disk, or displayed in a GUI. I.e., an engine, and two front-ends (the GUI and the CLI).
for example:
public interface DirectoryComparer {
CompareResult performCompare(Directory dir1, Directory dir2);
public static interface CompareResult {
//...things here that you need, such as, file or dir difference, etc
Iterable<File> getFileDiff();
Iterable<Directory> getDirectoryDiff();
}
}
then, the GUI clients will just use DirectoryComparer to display the results, and the CLI client will write these results to a file or three. But those two clients are completely separate, and can be maintained separately.