Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > need advice using swing jfilechooser

Reply
Thread Tools

need advice using swing jfilechooser

 
 
jimgardener
Guest
Posts: n/a
 
      10-27-2010
hi
I created a gui app in which I have two jfilechooser widgets ,one to
select a file and other to select a different directory.I want the
selected filename and selected directory name to be displayed in a
textarea.If no selection is made ,i want to display an error message
instead.
I coded like this

class MyView extends JFrame {
private JFileChooser filechooser;
private JFileChooser dirchooser;
private JTextArea resultfield;
...//and many jpanels to contain these widgets above
//and ok,quit buttons
public MyView(MyModel model){
super("top frame");
model=model;
createAndAddAllWidgets();
}
public void createAndAddAllWidgets(){
...
filechooser=new JFileChooser("Select imagefile");
...
dirchooser=new JFileChooser();
dirchooser.setFileSelectionMode(JFileChooser.DIREC TORIES_ONLY);
...
//similarly adds textarea and needed ok, quit buttons
}
public String getSelectedFile(){
//?
}
public String getSelectedFolder(){
//?
}
public void displayMessage(String msg){
resultfield.setText(msg);
}
public void addOKButtonListener(ActionListener okl){
okbtn.addActionListener(okl);
}
public void addQuitButtonListener(ActionListener qbl){
quitbtn.addActionListener(qbl);
}

}//end of MyView class

class MyController{
private MyModel model;
private MyView view;
public MyController(MyModel m,MyView v ){
model=m;
view=v;
view.addOKButtonListener(new OKButtonListener());
view.addQuitButtonListener(new QuitButtonListener());
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String fileselection=view.getSelectedFile();
String folderselection=view.getSelectedFolder();

String result=model.processSelections(fileselection,
folderselection);
view.displayResult(result);
}
}//end inner class
class QuitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
view.dispose();
}
}//end inner class
}//end of MyController class

class MyModel{
public String processSelections(String filename,String foldername){
String result="you selected file:"+filename+" you selected
folder:"+foldername;
return result;
}
}//end of MyModel class

What I couldn't figure out was how to code the logic in MyView's
getSelectedFile(),getSelectedFolder() methods..
I can get the selected file name string as ,

filechooser.getSelectedFile().getPath() and
the selected folder name as
dirchooser.getSelectedFile().getPath()

But,how should I deal with the situation when no file is selected or
no folder is selected?Should I return an empty string from the method ?
How can I display an error message if both happen at the same time?
I need to display the result from MyModel's processSelections() if
selections are made.I am not sure how I should do this.Should I define
a new Exception for these empty selection cases? or should I create
StringBuffer and append messages into it, and use it when I call
MyView's displayMessage() ?
If anyone can help me here ,it would be nice.
thanks,
jim







 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      10-28-2010
