1

For the sake of everyone understanding my problem, I've created a simple GUI program which shows my problem. I'll first put the codes for you to analyze. And then, please watch the video below to see what I've been meaning to ask. Please bear with me, the video is just a few seconds and will not take time to load.

Menu JFrame:

    //This is a Menu JFrame Window
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class Menu extends JFrame implements GlobalVariables{

        public Menu(){
            clickMe.setBounds(75, 50, 100, 50);
            clickMe.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    dispose();
                    SubMenu sm = new SubMenu();
                    sm.subMenuProperties();
                }
            });
            add(clickMe);

        }

        void menuProperties(){    
            setLayout(null);
            setSize(250,175);
            setVisible(true);        
            setResizable(false);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        }

        public static void main(String[] args) {
            Menu m = new Menu();
            m.menuProperties();
        }
    }

SubMenu JFrame:

    //This is a SubMenu JFrame Window
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class SubMenu extends JFrame implements GlobalVariables{

        public SubMenu(){
            clickMe2.setBounds(75, 50, 100, 50);
            clickMe2.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    dispose();
                    Menu m = new Menu();
                    m.menuProperties();
                }
            });
            add(clickMe2);

        }

        void subMenuProperties(){    
            setLayout(null);
            setSize(250,175);
            setVisible(true);        
            setResizable(false);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        }

        public static void main(String[] args) {
            SubMenu sm = new SubMenu();
            sm.subMenuProperties();
        }    
    }

And if your're wondering what's the "GlobalVariables" implementation, here it is:

    import javax.swing.*;

    public interface GlobalVariables{    
        //Menu Variable
        JButton clickMe = new JButton("SubMenu");
        //SubMenu Variable
        JButton clickMe2 = new JButton("Back");   
    }

Now, this video will show you what I mean in JFrame being multiplied:

http://www.youtube.com/watch?v=iCavg_1SqvY

*If you watched the video, you can see what I mean when I say the JFrame is being multiplied. I've been analyzing this for days but I cannot identify my mistake. If you can just pinpoint my mistake, I'll be more than thankful. I'm also open for comments and adjustments in the code, I just wish that the structure of the code will not be re-structured because as I've said earlier, I've created this simple GUI just to show you my problem but to tell you at least, I have a whole program waiting to be finished using this kind of approach in GUI making. Please help me.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Malice Sloth
  • 45
  • 3
  • 12
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 12 '13 at 11:47
  • thank you Mr. @AndrewThompson! Got an idea with your help! *still, if any out there has any more ideas, I'm still open for them... – Malice Sloth Feb 12 '13 at 12:01
  • The problem is you keep on adding actionlistener object to the button so it stacks, resulting to multiple execution of codes inside `actionPerformed` – Bnrdo Feb 14 '13 at 07:46

2 Answers2

1

Those two classes are both a subclass of JFrame which results in a new window.

The one named Menu contains a button "clickMe" which instantiates SubMenu that contains a button "clickMe2" that instantiates Menu. This creates a endless loop creating more instances of each class. I.e creating more frames.

In the class SubMenu, remove these lines:

Menu m = new Menu();
m.menuProperties();

To get started using swing, Oracle has great tutorials.

joese
  • 86
  • 6
1

I found the issue with the code. You need to remove the ActionListener in you actionPerformed method. For each button click, your code creates a new ActionListener object and it gets associated with your button. Subsequent clicks gets executed by this duplicate action listeners and you end up getting more JFrames.

Add the below line to actionPerformed(ActionEvent e) method of both classes after the line dispose(). This should fix your problem

Menu.java
clickMe.removeActionListener(this);
......
SubMenu.java
clickMe2.removeActionListener(this)

;

Ramadas
  • 510
  • 2
  • 7