Results 1 to 5 of 5
  1. #1
    SteJ is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Disposing JFrame while using extends

    I have read around the forum and looked for disposing a JFrame as the simple dispose(); method won't work in this program. I have my main which declares and initializes the JFrame and then a seperate class which extends the JFrame and all the components are made in the constructor of the class that extends the JFrame. The user has the option to save their order details (what the program is for) and then when they load it, it brings up a new JFrame with their old order details on the frame. However, in the background the previous JFrame is still there and I have tried disposing it through the action listener with this.dispose(); however this method does not work and I cannot get the name of the JFrame as it is in the main, what method can I use to dispose of the old JFrame once the new one is loaded?

    Many thanks.

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Disposing JFrame while using extends

    Why does the base JFrame need initialized? You can just extend it normally, without main. I might not understand what you're trying to say though - can you post the "main which declares and initializes the JFrame and then a seperate class which extends the JFrame" ?

  3. #3
    SteJ is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Disposing JFrame while using extends

    Quote Originally Posted by Diargg View Post
    Why does the base JFrame need initialized? You can just extend it normally, without main. I might not understand what you're trying to say though - can you post the "main which declares and initializes the JFrame and then a seperate class which extends the JFrame" ?
    Thanks for the reply. I think I have mixed up what I have said as just getting to grip with the terms used in java as studying it at uni, first year.

    In my main I have

    public class GuiTest {

    public static void main(String[] args)
    {
    Gui newPanel = new Gui();
    JFrame main = new JFrame();
    mainGui.setDefaultCloseOperation(3);
    mainGui.add(newPanel);
    mainGui.setVisible(true);
    mainGui.pack();
    }
    }

    then another class like this...

    public class Gui extends JPanel
    {
    //code
    }

    I can't really post the actual code because its for an assignment which gets checked for plagiarism and i'm not sure if posting it on forums will go against me, sounds silly, i know.

    Many thanks.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Disposing JFrame while using extends

    Try eliminating
    JFrame main = new JFrame();

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Disposing JFrame while using extends

    Quote Originally Posted by SteJ View Post
    In my main I have

    Java Code:
    public class GuiTest {
    
        public static void main(String[] args) 
        {
            Gui newPanel = new Gui();
            JFrame main = new JFrame();
            mainGui.setDefaultCloseOperation(3);
            mainGui.add(newPanel);
            mainGui.setVisible(true);
            mainGui.pack();       
        }
    }
    This is quite confusing to me since you first create a main variable and then try to use a mainGui variable, one that has never been declared nor initialized. Code like this won't compile, so we shouldn't waste time discussing it until you show something that makes sense.

    I can't really post the actual code because its for an assignment which gets checked for plagiarism and i'm not sure if posting it on forums will go against me, sounds silly, i know.
    Not silly as we believe in abiding by the rules of your institution.

    There's nothing wrong though with creating running program that is different from your assignment but that demonstrates your current problem.

    I have read around the forum and looked for disposing a JFrame as the simple dispose(); method won't work in this program. I have my main which declares and initializes the JFrame and then a seperate class which extends the JFrame and all the components are made in the constructor of the class that extends the JFrame. The user has the option to save their order details (what the program is for) and then when they load it, it brings up a new JFrame with their old order details on the frame. However, in the background the previous JFrame is still there and I have tried disposing it through the action listener with this.dispose(); however this method does not work and I cannot get the name of the JFrame as it is in the main, what method can I use to dispose of the old JFrame once the new one is loaded?
    I think that to make sense out of this, we would need to see code. dispose() works fine for me when I need to use it. I'm not sure why you feel the need for two JFrames though. Most GUI applications that I know of use only one main window. Also, what do you mean by "in the background the previous JFrame is still there and I have tried disposing it through the action listener with this.dispose();" How is it still there? Is it a visible GUI? Or is it still present in some other form? Again, realistic code of some form would help.

    Regarding "and I cannot get the name of the JFrame as it is in the main", names are not important as you may think they are, since they only represent variables, not objects, and what you need is an object reference. Again with code, we can perhaps show you how to get this. Also there are ways of getting references to all the windows that a program is showing.

Similar Threads

  1. Replies: 6
    Last Post: 04-27-2011, 05:27 AM
  2. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 02:29 AM
  3. extends question
    By mac in forum New To Java
    Replies: 8
    Last Post: 04-03-2010, 02:29 AM
  4. getting problem in disposing the widgets
    By thendral m in forum SWT / JFace
    Replies: 7
    Last Post: 01-12-2010, 08:37 AM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Tags for this Thread

Posting Permissions

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