Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: StackOverflowError when hiding the first of two Swing GUIs?

  1. #1
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default StackOverflowError when hiding the first of two Swing GUIs?

    Hello there all,
    My basic problem is this;
    I have an object called GUI that has a few methods to build itself with a simple title, and then three methods; Display, Hide and Close. I don't really need to explain what they do. The problems arise when I try to call Hide(). For some reason after calling Display() it doesn't simply hide the window, it creates a StackOverflowError... From what I can gather this happens if you recursively call something too much, or errors along those lines, which confuses me. The code is below:

    Java Code:
    package com.dorksanddorkettes.multiGuiTest;
    
    public class guiCaller {
    
        public static void main(String[] args) {
    	gui first = new gui("First!");
    	gui second = new gui("Second!");
    	first.display();
    	first.hide();
    	second.display();
    	second.hide();
    	first.close();
    	second.close();
        }
    }
    
    package com.dorksanddorkettes.multiGuiTest;
    
    import javax.swing.JFrame;
    
    public class gui extends JFrame{
    
        public gui(String message) {
    	this.setSize(200, 100);
    	this.setTitle(message);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public void display() {
    	this.setVisible(true);
        }
        
        public void hide() {
    	this.setVisible(false);
        }
        
        public void close() {
    	this.dispose();
        }
    }
    I honestly can't see why it would create a Stack Overflow of all things, and in case your interested it doesn't create the second GUI at all, the overflow stops it. The Exception traces back to the hide() method, and centres around, what is on this site, line 34 (the setVisible(false) line).
    Thanks for any help,
    ~Longeye~

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: StackOverflowError when hiding the first of two Swing GUIs?

    Well if you take a look at the stack trace it's giving you:

    at GuiTest.hide(GuiTest.java:28)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at GuiTest.hide(GuiTest.java:28)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at GuiTest.hide(GuiTest.java:28)

    You can gather that GuiTest (name changed to match Java standard naming conventions) calls hide, which calls parent class Component's show() method, which calls Component.setVisible(), which calls Window.setVisible(), which calls hide().. And since you've overridden the hide() method, it starts back at the beginning and keeps doing that until it hits a StackOverflow.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Re: StackOverflowError when hiding the first of two Swing GUIs?

    Oh bugger! Of course! I hadn't though about overrinding it... Thank you very much, it all makes perfect sense!
    Also the name would have fit the conventions but for a typo, I know that much about Java at least ;)
    Again, thanks for the help!
    -Longeye-

Similar Threads

  1. GUIs in RCP
    By noodle_variation_187-X in forum Eclipse
    Replies: 0
    Last Post: 08-24-2011, 03:59 PM
  2. StackOverflowError
    By selva.bics in forum AWT / Swing
    Replies: 5
    Last Post: 08-05-2011, 01:09 PM
  3. Problem with GUIS
    By sunde887 in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 07:18 AM
  4. guis and php
    By paddala in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2010, 02:43 AM
  5. java.swing.popup not hiding
    By pinks_70986 in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2009, 08:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •