Note that there are some explanatory texts on larger screens.

plurals
  1. POCode runs multiple times
    primarykey
    data
    text
    <p>Here's and example code where the code i'm executing runs multiple times.</p> <p>First class:</p> <pre><code>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); } } </code></pre> <p>When button is pressed this one opens:</p> <pre><code>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); } } </code></pre> <p>When button is pressed it executes this code:</p> <pre><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); } } } </code></pre> <p>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.</p> <p>I'm guessing it's a simple fix but I can't figure it out.</p> <p>Edit:</p> <p>When i press the btnrun button it runs the "Test3.error()" Code, then closes all frames and creates another main frame.</p> <p>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.</p> <p>If i do it again it runs "Test3.error()" 3 times and so on it keeps doing that.</p> <p>What i want is for it to always run "Test3.error()" just once but for some reason it doesn't.</p> <p>Another example:</p> <pre><code>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); } </code></pre> <p>}</p> <p>And the code that runs:</p> <pre><code>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); } </code></pre> <p>} }</p> <p>Same problem.</p> <p>Everytime i press the run button, it executes the code as many times as i've pressed it in the past.</p> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload