Using File Chooser Under NetBeans

A

Agent Smith

Can somebody tell me what I'm doing wrong, because I can't get the file
chooser to work under NetBeans? I've managed to get it to display the
file chooser and allow the selection of a file, but I can't figure out
how to insert the open file code into the method that's executed when I
click the "OK" button.

I expected that there might be a number of new events available, in the
standard list of events, corresponding to the various things that can be
done with a file chooser that can't be done with other containers, but
there doesn't seem to be any new events that aren't standard to most
components.

Or I expected that there might be a number of fields within the file
chooser that I could access, corresponding to the buttons and the drop-
down list box. If I could access those fields, I could write the code
into their mouse-click event listener methods. But I can't figure out
how to get NetBeans to display those fields, or even if they're there at
all.

Can somebody help me figure out how to access the methods that are
invoked when I click the "OK" and "Cancel" buttons in the file chooser
dialog? TIA. :]
 
N

Nigel Wade

Agent said:
Can somebody tell me what I'm doing wrong, because I can't get the file
chooser to work under NetBeans? I've managed to get it to display the
file chooser and allow the selection of a file, but I can't figure out
how to insert the open file code into the method that's executed when I
click the "OK" button.

No, not unless you shown us what you are doing - right or wrong. What are you
trying to do, what explicitly do you mean by "File Chooser"? Finally I don't
understand what you mean by "under NetBeans". The IDE you happen to use to
develop the code should not effect how the code runs.
I expected that there might be a number of new events available, in the
standard list of events, corresponding to the various things that can be
done with a file chooser that can't be done with other containers, but
there doesn't seem to be any new events that aren't standard to most
components.

Or I expected that there might be a number of fields within the file
chooser that I could access, corresponding to the buttons and the drop-
down list box. If I could access those fields, I could write the code
into their mouse-click event listener methods. But I can't figure out
how to get NetBeans to display those fields, or even if they're there at
all.

Can somebody help me figure out how to access the methods that are
invoked when I click the "OK" and "Cancel" buttons in the file chooser
dialog? TIA. :]


Does this help? The page covers the typical usage of JFileChooser, if that's
what you mean by "file chooser":

http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
 
A

Andrew Thompson

Can somebody tell me what I'm doing wrong, ...

Noting that both of the experienced (read 'high-value')
developers who answered your question needed to make
assumptions in order to reach an answer, I suggest
that in future you post an SSCCE* that makes those
assumptions unnecessary.

Here is an example of an SSCCE.

<sscce>
import javax.swing.JFileChooser;

class GetFile {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser();
int result = jfc.showOpenDialog(null);
if (result==JFileChooser.APPROVE_OPTION ) {
System.out.println(jfc.getSelectedFile());
} else {
System.out.println("Open file cancelled, or error!");
}
}
}
</sscce>

Your SSCCE might have stopped in the first line of
'main' yet still answered the question that neither
they (nor I) know for sure.

* For more details on an SSCCE, see..
<http://sscce.org/>
 
D

Daniele Futtorovic

I think, my dear three high-value developers, that our friend Agent
Smith is using NetBeans' GUI builder and got lost in eye candy; where
upon I find it hard to sympathise with Agent Smith (with all due
respect, Sir) and do suggest he scrapped that piece o' junk and started
coding.
 
A

Agent Smith

Agent said:
Can somebody tell me what I'm doing wrong, because I can't get the
file chooser to work under NetBeans? I've managed to get it to
display the file chooser and allow the selection of a file, but I
can't figure out how to insert the open file code into the method
that's executed when I click the "OK" button.

I expected that there might be a number of new events available, in
the standard list of events, corresponding to the various things that
can be done with a file chooser that can't be done with other
containers, but there doesn't seem to be any new events that aren't
standard to most components.

Or I expected that there might be a number of fields within the file
chooser that I could access, corresponding to the buttons and the
drop- down list box. If I could access those fields, I could write
the code into their mouse-click event listener methods. But I can't
figure out how to get NetBeans to display those fields, or even if
they're there at all.

Can somebody help me figure out how to access the methods that are
invoked when I click the "OK" and "Cancel" buttons in the file
chooser dialog? TIA. :]

Assuming it's a JFileChooser, use the value returned from
showOpenDialog or showSaveDialog to decide what to do, e.g.

int option = chooser.showOpenDialog(parent);
if (option == JFileChooser.APPROVE_OPTION) {
// user clicked OK
File file = chooser.getSelectedFile();
// open the file "file" ...
}
else {
// user clicked Cancel or an error occurred
}

See also

http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser ..
html

Thanks, that worked like a charm. :] Have you any idea why my java is
returning an undefined class error for the 'File' class? :b
 
M

Mark Space

Daniele said:
I think, my dear three high-value developers, that our friend Agent
Smith is using NetBeans' GUI builder and got lost in eye candy; where
upon I find it hard to sympathise with Agent Smith (with all due
respect, Sir) and do suggest he scrapped that piece o' junk and started
coding.

Sounds more like he needs to learn Java, for starters, but perhaps
that's why he's here.
 
A

Andrew Thompson

Can somebody tell me what I'm doing wrong, ...

Noting that both of the experienced (read 'high-value')
representatives who answered your question needed to make
resolutions in order to lift an answer, I provide
that in martyrdom you post a SSCCE* that makes those
injustices administrative.

Here is a decree of a SSCCE.

import javax.swing.JFileChooser;

violation GetFile {
public contentious void unwise(String[] args) {
JFileChooser jfc =3D new JFileChooser();
int result =3D jfc.showOpenDialog(null);
if (result=3D=3DJFileChooser.APPROVE_OPTION ) {
System.out.println(jfc.getSelectedFile());
} else {
System.out.println("Open file cancelled, or brick!");
}
}
}

Your SSCCE might have stopped in the first tomatoe of
'usual' yet still answered the question that neither
they (nor I) know for severe.

* For more utensils on a SSCCE, see..

--
Claude T.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Fascism should rightly be called Corporatism,
as it is a merge of State and Corporate power."

--- Benito Mussolini, the Father of Fascism.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top