I've been working through an excersise in a introduction to Java book by Deitel and I have a solution to a problem. The program will compile, but I get a message that says, "Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself" Can Someone please help me?

Here is the code for the program:

// Joseph Jordan
//14.8 
//Grid GUI
package grid;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Grid extends JFrame
{
   private JButton Button_ok;
   private JButton Button_cancel;
   private JButton Button_help;

   private JTextField Value_x;
   private JTextField Value_y;

   private JCheckBox Box_snapGrid;
   private JCheckBox Box_showGrid;

   private JLabel Label_x;
   private JLabel Label_y;

   private JPanel checkPanel;
   private JPanel buttonPanel;
   private JPanel fieldPanel1;
   private JPanel fieldPanel2;
   private JPanel fieldPanel;

public Grid()
{

super("Grid" );
// building checkPanel

Box_snapGrid = new JCheckBox ("Snap to Grid");
Box_showGrid = new JCheckBox ("Show Grid");

checkPanel = new JPanel();
checkPanel.setLayout(new GridLayout(2,1));
checkPanel.add (Box_snapGrid);
checkPanel.add (Box_showGrid);

//Building fieldPanel's
Label_x = new JLabel ("X; ");
Value_x = new JTextField ("8", 3);
fieldPanel1 = new JPanel();
fieldPanel2 = new JPanel();
fieldPanel1.setLayout (new FlowLayout());
fieldPanel1.add (Label_x);
fieldPanel1.add (Value_x);

Label_y = new JLabel ("Y: " );
Value_y = new JTextField("8",3);
fieldPanel2.setLayout (new FlowLayout());
fieldPanel2.add (Label_y);
fieldPanel2.add (Value_y);

fieldPanel = new JPanel();
fieldPanel1.setLayout (new BorderLayout());
fieldPanel1.add (fieldPanel1, BorderLayout.NORTH);
fieldPanel1.add (fieldPanel2, BorderLayout.SOUTH);

//Building Buttons

Button_ok = new JButton( "OK");
Button_cancel = new JButton("Cancel");
Button_help = new JButton("help");

buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout ( 3, 1, 10 ,5));
buttonPanel.add (Button_ok);
buttonPanel.add (Button_cancel);
buttonPanel.add (Button_help);


Container container = getContentPane();
container.setLayout(new FlowLayout (FlowLayout.CENTER, 10,5));
container.add (checkPanel);
container.add(fieldPanel);
container.add(buttonPanel);

setSize(500, 300);
setVisible (true);

}

public static void main(String args[])
{
    Grid application = new Grid();
    application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

}

For lines 63 and 64 try this:

add (fieldPanel1, BorderLayout.NORTH);
add (fieldPanel2, BorderLayout.SOUTH);

ieldPanel1.add (fieldPanel1, BorderLayout.NORTH);
fieldPanel1.add (fieldPanel2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.