2

I currently have a JFileChooser in my view class, which I want to trigger certain events stored in my controller class. I add ActionListeners to JButtons like the following:

view.setUploadButtonListener(new UploadButtonListener());

where view is my view class and UploadButtonListener is an internal class of my controller class. The same way doesn't seem to work for JFileChooser (at least I can't figure it out).

How can I add an ActionListener to my JFileChooser that fires when the user clicks OK. And can I pass the selected file path as argument in the listener as well?

rebeliagamer
  • 1,257
  • 17
  • 33
Iwan Cuche
  • 45
  • 5

3 Answers3

2

As shown here, you can use the chooser's addActionListener() method. The example implements several listeners, including ActionListener. For additional flexibility consider substituting a suitable Action.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I recommend: "How to Use File Choosers"

JFileCooser showOpenDialog method pops up an Open File file chooser dialog and returns value of the state of the file chooser on popdown:

  • JFileChooser.CANCEL_OPTION
  • JFileChooser.APPROVE_OPTION
  • JFileCHooser.ERROR_OPTION

So this is the code for your ActionListener that will open File Chooser and react on demand:

public void actionPerformed(ActionEvent e) {
    if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        String name = file.getName();
        ...
    }
}

Read more about showOpenDialog here: showOpenDialog - doc

rebeliagamer
  • 1,257
  • 17
  • 33
1

To have your view logic in view and controller logic in controller I would go for something like that:

class UploadButtonListener implements ActionListener{
    private View view;
    public UploadButtonListener(View view){
        this.view = view;
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        String filename = view.getFileName();
        if(!filename.isEmpty()){
                    ... your logic
            }
    }

}

then add to your view class following method:

public String getFileName(){
   JFileChooser fc = new JFileChooser();
   if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
     return fc.getSelectedFile().getName();
   else
     return "";
}
Luke
  • 1,236
  • 2
  • 11
  • 16
  • Thanks for the answer, it works so far. Could you explain me what the `AddCheckBoxAction` is triggered by? I am confused by the name as there are no checkboxes in my gui. – Iwan Cuche Oct 18 '13 at 08:27
  • Sorry for confusion I was doing copy paste from my older project and I forgot to change the name of constructor :) I renamed it to UploadButtonListener now. It is a constructor of course and in your View class you can simply do the following: JButton btn = new JButton("btn"); btn.addActionListener( new UploadButtonListener(this) ); or JMenuItem menuItem = new JMenuItem("Upload File"); menuItem.addActionListener( new UploadButtonListener(this) ); Is it clear now ? – Luke Oct 18 '13 at 11:05