How do you pause a program until a button is pressed in JAVA?

Java programming question?

  • i have finished my code for the first two cards and if button one is pressed the card will switch to card2. but it is show up asking wich one off my clases wi would like to run the game(CreativeMindsStory ) or my card layout class (CardLayoutDemo) but if i click on the stoy wit ends ans shows "Exception in thread "main" java.lang.StackOverflowError' import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CreativeMindsStory { public static void main(String[] args) { JFrame frame = new JFrame("VENGANCE"); frame.setDefaultCloseOperation(JFrame.... CardLayoutDemo demo = new CardLayoutDemo(); demo.addComponentToPane(frame.getConte... frame.pack(); frame.setVisible(true); } } class CardLayoutDemo implements ActionListener { CreativeMindsStory obj = new CreativeMindsStory(); CardLayoutDemo clo = new CardLayoutDemo(); JPanel cards; final String CARD1 = "begin"; final String CARD2 = "soldier"; final static String BUTTONPANEL = "Card with JButtons"; final static String BUTTOnPANEL = "Card with JButtons"; public void addComponentToPane(Container pane) { JPanel selection = new JPanel(); JPanel card1 = new JPanel(); // init your buttons so you can make sure to add this as // ActionListener JButton button1 = new JButton("Button 1"); button1.addActionListener(this); card1.add(button1); card1.add(new JButton("Button 2")); card1.add(new JButton("Button 3")); JPanel card2 = new JPanel(); card2.add(new JButton("Button 4")); card2.add(new JButton("Button 5")); CardLayout cl = new CardLayout(); // save value to use in this scope cards = new JPanel(cl); // init cards and set the layout cards.add(card1, BUTTONPANEL); cards.add(card2, BUTTONPANEL); pane.add(selection, BorderLayout.PAGE_START); pane.add(cards, BorderLayout.CENTER); cl.show(cards, CARD1); } public void actionPerformed(ActionEvent evt) { CardLayout cl = (CardLayout) (cards.getLayout()); if (evt.getActionCommand().equals("Button 1")) { cl.show(cards, CARD2); } // // cl.show(cards, evt.getActionCommand()); // } public static void main(String[] args){ new CardLayoutDemo(); } } 1. How do i make the card layout demo not appear and it just run the program? 2. how doyou fix the stack overflow

  • Answer:

    Stack overflow always means two things. You've either got a loop or recursion that's out of control, or you've got an event handler that's causing the same event to be retriggered. In your case, it looks like you're initializing a CardLayoutDemo INSIDE the CardLayoutDemo class, so it's just recursively creating that class over and over again.

Michael Duff at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Stack overflow always means two things. You've either got a loop or recursion that's out of control, or you've got an event handler that's causing the same event to be retriggered. In your case, it looks like you're initializing a CardLayoutDemo INSIDE the CardLayoutDemo class, so it's just recursively creating that class over and over again.

godfathe...

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.