1

I'm pretty new to swing and am having some troubles. Heres my code. I'm getting the error

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself at java.awt.Container.checkAddToSelf(Container.java:472) at java.awt.Container.addImpl(Container.java:1083) at java.awt.Container.add(Container.java:410) at UMSL.Eval.createUserInterface(Eval.java:126) at UMSL.Eval.(Eval.java:95) at UMSL.Eval.main(Eval.java:56) Java Result: 1

Can anyone explain to me what this error message is trying to tell me and how I can make things work.

  private void createUserInterface()
{


  JPanel contentPane;

   contentPane = new JPanel();



JPanel instructorPanel = new JPanel();
instructorPanel.setBounds(40, 20, 276, 48);
instructorPanel.setBorder (BorderFactory.createEtchedBorder() );
instructorPanel.setLayout( null) ;
instructorPanel = new JPanel();
contentPane.add(instructorPanel);
  // set up Instructor Label
JLabel instructorLabel = new JLabel();
instructorLabel.setBounds (25, 15, 100, 20);
instructorLabel.setText("Instructor:");
instructorLabel.add (instructorLabel);
Joe
  • 45
  • 4
  • 2
    You're adding a label to itself. Add it to `instructorPanel` – Reimeus Dec 01 '13 at 22:34
  • `instructorPanel.setBounds(40, 20, 276, 48);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Dec 02 '13 at 04:00

1 Answers1

1

Like @Reimeus said, you're trying to add a label to itself with the line instructorLabel.add (instructorLabel); I'm assuming that you're trying to add the label to the instructor panel which would be instructorPanel.add(instructorLabel);

fdsa
  • 1,379
  • 1
  • 12
  • 27