-2

I keep receiving this error

    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.table.*;
    import java.awt.Container.*;

     public class main extends JFrame implements ActionListener
    {



    JButton CustomerOrder;
    JButton SoftwareProducts;
    JButton BooksProducts;
    JButton AddBooks;
    JButton StaffMember;
    JButton LoginView;


    static ArrayList<Software> softwareList = new ArrayList<Software>();

    CardLayout c1 = new CardLayout();


    JPanel mainPanel;
    SoftwareProducts addsoftware;

    CustomerOrder customerorder;

    BooksProducts addProduct;

    AddBooks addBook;

    SoftwareProducts sf = new SoftwareProducts();

    //Press submit;

    public static void main(String[] args)
   {

    new main();
    JFrame frame = new JFrame("Selling Product");
    frame.setLocationRelativeTo(null);
    //frame.getContentPane().add(main, BorderLayout.CENTER);
    //new CustomerOrder();
    //EmailFrame email=new EmailFrame();
    //CustomerOrder co = new CustomerOrder();
    //LoginView login = new LoginView();

  }

public main(){

    super();




  // getContentPane().setBackground(Color.green);





   setSize(700,800);
   setLayout(new FlowLayout());

     /*CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
     cardLayout.show(mainPanel, "softwareProducts");
     */


   //CustomerOrder co = new CustomerOrder();
   //add(co);



   SoftwareProducts = new JButton("SoftwareProducts");
   SoftwareProducts.setSize(150,40);
   SoftwareProducts.setLocation(20,50);


   CustomerOrder = new JButton("CustomerOrder");
   CustomerOrder.setSize(160,40);
   CustomerOrder.setLocation(150,50);



   BooksProducts = new JButton("BooksProducts");
   BooksProducts.setSize(200,40);
   BooksProducts.setLocation(310,50);


   AddBooks = new JButton("AddBook");
   AddBooks.setSize(200,40);
   AddBooks.setLocation(310,50);


   StaffMember = new JButton("StaffMember");
   StaffMember.setSize(200,40);
   StaffMember.setLocation(310,50);


   LoginView = new JButton("LoginView");
   LoginView.setSize(150,40);
   LoginView.setLocation(20,50);


   SoftwareProducts.addActionListener(this);

   add(SoftwareProducts);

   CustomerOrder.addActionListener(this);

   add(CustomerOrder);

   BooksProducts.addActionListener(this);

   add(BooksProducts);

   AddBooks.addActionListener(this);

   add(AddBooks);


   StaffMember.addActionListener(this);

   add(StaffMember);




   mainPanel = new JPanel();
   mainPanel.setBackground(Color.BLUE);
   mainPanel.setPreferredSize(new Dimension(600,500));
   mainPanel.setLayout(c1);







   addsoftware = new SoftwareProducts();
   mainPanel.add(addsoftware,"addsoftware");


   customerorder = new CustomerOrder();  ////// I am getting the error
        from this part of code  
   mainPanel.add(customerorder,"customerorder");///// I am getting the     
         error from this part of code  

   addProduct = new BooksProducts();
   mainPanel.add(addProduct,"addProduct");


   addBook = new AddBooks();
   mainPanel.add(addBook,"addBook");



  /*  mainPanel.add(SoftwareProducts);

     mainPanel = new JPanel(new CardLayout());

     mainPanel.add(SoftwareProducts);

     getContentPane().add(mainPanel);


     addProduct = new BooksProducts();
     mainPanel.add(addProduct,"addProduct");


    */




      add(mainPanel);






       setVisible(true);

}




public void actionPerformed(ActionEvent e)
{

   if(e.getSource() == SoftwareProducts)
   {

    c1.show(mainPanel,"addsoftware");
    this.setTitle("SoftwareProducts");
    //System.out.println("Softwares");
    }


    if(e.getSource() == CustomerOrder)
   {

    c1.show(mainPanel,"customerorder");
    this.setTitle("CustomerOrder");
    //System.out.println("Custmers");
    }


     if(e.getSource() == BooksProducts)
   {

     c1.show(mainPanel,"addProduct");
     this.setTitle("BooksProducts");
    }

    if(e.getSource() == AddBooks)
   {

     c1.show(mainPanel,"addBook");
     this.setTitle("AddBooks");
    }

   if(e.getSource() == LoginView)
   {

     c1.show(mainPanel,"LogIN");
     this.setTitle("LOGIN");
    }

}

      /*  public void actionPerformed(ActionEvent e)
        {
      if(e.getSource() == press)
 {

    System.out.println("addProducts");
    }

   }*/
  }

