Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:30:08+00:00 2026-05-28T21:30:08+00:00

Here’s and example code where the code i’m executing runs multiple times. First class:

  • 0

Here’s and example code where the code i’m executing runs multiple times.

First class:

public class Test2 extends JFrame {
public static int asd = 0;

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test2 frame = new Test2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnShowtest = new JButton("ShowTest2");
    btnShowtest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Test1 a1 = new Test1();
            a1.setVisible(true);
            dispose();
        }
    });
    contentPane.add(btnShowtest, BorderLayout.CENTER);
}
}

When button is pressed this one opens:

public class Test1 extends JFrame {
public static int ab = 1;

private JPanel contentPane;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test1 frame = new Test1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Test1() {
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnrun = new JButton("runt");
    btnrun.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Test3.error();
        }
    });
    contentPane.add(btnrun, BorderLayout.CENTER);
}
}

When button is pressed it executes this code:

public class Test3 {
public static void error(){
    JOptionPane.showMessageDialog(null, "error");
    Test2.asd += 1;
    System.err.print(Test2.asd);
    final Frame[] frames = Frame.getFrames();
    for (Frame f : frames){ 
        f.dispose();
        Test2 newtet = new Test2();
        newtet.setVisible(true);
    }
}
}

Now every time I press the buttons again it executes Test3.error(); multiple times.
For example if I press the buttons 10 times then Test3.error(); runs 10 times.

I’m guessing it’s a simple fix but I can’t figure it out.

Edit:

When i press the btnrun button it runs the “Test3.error()” Code, then closes all frames and creates another main frame.

When i get back to btnrun and press it again, it runs “Test3.error()” 2 times instead of once. 2 JOptionePanes and creates new mainframe twice.

If i do it again it runs “Test3.error()” 3 times and so on it keeps doing that.

What i want is for it to always run “Test3.error()” just once but for some reason it doesn’t.

Another example:

public class Frame1 extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame1 frame = new Frame1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Frame1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnRun = new JButton("Run");
    btnRun.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Run.run();
        }
    });
    contentPane.add(btnRun, BorderLayout.CENTER);
}

}

And the code that runs:

public class Run {
public static void run(){
    final Frame[] frames = Frame.getFrames();
    for (Frame f : frames){ 
        f.dispose();
        Frame1 newtet = new Frame1();
        newtet.setVisible(true);
}

}
}

Same problem.

Everytime i press the run button, it executes the code as many times as i’ve pressed it in the past.

Press button once it diposes Frame1 and recreates Frame1.
Press it again and it executes the code 2 times in a row.
(disposes Frame1, creates Frame1, diposes Frame1 and creates Frame1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:30:08+00:00Added an answer on May 28, 2026 at 9:30 pm

    Yes, you’ve added an action listener here:

    JButton btnrun = new JButton("runt");
    btnrun.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Test3.error();
        }
    });
    

    That means “When the button is clicked, call Test3.error().” Were you expecting the action listener to be removed automatically after executing once?

    EDIT: Okay, I think I’ve worked out what’s going on. This is the problem:

    final Frame[] frames = Frame.getFrames();
    for (Frame f : frames){ 
        f.dispose();
        Test2 newtet = new Test2();
        newtet.setVisible(true);
    }
    

    For every existing frame, you’re disposing that frame and creating a new Test2(). So if you had 3 frames on the screen (a Test1, a Test2 and a Test3) when you click the button, you’ll end up with three new Test2 frames. I suspect you didn’t mean to do that. Did you mean this instead?

    for (Frame f : Frame.getFrames()){ 
        f.dispose();
    }
    Test2 newtet = new Test2();
    newtet.setVisible(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an example: I write html code inside of textarea, then I swap
Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.