0

I am currently developing my own minesweeper. While overriding a method setTitle I have the following problem.

//InfoDisplayer is inner class of class MenuActionListener
    class InfoDisplayer extends JDialog{ 


    JLabel info;
                BorderLayout infoBorderLayout = new BorderLayout();

                public InfoDisplayer(JFrame ownerFrame){
                    super(ownerFrame,true); //Always modal is set to be true....
                    info = new JLabel();
                    setFocusable(false);                        
                    setSize(300,400);
                    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                    setLayout(infoBorderLayout);
                    add(info,BorderLayout.SOUTH);
                    setVisible(true);
                }

                void setInfo(JLabel  info){
                    this.info = info;
                }

                public void setTitle(String  title){
                       setTitle(title); 
                    }                                   
                }

            if ((event.getActionCommand()).equals("HowToPlay")){
            InfoDisplayer instructionsDisplay = new InfoDisplayer(gUIManagerFrame); 
                    //gUIManagerFrame is an object of its outer class,MenuActionListener
                instructionsDisplay.setTitle("INSTRUCTIONS");
                instructionsDisplay.setInfo(new JLabel("<html><h1><B>INSTRUCTIONS</B></h1></html>"));

                }   
                else{// if about is clicked!!
                InfoDisplayer aboutDisplay = new InfoDisplayer(gUIManagerFrame);
                aboutDisplay.setTitle("MineSweeper v0.1");
                aboutDisplay.setInfo(new JLabel("<html><h1><B>MineSweeperv1.0</B></h1> </html>"));

            }
        }           

The setTitle and setInfo method doesn't work.In addition to that, setTitle method produces exception.

at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338) at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338) at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338) at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338) at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338) at graphicalUserInterface.BoardMenuBar$MenuItemsActionListener$1InfoDisplayer.setTitle(GUIManager.java:338)

I am also eager to know the reason for continues exception Generation.

EDIT: Sorry, I doesnt looked at the recursive call. Its my mistake. Now I have corrected It.

Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

3 Answers3

2
public void setTitle(String  title){
                   setTitle(title); 
                }                                   

This is calling itself - recursive. You want

public void setTitle(String  title){
                   super.setTitle(title); 
                }                                   
1

Look at your setTitle code:

public void setTitle(String  title) {
   setTitle(title); 
} 

That can't possibly work, can it? Your method is just recursing endlessly, eventually blowing up the stack. It's not clear what you want to do, but if you just want to use the inherited implementation, you can simply delete your method entirely.

Now setInfo changes the value of a field, but doesn't add the label to the dialog. You probably want to remove the existing label, and then add the new one.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • If I am correct, why change in model doesnt trigger the change in view? as in Model-view-controller. My 2nd question: How I can trigger the change in view if it doesnt happen in default? Note to our profession God :I spent many hours in meta, collecting info about you. I have a doubt whether to concentrate on theory like (TOC,compiler-theory) or on programming-languages!!! – Muthu Ganapathy Nathan Dec 05 '11 at 16:54
  • @EAGER_STUDENT: It's not really clear what you mean, but if you're talking about the `setInfo` call, just setting a field won't automatically update anything else. Nothing magically "watches" fields for you like that. – Jon Skeet Dec 05 '11 at 17:00
  • how to make the changes in fields (like **setInfo** call) to get reflected in the output. please see also http://stackoverflow.com/questions/8395621/in-model-view-controller-why-change-in-model-doesnt-trigger-the-change-in-vie/8396280#8396280 – Muthu Ganapathy Nathan Dec 06 '11 at 06:47
  • @EAGER_STUDENT: As I said, you'd need to remove the existing label from the set of controls, and add the new one. But to be honest, this isn't really a change in the *model*, as the model shouldn't even know about the UI. I suspect rereading a swing-based MVC tutorial from scratch would be the best course of action at this point. – Jon Skeet Dec 06 '11 at 06:57
1
public void setTitle(String  title){
        setTitle(title); 
    }                                   
}

This code where you are trying to override the method calls the method over and over again (recursively). Eventually the stack runs out of memory and your program crashes.

I assume that you are trying to use the default setTitle() method and for this you want to use the superclass's method setTitle().

public void setTitle(String  title){
        super.setTitle(title); 
    }                                   
}

Simple fix, but works wonders!

Austin Heerwagen
  • 653
  • 3
  • 12