Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Showing JPanel error

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Showing JPanel error

    Why do I get this error? --> Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

    It's ridiculous... I'm just adding a JPanel to a JFrame..or?

    Class with JPanel
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class Stapeldiagram extends JPanel
    {
     
        public Stapeldiagram()
        {
     
            this.setBackground(Color.white);
        }
     
        @Override
             public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fill3DRect(35, 225-150, 40, 150, true);
            g.draw3DRect(35,225-150,40,150,false);
            g.setColor(Color.green);
            g.fill3DRect(75, 225-100, 40, 100, true);
            g.draw3DRect(75, 225-100, 40, 100,false);
            g.setColor(Color.blue);
            g.fill3DRect(115, 225-190, 40, 190, true);
            g.draw3DRect(115, 225-190, 40,190,false);
        }
    }//end class


    Class to show the JPanel
    import java.awt.Color;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
     
     
    public class VisaStapel extends JFrame {
     
        Stapeldiagram stip1;
     
        public VisaStapel()
        {
     
            stip1 = new Stapeldiagram();
            add(stip1);
            this.setBackground(Color.white);
        }
     
     
            public static void main(String[] args)
        {
            JFrame f = new JFrame();
            f.setSize(400, 300);
            f.setLocation(100,100);
            f.setTitle("Min JPanel!");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            VisaStapel p = new VisaStapel ();//anropar min JPanel
            f.add(p);
            f.setVisible(true);
        }
     
    }
    Last edited by Fermen; June 19th, 2011 at 08:17 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Showing JPanel error

    I am making a guess here, so forgive me if I'm incorrect.
    I believe it's down to the fact that you're trying to add a JFrame to a JFrame.
    Try replacing your JFrame f = new JFrame with:

    JFrame f = new VisaStapel ();
    then set the size etc of that object.
    Last edited by newbie; June 19th, 2011 at 08:59 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Showing JPanel error

    What line is the error occurring on? You didn't post the full text of the compiler's error message which includes the line and the line number.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Showing JPanel error

    VisaStaple p = new VisaStaple();
    f.add(p);
    Your VisaStaple class extends JFrame. So by trying to add a VisaStaple object to a JFrame you are trying to add a frame to a frame which doesn't work unless you use JInternalFrame.

    I really don't know what you are trying to do here. If you want the Staplediagram class's paintComponent product to appear in the JFrame, I would suggest making a simple change:
    Staplediagram s = new Staplediagram();
    f.add(s);
    f.setVisible(true);

    I don't know if that was a simple fluke/typo or a major logical error. A bit more context would help.

    IF however, that is what you want, that should do it.

  5. The Following User Says Thank You to bgroenks96 For This Useful Post:

    Fermen (June 20th, 2011)

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Showing JPanel error

    Quote Originally Posted by bgroenks96 View Post
    Your VisaStaple class extends JFrame. So by trying to add a VisaStaple object to a JFrame you are trying to add a frame to a frame which doesn't work unless you use JInternalFrame.

    I really don't know what you are trying to do here. If you want the Staplediagram class's paintComponent product to appear in the JFrame, I would suggest making a simple change:
    Staplediagram s = new Staplediagram();
    f.add(s);
    f.setVisible(true);

    I don't know if that was a simple fluke/typo or a major logical error. A bit more context would help.

    IF however, that is what you want, that should do it.
    Yes, this change make it work. But it should, according to my instructions work the way I wrote it to. This is the error:

    Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Container.java: 431)
    at java.awt.Container.addImpl(Container.java:1039)
    at java.awt.Container.add(Container.java:959)
    at javax.swing.JFrame.addImpl(JFrame.java:545)
    at java.awt.Container.add(Container.java:365)
    at Kap5.VisaStapel.main(VisaStapel.java:35)
    Java Result: 1

  7. #6
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Showing JPanel error

    Oh, I think I found out the error now. I should have extended my VisaStapel - class with JPanel instead of JFrame...

    Thanks for the help everyone

Similar Threads

  1. JTextBox not showing?
    By minime12358 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 14th, 2011, 08:02 AM
  2. showing page
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 26th, 2010, 08:52 AM
  3. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM