actionPerformed executed twice


I have a class named Controladora derived from JDialog which implements windowListener for acting when button into a toolbar is pressed, using:

public void actionPerformed(ActionEvent e) {
    <instructions to be executed>

public void ....()
   myBtn1 = new JButton("...");

   myBtn2 = new JButton("...");


and I have another class named PanelGeneral derived from Controladora which implements windowListener too. so it has a method:

public void actionPerformed(ActionEvent e) {

When I create a Dialog I use PanelGeneral as my default class due to PanelGeneral executes super constructor for creating Controladora, so when toolbar button is pressed panelGeneral.actionPerformed is executed. My Goal with events in this place is be able insert new code for a specific situation, besides super.actionPerformed(e) makes Controladora.actionPerformed(e) be executed as a default. But my problem is when I press a button on toolbar instructions on PanelGeneral.actionPerformed(e) and also for Controladora.actionPerformed(e) are executed twice. I noted BUttonModel executes something like setPressed(booelan) function which triggers event, and I don't know if it is true, maybe setPressed is executed twice for pressed and unpressed state. I need this:

PanelGeneral.actionPerformed(e) be executed once when I press a button and this event be in charge of execution of super.actionPerformed

Who is Participating?
Question Closed, 20 points refunded.
Community Support Moderator
Delete the 'super.actionPerformed(e);'

Another advice: don't :                 'myBtn2.addActionCommand("2");'

write the actionperformed like this:

    public void actionPerformed(ActionEvent a)
   Object o = a.getSource();

      if(o ==  myBtn1)            // Button 1
      else if(o ==  myBtn2)            // Button 2

Why? One typo in one of the command Strings and you will not understand why some button sometimes doesn't work.

accarvajalAuthor Commented:
For your first Advice:

I can't delete actionPerformed on super (Controladora) due to is done so, because is the default execution to be runned. So I extended controladora inheriting it on PanelGeneral, because Controladora is like a kernell that has to execute statements and they will never be modified. So PanelGeneral construct Controladora and this one executes a set of statements are common for many Dialogs, like toolbar Functionality. I left actionPerformed on PanelGeneral because this method is requiered to be executed first and I call super.actionPerformed in order to common statements be executed first, and whenever be necessary, I can insert more statements that apply just for a specific dialog, referring to a button on PanelGeneral
accarvajalAuthor Commented:
I found when problem happens!

When I create a JDialog for first time, I can use Toolbar buttons with no problems, actionPerformed is executed normally, just once, and then I press close button on JDialog. When I execute JDialog for second time, Begins problems, Toolbar buttons actionPerformed are executed twice, what does it happen?, first JDialog (created using new) keeps in memory?

plase help!, this is a short cut of source code,
main class:

package FrameWork;

public class App
ActionListener evMenu = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            JMenuItem mnuItem = (JMenuItem)evt.getSource();

      public Object toolBarActualizacion[][] = {
                     {new JButton(), new String("*imgRegistroInicial.gif"), new String("Tip_Inicio")},
                     {new JButton(), new String("*imgRegistroAnterior.gif"), new String("Tip_Anterior")},
                     {new JButton(), new String("*imgRegistroSiguiente.gif"), new String("Tip_Siguiente")},
                     {new JButton(), new String("*imgRegistroFinal.gif"), new String("Tip_Final")},
                     {new JButton(), new String("*imgRegistroNuevo.gif"), new String("Tip_Nuevo")},
                     {new JButton(), new String("*imgRegistroActualiza.gif"), new String("Tip_Actualiza")},
                     {new JButton(), new String("*imgRegistroRetira.gif"), new String("Tip_Retira")},
                     {new JButton(), new String("*imgRegistroRecargaListas.gif"), new String("Tip_Recarga_Listas")},
                     {new JButton(), new String("*imgRegistroRestaura.gif"), new String("Tip_Restaura_Datos")}

      public Object toolBarConsulta[][] = {
                     {new JButton(), new String("*imgGeneraInforme.gif"), new String("Tip_Genera_Informe")},
                     {new JButton(), new String("*imgGeneradorReportes.gif"), new String("Tip_Generador_Reportes")},
                     {new JButton(), new String("*imgRecargaDatos.gif"), new String("Tip_Recarga_Datos")},
                     {new JButton(), new String("*imgBuscarDatos.gif"), new String("Tip_Buscar")},
                     {new JButton(), new String("*imgSQL.gif"), new String("Tip_Filtros")}

App() {
        m_frmMarcoPrincipal = new JFrame(Global.langString("Tit_Aplicacion"));
        m_frmMarcoPrincipal.setIconImage(new ImageIcon(Global.m_sDirectorioTrabajo + "/Rc/Imagenes/App.gif").getImage());
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        m_frmMarcoPrincipal.setSize(screenSize.width, screenSize.height - 30);

        JMenuBar mnuPrincipal = new JMenuBar();
        .... //-> instructions in order to insert menu items
        .... //-> instructions in order to insert action listeners (evMenu - created above)

public static void main(String[] args) {
          m_sSistemaOperativo = System.getProperty("");        
          m_sDirectorioTrabajo = UnificarRutas(System.getProperty("user.dir") + "\\");
          new App();

    public void EjecutarOpcion(String sOpcion) {
            m_dxyDimension = new Dimension(...,...);
            Vector<Vector<Object>> vpagPropiedades = new Vector<Vector<Object>>();
                Vector<Object> vPagina = new Vector<Object>();
            vPagina.addElement(new PanelGeneral(toolBarConsulta, 0));
            vPagina.addElement(new PanelGeneral(toolBarActualizacion, 1));

            new DialogoPropiedades(this, m_frmMarcoPrincipal,           sOpcion, m_dxyDimension, vpagPropiedades);    

other class:

package FrameWork;

import static FrameWork.Global.*;

public class DialogoPropiedades extends JDialog implements WindowListener  {
      DialogoPropiedades(App Parent, JFrame frmParent,                String title, Dimension dxyDimension, int iTabla, Vector<Vector<Object>> tabsAplicacion) throws HeadlessException {
            super(frmParent, title, true);

      private JTabbedPane CrearPaginas(Vector<Vector<Object>> tabsAplicacion) {
            JTabbedPane pnlPropiedades = new JTabbedPane();
            for (int i = 0; i < tabsAplicacion.size(); i++) {
                  Vector<Object> vAtributos = tabsAplicacion.get(i);
pnlPropiedades.addTab("titulo Dialogo", null,
            return pnlPropiedades;

      private void Init(Dimension dxyDimension) {

now, PanelGeneral class:

package FrameWork;

import FrameWork.Controladora;

public class PanelGeneral extends Controladora implements ActionListener {
      static final long serialVersionUID = 0;

    /** Constructor */
      public PanelGeneral(toolComponentes, int iPagina) {
            super(toolComponentes, iPagina);

    /**  Eventos    
     *   Orden: Publicos, Protegidos, Privados
    public void actionPerformed(ActionEvent e) {
          super.actionPerformed(e); // This method is executed twice, here where problem is

Here, Controladora (Like a kernell) class:
NOTE: TipoPagina, contains simply a integer value 0 or 1 that means, tab into JTabbedPane is for consulting or manipulating data, according to this create a toolbar for navigation and manipulation or for consulting data

package FrameWork;
import static FrameWork.Global.*;

public class Controladora extends JPanel implements ActionListener {
      public Controladora(Object[][] toolComponentes, int iPagina) {
            m_iTipoPagina = iPagina;
            if (toolComponentes == null) return;
            m_toolBar = new JToolBar();
            Dimension dm = new Dimension(toolComponentes.length * m_iAnchoBotonToolBar, m_iAlturaBotonToolBar);
            int x = 1;

            for (int i = 0; i < toolComponentes.length; i++, x+=m_iAnchoBotonToolBar) {
                  JButton btnCommando = (JButton)toolComponentes[i][0];
                  btnCommando.setLocation(x, 0);
                  Dimension dmBtn = new Dimension(new Dimension(m_iAnchoBotonToolBar, m_iAlturaBotonToolBar));
                  if (toolComponentes[i][1] != null) {
                         String sImagen = (String)toolComponentes[i][1];
                         sImagen = sImagen.replace ("*",m_sDirectorioTrabajo + "Rc/Imagenes/");
                       btnCommando.setIcon(new ImageIcon(sImagen));
                  if (toolComponentes[i][2] != null)
                            btnCommando.setToolTipText (langString((String)toolComponentes[i][2]));


        public void actionPerformed(ActionEvent e) {
              // <this method is called from  PanelGeneral.actionPerformed(..)>
             <Global Instructions>

Controladora, is a General Class, PanelGeneral is a specialization class.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.