Re: need advice using swing jfilechooser
On Oct 28, 8:16 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
You didn't say what you wanted your program to do and the two
JFileChoosers didn't make a lot of sense. They are designed to allow
you to move through the directories and select the file you want. So
here is a simple program to select, load and display an image file.
There are a lot of ways to go about this but this is pretty simple and
covers a lot of things that can prove to be problematic when you first
try them. In this example if you don't select a file, nothing happens.
If you successfully pick one then it will be displayed on the JPanel.
If there is an error, a dialog is displayed with the error informatio=
n.
thank you for the reply..
The reason why I wanted the 2 filechoosers is because I wanted the
user to select a file and at the same time select another directory
..I am taking two strings as user input ie,name of an image file and
name of another directory so that I can try to find if a matching
image exists in that directory.I thought putting two filechoosers
would be simpler and easier for users.
I solved this error message display in a convoluted manner :-)
class Message{
private StringBuffer msg;
public Message(String msg){
this.msg=new StringBuffer(msg);
}
public Message(){
this.msg=new StringBuffer();
}
public void add(String msg){
this.msg.append(msg);
}
public String toString(){
return this.msg.toString();
}
}
then in MyView create an empty message
public MyView(MyModel model){
super("top frame");
model=model;
message=new Message();
....
}
public String getSelectedFile(){
String selectedfilename="";
java.io.File selectedFile=dirchooser.getSelectedFile();
if (selectedFolder==null){
message.add("you must select a file..");
resultfield.setText(message.toString());
}
...
return selectedfilename;
}
public void clearMessage(){
message=new Message();
}
Clicking OK button will first clear the message.
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
view.clearMessage();
...
Thanks for the filechooser demo code ..will go through this and
learn..Any more advices,pointers most welcome..
regards
jim