0

Can anyone please tell me why I get the following exceptions

Exception in thread "main" java.lang.IllegalArgumentException adding a window to a container :
 java.awt.Container.checkNotAWindow(Unknown Source) 
 java.awt.Container.addImpl(Unknown Source)  
 java.awt.Container.add(Unknown Source)         
 javax.swing.JFrame.addImpl(Unknown Source)
 java.awt.Container.add(Unknown Source)
 clockframe.<init>(clockframe.java:14)
 clockframe.main(clockframe.java:32)

My code which is inside clockpanel.java file is below: I am beginner so I don't know how to work this out......

    import java.awt.*;
    import javax.swing.*;

    public class clockframe extends JFrame
    {
      public clockframe()
      {
        super("Clock");
        setLookAndFeel();
        setSize(225, 125);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);
        clockpanel time = new clockpanel();
        add(time);
        setVisible(true);
      }

      private void setLookAndFeel()
      {
         try
         {
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
         }
         catch (Exception exc)
         {
             // ignore error
         }
      }

      public static void main(String args[])
      {
          clockframe clock = new clockframe();
      }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Run your code in a debugger, having configured it to break on exceptions: http://stackoverflow.com/questions/3066199/eclipse-break-when-exception-is-thrown – NPE Aug 10 '13 at 14:55
  • Start your class names with a capital letter – keyser Aug 10 '13 at 15:05
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). I'd guess `clockpanel` is actually a frame. But I don't like guessing. 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Aug 10 '13 at 15:05

3 Answers3

2

clockpanel probably extends a window such as JFrame. You probably meant to extend JPanel instead (although doing so is not necessary unless adding new functionality such as custom painting to the new JPanel).

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

Take a look at the JFrame documentation

IllegalArgumentException - if index is invalid 
IllegalArgumentException - if adding the container's parent to itself 
IllegalArgumentException - if adding a window to a container

It is looking likely that clockpanel inherits from Window thus triggering the last clause.

Edit: no need to guess any more.. just noticed the top of your stack trace.. this is the cause.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

Answer lies it self in stack trace :

 thread "main" java.lang.IllegalArgumentException adding a window to a container 

You are adding window to container, which is not allowed.

Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38