my dog learned polymorphism*
The moose likes JavaFX and the fly likes Connecting my application to windows vista save dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Connecting my application to windows vista save dialog" Watch "Connecting my application to windows vista save dialog" New topic
Author

Connecting my application to windows vista save dialog

Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
Hi,

I have my budget application that saves my current budget information in a pre-specified file in a pre-specified location. However, when I click my save button I want it to open the windows vista save dialog so that I can choose the name and location from there instead of have the name and location pre-specified in the source code. Any idea where to start on getting this accomplished and any good links that will explain the process? I am using java SE, if that makes a difference.

Here is the code with the pre-specified file name and location: (just for informational purposes)

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then you want a JFileChooser object. If you look carefully you'll see I just posted a link to the API documentation. Go there and follow the link to the tutorial about how to use file choosers.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

Please learn to use the 'code' tags to preserve your spacing - it makes your code MUCH easier to read. just click the button above where you enter your post, and paste your code between.

As to your question... that's really not the java way. by "connecting...to windows vista", you are locking yourself into a specific OS - what if you want to move your application to a Mac? Or Unix?

You really want to look at something like Java's Swing, which lets you build GUIs that run on ANY OS - but this is not a trivial thing to do.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Actually the dialog displayed by JFileChooser is a pretty good replica of the native file chooser dialog, at least it is in Windows from my experience.

And the code posted already has Swing-y things like ActionListener in it, so I was assuming it was already a Swing application.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Why are you testing the location of the source? That is tying your class to a particular implementation, which is usually a bad idea.

This looks to difficult for a "beginning" question, so I shall move it.
Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
Campbell,

Well, if you must insist, the reason that I am testing the location of the source by if (e.getSource == saveButton) is simply out of habit. You see, I have arrays of JButtons that are connected to 1 or 2 other handlers further up in the code so I have to test for which particular JButton within an array of JButtons.

So really, as you pointed out, I do not need to test for the source in this save handler, and I will probably end up taking it out.

While we are on the subject, you mention that by testing for the source I am tying the class to a particular implementation. (which I am assuming you mean that I am creating a whole class for one button)
What other reasons make testing for the source a bad idea? I am curious because I am a beginner.

Fred,

I did use the <code> tags for my code, or at least I thought I did. Either way, the code appears indented now. Was it not before? Additionally, you are probably right about tying the code to the vista operating system. Doing so would deny cross OS compatibility, which Java was created for, to my knowledge.
However, I posted this question before I found that the JFileChooser class existed. What a pleasant surprise it was to find that class! Even if I did not find it on my own, you annd everyone else pointed me in the right direction anyway, so "thank you" to you all.

Now, I have another question. I know that we can wrap a FileWriter in a BufferedWriter, but I want to control the way that my strings are written to the .txt file. Is there any way I can wrap a formatter in a bufferedwriter? or is that not possible? I would love to be able to do this:

<code>
BufferedWriter bw = new BufferedWriter(new Formatter(file));

for (int i = 0; i < someNumber; i++)
{
bw.format("%-20.25s",labels[i].getText());
}
</code>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Timothy Hoyle wrote:Fred,

I did use the <code> tags for my code, or at least I thought I did. Either way, the code appears indented now. Was it not before?
There aren't any <code> tags on this forum. Just look at your own post and you'll see what they do (nothing). In future just click on the "Code" button above the posting box, it will generate the right tags. Try it and see.
Now, I have another question. I know that we can wrap a FileWriter in a BufferedWriter, but I want to control the way that my strings are written to the .txt file. Is there any way I can wrap a formatter in a bufferedwriter? or is that not possible? I would love to be able to do this:

<code>
BufferedWriter bw = new BufferedWriter(new Formatter(file));

for (int i = 0; i < someNumber; i++)
{
bw.format("%-20.25s",labels[i].getText());
}
</code>
Yes, but do it the other way around, more like this:
Timothy Hoyle
Greenhorn

Joined: Oct 12, 2009
Posts: 18
On related note, I noticed that java's Formatter class has a Flush() method. What does a Flush() method do?

I am just learning streams and my best guess is that flush somehow cleans out the stream you just used? But what does it clean out?
Philippe Lhoste
Greenhorn

Joined: Oct 23, 2009
Posts: 28
How come this thread is on the JavaFX forum? I see no JavaFX code...
(BTW, how come this forum is written "Java FX"? :hunf

Anyway, you guessed right (although that's flush(), not Flush()!). Formatter implements the Flushable interface which precisely requests to implement the flush() method and is succinctly described as "[...] a destination of data that can be flushed. The flush method is invoked to write any buffered output to the underlying stream."
Formatter can use a buffered output which, as the name implies, can store part of the output in memory (in a buffer) until the buffer is full (then it is flushed, written to disk and emptied) or until the stream is closed. If you have neither (half empty buffer and you don't want to close the stream right now), you can flush the output to be sure it is written to disk.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting my application to windows vista save dialog
 
Similar Threads
How to input End of File indicator
I want to merge the two files into one of kind: by placing englishKeyword=English in format Russian
Aligning columns in a text file with Java Formatter()
end of file indicator for more than one file
Writing to files using socket server