actionPerformed executed twice

Posted on 2005-04-14
Last Modified: 2008-02-01

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

Question by:accarvajal
    LVL 24

    Expert Comment

    Delete the 'super.actionPerformed(e);'

    LVL 24

    Expert Comment

    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.


    Author Comment

    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

    Author Comment

    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.


    Accepted Solution

    Question Closed, 20 points refunded.
    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now