Re: Obtain file path and file name using file chooser
- From: "Steve W. Jackson" <stevewjackson@xxxxxxxxxxx>
- Date: Tue, 28 Nov 2006 15:29:59 -0600
In article <1164743756.733743.114700@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"bH" <bherbst65@xxxxxxxxxxx> wrote:
Andrew Thompson wrote:
bH wrote:
Hi All,
I want to obtain both file "path" and the filenamed MyText.txt ....as
in "C:\Documents
and Settings\bH\Desktop\MyShowWithFrame\MyText.txt"
I have tried to discover the options shown in How to Use File Choosers
(The Java? Tutorials Creating a GUI with JFC-Swing Using Swing
Components).htm
I cannot understand what options should be shown as but this does not
work.
Is it lazy? Or to put that another way 'it doesn't work'
tells us very little. Please describe in detail, what you
see on-screen, what happened off-screen (with debug
statements and such) and what you expected to happen.
However - taking a wild stab..
Program snip:
JButton opnButton = new JButton("Open a Text File...");
buttonPanel.add(opnButton);
opnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile ();
strngFileName = file.getName();
..if I guess correctly, you might try something like
strngFileName = file.getPath() +
System.getProperty("file.separator") +
file.getName();
Having said that, it seems strange you are dealing with
String's, when you have a File object (which is an object
that is already one step closer to getting an input/output
stream, than a String that represents a File).
Andrew T.
Hi Andrew,
Your guess was correct and your advice helpful. Now I am in another
difficulty.
Because I am connecting with a printer after locating a file address
when I am using the file chooser, all the file separators are "\" but
should be "/". I have tested a method: one that looks at the individual
char in the line but have not been able to represent the "\" because it
is an escape character.
A sample of the output from the chooser:
"C:\Documents and Settings\bH\Desktop\MyPrinter
WithFrame\MyText.txt\MyText.txt"
Needs to be:
"C:/Documents and Settings/bH\Desktop/MyPrinter
WithFrame/MyText.txt/MyText.txt"
TIA.
bH
It's not clear what "connecting with a printer" has to do with anything.
Printing in Java doesn't involve files. It involves the Printable and
Pageable interfaces and sending a "picture" to the printer via
appropriate APIs.
But if you're seeking a way to refer to a File object after it's been
selected using a JFileChooser, you should look into the java.io.File API
a bit more closely. It includes a couple of possible useful methods,
which are toURL and toURI. You might be better off using the latter.
And doing so will result in a URI that encodes any spaces or other
"special" characters, as well as using "real" slashes instead of those
horrid backslash things Microsoft hoisted upon you.
= Steve =
--
Steve W. Jackson
Montgomery, Alabama
.
- References:
- Obtain file path and file name using file chooser
- From: bH
- Re: Obtain file path and file name using file chooser
- From: Andrew Thompson
- Re: Obtain file path and file name using file chooser
- From: bH
- Obtain file path and file name using file chooser
- Prev by Date: http://javaservice.objectweb.org/
- Next by Date: Re: Obtain file path and file name using file chooser
- Previous by thread: Re: Obtain file path and file name using file chooser
- Next by thread: Re: Obtain file path and file name using file chooser
- Index(es):