Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DrMadolite

Thread: Labels won't show in Frame

  1. #1
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Labels won't show in Frame

    I've written a program with 4 classes:
    - "Main" just creates an object of the JFrame in my Frame class.
    - "Frame" creates the frame, visibility is true and I'm using "GameBar" and "Labels" classes to make a JMenuBar and a couple of JLabels.
    - There are no errors that show up, so the code should be fine. But when I run it, it throws an Exception at me:


    Java Code:
    Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    	at java.awt.Container.checkNotAWindow(Unknown Source)
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at javax.swing.JFrame.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at Main.main(Main.java:7)

    Does anyone know what's going on? I mean, I can see what the exception is telling me, but I'm unsure as to what it means specifically. The frame is successfully created and it has a title, but it just can't seem to find the content inside my "GameBar" and "Labels" classes. But as I said, there's no errors popping up in any of the codelines. I'm using Eclipse.

    Thanks in advance.
    May the Newtonian physics be with you.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Labels won't show in Frame

    Can you post the code of your main method?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: Labels won't show in Frame

    Quote Originally Posted by awinston View Post
    Can you post the code of your main method?
    Sure thing:

    Java Code:
    public class Main {
    
    	public static void main(String[] args) {
    		TDXFrame tdx = new TDXFrame();
    		tdx.add(new TDXFrame());
    	}
    
    }
    When I said "Frame" above, I meant "TDXFrame" (just shortened it down for this thread). Also, all the frame setters are inside the TDXFrame class, in case it's relevant. Essentially, the Main should run the Frame and the Frame should run the GameBar and Labels.

    But only the frame itself is there, even though add codes have been correctly written (afaik, since no errors are showing) for GameBar and Labels.
    Last edited by DrMadolite; 08-06-2012 at 05:42 PM.
    May the Newtonian physics be with you.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Labels won't show in Frame

    Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    The message says what the problem is.

    Read the API doc for the Container class's addImpl() method.
    Last edited by Norm; 08-06-2012 at 05:42 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Labels won't show in Frame

    I'm assuming TDXFrame extends JFrame, but correct me if I'm wrong.

    You cannot add a top-level container(JFrame, JDialog, JWindow, JApplet) to another top-level container. In this case, you are trying to add a JFrame to a JFrame.
    Last edited by awinston; 08-06-2012 at 05:50 PM.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: Labels won't show in Frame

    Ok guys, thanks for the replies. It looks like TDXFrame should be making a JPanel, not a JFrame. As you said, awinston, I was indeed making a JFrame contain a JFrame.
    Last edited by DrMadolite; 08-06-2012 at 05:52 PM.
    awinston likes this.
    May the Newtonian physics be with you.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Labels won't show in Frame

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. show content on the same main Frame
    By Shien in forum AWT / Swing
    Replies: 6
    Last Post: 12-12-2011, 06:03 PM
  2. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  3. Replies: 4
    Last Post: 01-31-2009, 05:29 AM
  4. Replies: 2
    Last Post: 08-01-2008, 02:58 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Posting Permissions

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