-1

i'm doing a class that does a sort of "file explorer", in the constructor i create the frame ,panel ecc.. but than i want to say to the main program that calls this class that the user has finish the selection, i know i can call a static method that is in the main from this class,but i want to make a action listener because i want to use this class for different programs

For Example if FileEx is my class:

public class FileEx()
{


public FileEx()
{
    //program that do something 
    if(done == true)
       //here i want to call the action 
} 

public void addActionListener(ActionListener ac) //i don't know if it's correct 
                                                 //but i want something like this
{

}



}

public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){

       @Override
        public void actionPerformed(ActionEvent e)
        {
              //when done is true i want this block of code to be called
        }
});

}
Marco Ortali
  • 73
  • 1
  • 10
  • Just create a field with type `List` in `FileEx` and call for all listener in that list the `actionPerformed` method on each event you want to support. And do not forget to add the `addActionListener` method witch adds the listener to the list. – Gábor Bakos Nov 19 '14 at 17:08
  • Hey, it looks is what i want, can you please give me an example? would be great! – Marco Ortali Nov 19 '14 at 17:31
  • No, based on your above code you do not want an ActionListener since you're not in fact listening for actions. Again, go with a PropertyChangeListener and PropertyChangeSupport. – Hovercraft Full Of Eels Nov 19 '14 at 17:39

2 Answers2

3

ActionListeners will only work when added to components that allow them to be added and that notify listeners with them such as JButtons, JMenuItems, JComboBoxes and such. We have no idea what type of class FileEx is or why it should accept an ActionListener and a little more information would be qutie helpful. If you want to notify another object that an event occurs, such as that a calculation is done, use another type of listener such as a PropertyChangeListener. Alternatively you could do the processing in a modal JDialog window, which will notify the calling window that it is done performing its duties by returning code flow to the calling window.


For example, please look at my answers to similar questions:


EDIT
For example, if you wanted your FileEx to allow other classes to listen for changes to a String called selection (the so-called "bound" property) you could create it to look something like:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class TestFileEx {
   public static void main(String[] args) {
      final FileEx fileEx = new FileEx();

      fileEx.addPropertyChangeListener(FileEx.SELECTION, new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            // TODO code to call when fileEx has changed selections

            String fileExSelection = evt.getNewValue().toString();

            // or

            String fileExSelection2 = fileEx.getSelection();            

         }
      });
   }
}

and

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.event.SwingPropertyChangeSupport;

publicclass FileEx {
   public static final String SELECTION = "selection";
   private SwingPropertyChangeSupport propertyChangeSupport = new SwingPropertyChangeSupport(
         this);
   private String selection;

   public void someMethodThatChangesSelection() {

   }



   public String getSelection() {
      return selection;
   }

   public void setSelection(String selection) {
      String oldValue = this.selection;
      String newValue = selection;
      this.selection = selection;
      // notify the listeners of change
      propertyChangeSupport.firePropertyChange(SELECTION, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      propertyChangeSupport.addPropertyChangeListener(listener);
   }

   public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
      propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      propertyChangeSupport.removePropertyChangeListener(listener);
   }

   public void rem(String propertyName, PropertyChangeListener listener) {
      propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
   }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @ABFORCE I's sure that HFOE [answering your question](http://stackoverflow.com/q/27019855/714968) – mKorbel Nov 19 '14 at 18:25
  • Thanks! sure this is correct and work but the other answer is more simple and fast to do and does exactly what i was searching ! – Marco Ortali Nov 19 '14 at 20:25
  • @MarkWalter: and the other answer is wrong as you should not be using an ActionListener for this sort of thing. But it's your code. – Hovercraft Full Of Eels Nov 19 '14 at 20:31
  • @MarkWalter: regardless, best of luck with your project! – Hovercraft Full Of Eels Nov 19 '14 at 23:17
  • 1
    1+ The right approach often takes more time at the beginning but save you a lot of time in the future @MarkWalter. However if you insist in the other approach then consider use [EventListenerList](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/EventListenerList.html) instead of a plain `ArrayList` to hold the action listeners. – dic19 Nov 19 '14 at 23:48
0

Here is the code based on your example which adds actionlisteners and calls them:

public class FileEx()
{
   private final List<ActionListener> listeners = new ArrayList<>();

public FileEx()
{
    //program that do something 
    if(done == true) {
        notifyListeners();
    }
} 

public void addActionListener(ActionListener ac)
{
    listeners.add(ac);
}

private void notifyListeners()
{
    for (final ActionListener listener: listeners)
    {
        listener.actionPerformed(null);//You can create event if you want.
    }
}



}

public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){

       @Override
        public void actionPerformed(ActionEvent e)
        {
              //when done is true i want this block of code to be called
        }
});

}
Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52