JFileChooser Actionlistener
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: JFileChooser Actionlistener

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    JFileChooser Actionlistener

    Hello,

    While programming in Java i ran into a problem concerning a JFileChooser and it's actionListener.

    The problem is, that my Actionlistener only seems to listen to the events made by my JFileChoosers 'close' button.

    As you can see in my actionlistener im printing a line which should display "Display Test".

    But this only happens when i click the close button, and not when i use the open button.

    I would appriciate any help you can give me.

    //Java Code FileChooser.java
    Code:
    package View;
    
    import Controller.CFileChooser;
    import java.awt.Container;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    
    
    public class FileChooser
    			extends		JFrame
    			implements	ActionListener
    {
    	// GUI
        private JFileChooser FSFileChooser;
    
    	// Instantievariabele
            private CFileChooser _controller;
    	public FileChooser(CFileChooser controller)
    	{
            super();
            _controller = controller;
    		// Geef het frame een titel en zet de afmetingen
    		setTitle("Pick your *.leg File!");
    		setBounds(320,20,500,400);
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    		Container pane = getContentPane();
    
    		pane.setLayout(null);
    
    	FSFileChooser = new JFileChooser();
            FSFileChooser.addActionListener(this);
            FSFileChooser.setBounds(0,0,500,370);
            FSFileChooser.setVisible(true);
            FSFileChooser.setMultiSelectionEnabled(false);
            pane.add(FSFileChooser);
    	}
    
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Display Test");
            //String command = e.getActionCommand();
            //if (command.equals(JFileChooser.CANCEL_SELECTION)) {
            //  System.out.println(JFileChooser.CANCEL_SELECTION);
            //}
        }
    }
    //End Java Code

    Greetings Dennis
    Last edited by Tyranox; January 13th, 2009 at 08:57 AM.

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: JFileChooser Actionlistener

    Use code tags, please.

    JFileChooser is a dialog. Therefore, it returns its results as an int value, as like in the following

    Code:
    JFileChooser fc = new JFileChooser();
    int returnval = fc.showOpenDialog();
    if (returnval == JFileChooser.APPROVE_OPTION) {
     //user selected a file
    }
    //do other options if needed

  3. #3
    Join Date
    Jan 2009
    Posts
    3

    Re: JFileChooser Actionlistener

    I seem to have found the problem.

    It was stupid realy.

    My Code seemed to be correct, the only reason it did not work, is that i did not have a file selected.

    so the Open button in JFileChooser wont work if you dont have a file selected.

    Greetings, Tyranox

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JFileChooser Actionlistener

    Quote Originally Posted by Tyranox View Post
    so the Open button in JFileChooser wont work if you dont have a file selected.
    Seems reasonable - how could it open a non-existent file?

    A designer knows he's achieved perfection not when there is nothing left to add, but when there is nothing left to take away...
    A. de Saint-Exupery
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center