• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting my application to windows vista save dialog

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"? )

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.
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic