Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Error: IllegalArgumentException: adding a window to a container

I keep receiving the error:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
  at java.awt.Container.checkNotAWindow(Container.java:483)
  at java.awt.Container.addImpl(Container.java:1084)
  at java.awt.Container.add(Container.java:966)
  at Lab2.EmployeeGUI.main(EmployeeGUI.java:28)

Can someone please help me and tell me what I'm doing wrong? I am beginner programmer.

package Lab2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Jim Doyle
 */
public class EmployeeGUI extends JFrame implements ActionListener {

    JTextField fName, mName, lName, phone, sal, years;
    JComboBox boxTitle, boxDept;
    DefaultListModel lstdefault;
    JList project;

    DbWork dbw = new DbWork("Lab2");
    DbWork Title = new DbWork("Lab2");
    DbWork Dept = new DbWork("Lab2");
    DbWork Prjs = new DbWork("Lab2");
    DbWork PrjList = new DbWork("Lab2");

    public static void main(String[] args) {
        EmployeeGUI app = new EmployeeGUI();
        JFrame frame = new JFrame("Employee Interface by Jim Doyle");
        frame.getContentPane().add(app, BorderLayout.CENTER);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public EmployeeGUI() {

        JPanel labels = new JPanel();
        labels.setLayout(new GridLayout(8,1));
        labels.add(new JLabel("First Name"));
        labels.add(new JLabel("MI"));
        labels.add(new JLabel("Last Name"));
        labels.add(new JLabel("Title"));
        labels.add(new JLabel("Telephone"));
        labels.add(new JLabel("Salary"));
        labels.add(new JLabel("Department"));
        labels.add(new JLabel("Years in Service"));

        getContentPane().add(labels, BorderLayout.WEST);


        JPanel fields = new JPanel();
        fields.setLayout(new GridLayout(8,1));
        fName = new JTextField(15);
        mName = new JTextField(15);
        lName = new JTextField(15);
        phone = new JTextField(15);
        sal = new JTextField(15);
        years = new JTextField(15);

        boxTitle = new JComboBox();
        boxDept = new JComboBox();

        fields.add(fName);
        fields.add(mName);
        fields.add(lName);
        fields.add(boxTitle);
        fields.add(phone);
        fields.add(sal);
        fields.add(years);

        getContentPane().add(fields, BorderLayout.CENTER);

        JPanel prjinfo = new JPanel();
        prjinfo.setLayout(new GridLayout(1,2));
        prjinfo.add(new JLabel("Project Description"));
        project = new JList();
        lstdefault = new DefaultListModel();

        // add items to title combo box
        while(Title.nextRecord()) {
            String txtTit = Title.getField(1);
            if(txtTit!=null) {
                boxTitle.addItem(Title.getField(1));
            }
        }

        // add items to department combo box
        while(Dept.nextRecord()) {
            String txtDept = Dept.getField(2);
            if(txtDept!=null) {
                boxDept.addItem(Dept.getField(2));
            }
        }

        while(PrjList.nextRecord()) {
            lstdefault.addElement(PrjList.getField(1));
        }

        project = new JList(lstdefault);
        project.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        prjinfo.add(project);
        getContentPane().add(prjinfo, BorderLayout.SOUTH);

    }

     public void actionPerformed(ActionEvent e) {
         String button = e.getActionCommand();

         if(button == "First") {
             if(dbw.firstRecord()) {
                Execute(); 
             }  
         }

         else if(button == "Next") {
             if(dbw.nextRecord()) {
                Execute(); 
             } 
         }

         else if(button == "Save") {
             String sql = "UPDATE FirstName, MiddleName, LastName, WorkPhone, Salary, YearsInService FROM Employee;";
             dbw.processQuery(sql);
         }

        }

     private void action() {
         boxTitle.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 JComboBox b = (JComboBox)e.getSource();
                 String ntitle = (String)b.getSelectedItem();
                 updateTitle(ntitle);
             }
         });
     }



     private void Execute() {
         fName.setText(dbw.getField(1));
         mName.setText(dbw.getField(2));
         lName.setText(dbw.getField(3));
         phone.setText(dbw.getField(5));
         sal.setText(dbw.getField(6));
         years.setText(dbw.getField(8));
         String ftext = dbw.getField(4);
         int dx = TitleList(ftext);
         boxTitle.setSelectedIndex(dx);
         String dtext = dbw.getField(7);
         int dx2 = DeptList(dtext);
         boxDept.setSelectedIndex(dx2);
         action();
     }

     int TitleList(String title) {
         int dx = 0;
         for(int z=0; z<boxTitle.getItemCount(); z++) {
             if(title.equals(boxTitle.getItemAt(z))) {
                 dx = z;
             }
         }
         return dx;
     }

     int DeptList(String dept) {
         int dx = 0;
         for(int z=0; z<boxDept.getItemCount(); z++) {
             if(dept.equals(boxDept.getItemAt(z))) {
                 dx = z;
             }
         }
         return dx;
     }

     private void updateTitle(String title) {

     }

}
like image 419
user2444472 Avatar asked Jun 02 '13 04:06

user2444472


3 Answers

EmployeeGUI extends from JFrame, but in your main method, you are creating a new JFrame and are trying to add an instance of EmployeeGUI to it.

Change EmployeeGUI so it extends from JPanel instead

like image 141
MadProgrammer Avatar answered Nov 10 '22 10:11

MadProgrammer


Here:

frame.getContentPane().add(app, BorderLayout.CENTER);

you're trying to add a JFrame to a JFrame which makes no sense.

Why not instead just try to display app rather than add it to a JFrame. Or even better, not have EmployeeGUI extend JFrame.

like image 27
Hovercraft Full Of Eels Avatar answered Nov 10 '22 11:11

Hovercraft Full Of Eels


You can't add a JFrame to another JFrame. But you can add a JPanel to a JFrame, in other words change EmployeeGUI to make it extends JPanel.

like image 4
Murad Alm. Avatar answered Nov 10 '22 11:11

Murad Alm.