Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting my application to windows vista save dialog

 
Timothy Hoyle
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49783
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic