Search code examples
javajbuttonjoptionpane

JOptionPane is acting weird


I am trying to make a Jbutton work properly. My program requires the user to click on a "hint" button. When the user clicks, a Joption pane will pop up with one hint. Next time he hits, next hint appears and last hint is removed from stack, and so on. I have added a stack of Strings where the hints are stored then added to the actionListener. It all works somewhat fine but instead of removing elements from the stack, my stack grows in size every time the user presses the "ok" button in the JOption pane. What could be the issue? Thank you.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Stack;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    /*-------------------------------------------------------
     * Class GUI creates an user friendly interface for the puzzle game. 
     * It contains buttons, and clues that aids and interects with the                 player
     * 
      --------------------------------------------------------*/
    public class Gui extends JFrame{

       private JButton hint;
       private JPanel window1;
       private JLabel label;
       private Puzzle gamePanel;
       private Display display;
       private Hints hints;
       private Stack theStack;



        public Gui() throws FileNotFoundException, IOException{

           window1 = new JPanel(); // panel for buttons

           hint = new JButton("HINT");
           window1.add(hint); //adds button to panel
           hint.addActionListener(new Hint());

           add(window1,BorderLayout.SOUTH);//buttons attach to frame.

        private class Hint implements ActionListener{

             public void actionPerformed(ActionEvent event){

              try {
       hints.getList(); // adds to the stack. Stack content comes from file 
            // a different class but called here.

            Stack s = hints.getStack();  // return the stack

                        if(hint.isSelected()){ //if user hits the button
                            display.hint(line); //ignore this method. 
                        }
                        else{
                          JOptionPane.showMessageDialog(window1,s.pop()); // pop from the stack
                          System.out.print(s.size()); // I added this just to show how my stack size is growing
                           }


                    if(s.isEmpty()){
             JOptionPane.showMessageDialog(window1,"You are out of hints"); 
                    }

              }catch (IOException ex) {
        Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
             }
          }
        }
    }



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;


    public class Hints{

        private Stack stack;
        private File f;
        private String line;
        private Scanner scanner;



        public Hints(){

            f= new File("hints.txt");
            stack = new Stack();

        }

       public Stack getList() throws FileNotFoundException, IOException{

       scanner = new Scanner(f);

       while(scanner.hasNextLine()){
       line = scanner.nextLine();
       stack.add(line); 

        }
         scanner.close();
          return stack;
       }
       public Stack getStack(){
           return stack;
       }

       }
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;


public class Puzzle_Tester {



    public static void main(String[] args) throws FileNotFoundException, IOException{

           Gui g = new Gui();
            Hints h = new Hints();

            g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                g.pack();
               // g.setSize(1900,1900);
        g.setVisible(true);
                Display display=new Display();



          }
      }

Solution

  • If you have this in your code: hints.getList();, more specifically in your action performed, then you are loading from file each time. Place this line hints.getList(); in the constructor of the Hint class. I am not sure if the framework will create a new Hint class each time, so if it does not resolve the problem, you might want to use a static constructor within which to load your stack.