and my customerOrder class

      import javax.swing.*;
      import javax.swing.table.*; 
      import java.awt.*;
      import java.awt.event.*;
      import java.util.Arrays; 
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      import java.util.*;
      import java.io.*;
       public class CustomerOrder extends JFrame 
     implements  ActionListener 

  {

//JButton asd = new JButton("BUTTON");
//JTextField productID;
//JTextField productName;
//JTextField productCost;
//JTextField productPYear;
//JTextField productPHouse;

JButton showOrder;


DefaultListModel<String> model = new DefaultListModel<>();
JList <String>orderList = new JList(model);
JTable softwareTabel = new JTable();


//DefaultTableModel tableModel = new DefaultTableModel(0, 1);


public CustomerOrder()
{

    //super();

   // JLabel toLabel=new JLabel("Product ID: ");
    //JTextField to=new JTextField();


    setLayout(new FlowLayout());


   /* 
    productName = new JTextField(12);
    add(productName);

    productCost = new JTextField(12);
    add(productCost);

    productPYear = new JTextField(12);
    add(productPYear);


    productPHouse = new JTextField(12);
    add(productPHouse);

    */
    showOrder = new JButton("SHOW ORDER");
    showOrder.setSize(25,40);
    showOrder.addActionListener(this);
    add(showOrder); 





}


public void actionPerformed(ActionEvent e)

{
    //Object[] colNames={"Product ID","Processor","RAm"};
    if(e.getSource() == showOrder)
    {  
         model.removeAllElements();

       /*  GridLayout exLayout = new GridLayout(3, 3);

         JLabel ram,processor; 

         ram = new JLabel("RAM");
         processor = new JLabel("Processor");
         String softwaredata[] = {"ID","RAM","Processor","Product ID","Product Name","Product Year","Product Year","Product PublishHouse"};

         JPanel panel = new JPanel();
         panel.setLayout(new GridLayout(8,   3));

         add(ram);
         add(processor);

         JTable table = new JTable();*/
         //DeafultTableModel dm = new DeafultTableModel(0,0);
          //String header[] = new String[] {"RAM", "Processor","ProductID","Product Name","Product Year","Product Publish House"}; 
          //dm.setColumnIdentifiers(header);
          //Object[][] software = new Object[8][3];

          //model.addRow(orderList.toArray());



          int x = 0;
       while (x < main.softwareList.size())
        {
            //./model.addElement(main.softwareList.get(x).getproductYear());
             model.addElement(""+main.softwareList.get(x).getproductID());
             model.addElement(""+main.softwareList.get(x).getRam());
             model.addElement(""+ main.softwareList.get(x).getProcessor());
             model.addElement(""+ main.softwareList.get(x).getproductID()); 
             model.addElement(main.softwareList.get(x).getproductName());
             model.addElement(""+ main.softwareList.get(x).getproductYear());
             model.addElement(main.softwareList.get(x).getproductPublishHouse());
             //model.addElement(main.softwareList.get(x).getproductID());*/              

             x++;

          //System.out.println("as");


        }
          /* ArrayList<String> table1 = new ArrayList();

          ArrayList<ArrayList<String>> table2 = new ArrayList();
          table1.add("Product ID");
          table1.add("RAM");
          table1.add("Processor");
          table1.add("Product Name");
          table1.add("Product Year");
          table1.add("Product PHouse");

          Object[] software = table1.toArray();
          String[] [] softwarest = new String[table1.size()][];

          int i = 0;
          for (List<String> next : table2){

             softwarest[i++] = next.toArray(new String[next.size()]);
            }



            JTable newTable = new JTable(softwarest,software);

            JFrame frame = new JFrame();
            frame.getContentPane().add(new JScrollPane(newTable));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            */
             //table.add(model);

         // add(orderList);
        //Object[] column = {"RAM:", "Processor:"};
        //Object[][] data = {{"RAM:", "Processor:"}};
        //JTable table = new JTable();
        //table.setShowGrid(false);
        //table.setTableHeader(null); 

    }



}



 }

When i run this code i get the following exception from the main class in customerorder = new CustomerOrder; mainPanel.add(customerorder,"customerorder")

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MASUMI
  • 1
  • 1
  • 1
    Is that code really a [MCVE](http://stackoverflow.com/help/mcve) (with emphasis on the *M*)? Also, please indent your code correctly, remove newlines and commented sections, it is hard to read currently. – Andy Turner Oct 09 '15 at 23:03
  • People on this site are looking to do as much good as they can in the programming community. That means giving as many useful answers as possible. When you ask your question you need to convince answerers that your question is worthwhile and to not just skip it. This means use a short, self-contained code example, rather than giving us all 200 or so lines of your code. – snickers10m Oct 09 '15 at 23:17
  • but I have specified where exactly I have faced the problem if you read it ??? you will find out ??? thanks – MASUMI Oct 09 '15 at 23:19
  • 1
    Pretty obvious what the problem is; CustomerOrder extends JFrame and you add an instance of it to a panel. Think about it from a logical standpoint; it makes no sense to have a window in a container. – TNT Oct 09 '15 at 23:33

2 Answers2

1

customorder is an instance of CustomerOrder, which extends from JFrame; public class CustomerOrder extends JFrame

As the error states illegalargumentexception adding a window to a container; it is illegal to add a window to another window, it simply doesn't make sense.

This is one of the reasons we discourage extending from top level containers like JFrame directly.

Either make the CustomerOrder visible like any normal frame (although that might lead you into other issues) or better yet, base your CustomerOrder component of a JPanel

public class CustomerOrder extends JPanel {
    //...
}

This way you can add it to what ever container you need to.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

Idk if this logic is correct, but in just playing around to try and figure out the reason it doesn't make sense for myself I concluded that:

  • extending JFrame causes only one object to be created and why it tries to add the JFrame frame; object to itself which causes the exception...

  • extending JPanel allows the JFrame frame; <-- object to divide into a Container & a Component which allows you to frame.add(this) ..

frame.add(this) is the component invoked by JPanel method .add()

if this is wrong plz correct..