On 10/27/2010 10:06 AM, jimgardener wrote:
> hi
> I created a gui app in which I have two jfilechooser widgets ,one to
> select a file and other to select a different directory.I want the
> selected filename and selected directory name to be displayed in a
> textarea.If no selection is made ,i want to display an error message
> instead.
> I coded like this
>
> class MyView extends JFrame {
> private JFileChooser filechooser;
> private JFileChooser dirchooser;
> private JTextArea resultfield;
> ...//and many jpanels to contain these widgets above
> //and ok,quit buttons
> public MyView(MyModel model){
> super("top frame");
> model=model;
> createAndAddAllWidgets();
> }
> public void createAndAddAllWidgets(){
> ...
> filechooser=new JFileChooser("Select imagefile");
> ...
> dirchooser=new JFileChooser();
> dirchooser.setFileSelectionMode(JFileChooser.DIREC TORIES_ONLY);
> ...
> //similarly adds textarea and needed ok, quit buttons
> }
> public String getSelectedFile(){
> //?
> }
> public String getSelectedFolder(){
> //?
> }
> public void displayMessage(String msg){
> resultfield.setText(msg);
> }
> public void addOKButtonListener(ActionListener okl){
> okbtn.addActionListener(okl);
> }
> public void addQuitButtonListener(ActionListener qbl){
> quitbtn.addActionListener(qbl);
> }
>
> }//end of MyView class
>
> class MyController{
> private MyModel model;
> private MyView view;
> public MyController(MyModel m,MyView v ){
> model=m;
> view=v;
> view.addOKButtonListener(new OKButtonListener());
> view.addQuitButtonListener(new QuitButtonListener());
> }
> class OKButtonListener implements ActionListener {
> public void actionPerformed(ActionEvent e) {
> String fileselection=view.getSelectedFile();
> String folderselection=view.getSelectedFolder();
>
> String result=model.processSelections(fileselection,
> folderselection);
> view.displayResult(result);
> }
> }//end inner class
> class QuitButtonListener implements ActionListener {
> public void actionPerformed(ActionEvent e) {
> view.dispose();
> }
> }//end inner class
> }//end of MyController class
>
> class MyModel{
> public String processSelections(String filename,String foldername){
> String result="you selected file:"+filename+" you selected
> folder:"+foldername;
> return result;
> }
> }//end of MyModel class
>
> What I couldn't figure out was how to code the logic in MyView's
> getSelectedFile(),getSelectedFolder() methods..
> I can get the selected file name string as ,
>
> filechooser.getSelectedFile().getPath() and
> the selected folder name as
> dirchooser.getSelectedFile().getPath()
>
> But,how should I deal with the situation when no file is selected or
> no folder is selected?Should I return an empty string from the method ?
> How can I display an error message if both happen at the same time?
> I need to display the result from MyModel's processSelections() if
> selections are made.I am not sure how I should do this.Should I define
> a new Exception for these empty selection cases? or should I create
> StringBuffer and append messages into it, and use it when I call
> MyView's displayMessage() ?
> If anyone can help me here ,it would be nice.
> thanks,
> jim
>


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 information.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class test extends JPanel {
// create this now so there won't be a delay on first opening
private final JFileChooser fc = new JFileChooser();
private final AbstractAction loadAction;

private BufferedImage bi;

public test() {
// set a default size for the JPanel
setPreferredSize(new Dimension(640,480));
// put a title on the JFileChooser dialog
fc.setDialogTitle("Select Image File");
// set a filefilter to show only directories and image files
fc.addChoosableFileFilter(new FileNameExtensionFilter(
"Image Files","jpg","jpeg","gif","png"));
// create the action for the load button
loadAction = new LoadAction();
}

public void paintComponent(Graphics g) {
// clear the background
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
// draw the image
g.drawImage(bi,0,0,null);
}

public class LoadAction extends AbstractAction {
public LoadAction() {
// label for the button
putValue(NAME,"Load Image");
}

public void actionPerformed(final ActionEvent ae) {
// show the file chooser dialog
int option = fc.showOpenDialog(test.this);

if (option == JFileChooser.CANCEL_OPTION) {
// nothing to do here
} else if (option == JFileChooser.APPROVE_OPTION) {
try {
// read the image file
bi = ImageIO.read(fc.getSelectedFile());
// resize the JPanel to the image
test.this.setPreferredSize(new Dimension(
bi.getWidth(),bi.getHeight()));
// cause it to be re-layed out
test.this.revalidate();
// repaint it
repaint();
} catch (IOException ioe) {
// display the io exception in a dialog
JOptionPane.showMessageDialog(test.this,ioe,
"ERROR!",JOptionPane.ERROR_MESSAGE);
}
} else if (option == JFileChooser.ERROR_OPTION) {
// display an error dialog
JOptionPane.showMessageDialog(test.this,
"ERROR READING IMAGE FILE","ERROR!",
JOptionPane.ERROR_MESSAGE);
}
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// create the frame
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the panel to show the image
test t = new test();
// wrap in a JScrollPane and add to frame
f.add(new JScrollPane(t),BorderLayout.CENTER);
// put the button on the frame
f.add(new JButton(t.loadAction),BorderLayout.SOUTH);
// display the frame
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2010/

 
Reply With Quote
 
 
 
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 8:16*am, Knute Johnson <(E-Mail Removed)>
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 information.
>


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
 
Reply With Quote
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 8:16*am, Knute Johnson <(E-Mail Removed)>
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 information.
>


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
 
Reply With Quote
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 8:16*am, Knute Johnson <(E-Mail Removed)>
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 information.
>


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
 
Reply With Quote
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 8:16*am, Knute Johnson <(E-Mail Removed)>
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 information.
>


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
 
Reply With Quote
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
On Oct 28, 8:16*am, Knute Johnson <(E-Mail Removed)>
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 information.
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JFileChooser - File Filter - Need help getting rid of "All Files" option nbuserx@netbeans.org Java 5 06-03-2009 07:02 PM
Need to modify JFileChooser's default behavior Ramon F Herrera Java 3 11-13-2007 04:58 AM
Why not using javax.swing.event with swing? S.T Java 2 05-25-2007 12:10 AM
Is this a bug in javax.swing.JFileChooser? RC Java 6 05-18-2007 06:40 AM
addChoosableFileFilter in javax.swing.JFileChooser cannot be applied to zipfilter error Darren Java 11 10-27-2005 08:37 AM



Advertisments