need advice using swing jfilechooser

Discussion in 'Java' started by jimgardener, Oct 27, 2010.

  1. jimgardener

    jimgardener Guest

    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.DIRECTORIES_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
    jimgardener, Oct 27, 2010
    #1
    1. Advertising

  2. 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.DIRECTORIES_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/
    Knute Johnson, Oct 28, 2010
    #2
    1. Advertising

  3. jimgardener

    jimgardener Guest

    On Oct 28, 8:16 am, Knute Johnson <>
    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
    jimgardener, Oct 28, 2010
    #3
  4. jimgardener

    jimgardener Guest

    On Oct 28, 8:16 am, Knute Johnson <>
    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
    jimgardener, Oct 28, 2010
    #4
  5. jimgardener

    jimgardener Guest

    On Oct 28, 8:16 am, Knute Johnson <>
    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
    jimgardener, Oct 28, 2010
    #5
  6. jimgardener

    jimgardener Guest

    On Oct 28, 8:16 am, Knute Johnson <>
    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
    jimgardener, Oct 28, 2010
    #6
  7. jimgardener

    jimgardener Guest

    On Oct 28, 8:16 am, Knute Johnson <>
    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
    jimgardener, Oct 28, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    10,965
    I82Much
    Jun 3, 2009
  2. Darren
    Replies:
    11
    Views:
    4,509
    Darren
    Oct 27, 2005
  3. RC
    Replies:
    6
    Views:
    706
    ..::WojT::..
    May 18, 2007
  4. S.T
    Replies:
    2
    Views:
    565
  5. Ramon F Herrera
    Replies:
    3
    Views:
    459
    Ramon F Herrera
    Nov 13, 2007
Loading...

Share This Page