Link to home
Create AccountLog in
Avatar of komlaaa
komlaaa

asked on

Adding a Container within a Container

Why this coder is not working?
What should i do?

ERROR MESSAGE: "java.lang.IllegalArgumentException: illegal component position"

=============== Code ==================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

/**
 *
 * @author  Agbeko Komla
 */
public class StudentSupportServices extends JFrame {
    private StudentPanel student;
    private CoursePanel course;
    private ProfessorPanel professor;
    private ExamPanel exam;
    private ControlPanel controls;

    private Connection connect;

   
    /** Creates a new instance of StudentInformations */
    public StudentSupportServices() {
       
        super("Student Support Services");

//These are all JPanels that i create externally        
        course = new CoursePanel();
        student = new StudentPanel();
        professor = new ProfessorPanel();
        exam = new ExamPanel();
        controls = new ControlPanel();


        Container studentAndCourseContentPane = getContentPane();
        studentAndCourseContentPane.setLayout( new FlowLayout( ) );
        studentAndCourseContentPane.add( student ) ;
        studentAndCourseContentPane.add( course ) ;

       
        Container professorAndExamContentPane = getContentPane();
        professorAndExamContentPane.setLayout( new FlowLayout( ) );
        professorAndExamContentPane.add( professor );
        professorAndExamContentPane.add( exam );

        JLayeredPane rootPane = getLayeredPane();                <=============== ERROR MESSAGE
        rootPane.add(studentAndCourseContentPane, BorderLayout.NORTH ) ;
        rootPane.add(professorAndExamContentPane , BorderLayout.CENTER );
        rootPane.add( controls, BorderLayout.SOUTH );
        //Container controlsContenPane = getContentPane();
        //controlsContenPane.add(controls, BorderLayout.SOUTH );
       
        pack();
        show();
    }
   
  public static void main( String [] args )
    {
       
       (new StudentSupportServices()).addWindowListener
        (
            new WindowAdapter()
            {
                public void windowClosing( WindowEvent e)
                {
                    System.exit( 0 );
                }
            }
        );
    }
}

==================
/*
 * ControlPanel.java
 *
 * Created on January 9, 2005, 3:10 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ControlPanel extends JPanel
{
    private JButton findData,findSchedule, addData, updateData, clear, help;
   
    /** Creates a new instance of ControlPanel */
    public ControlPanel()
    {
        super();
        setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 10));
        setBorder(new CompoundBorder(
        BorderFactory.createLineBorder(Color.BLACK,1),
        BorderFactory.createBevelBorder(BevelBorder.RAISED)));
       
        findData = new JButton( "Find" );
        //add actionListner later
        add(findData);
       
        findSchedule = new JButton( "Schedule" );
        //add actionListner later
        add(findSchedule);
       
        addData = new JButton( "Add" );
        //add actionListner later
        add(addData);
       
        updateData = new JButton( "Update" );
        //add actionListner later
        add(updateData);
       
        clear = new JButton( "Clear" );
        //add actionListner later
        add(clear);
       
        help = new JButton( "Help" );
        //add actionListner later
        add(help);
    }
   
}

============================
/*
 * Course.java
 *
 * Created on January 19, 2005, 8:19 AM
 */

package sss;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author  Agbeko Komla
 */
public class CoursePanel extends JPanel
{
   
    /** Creates a new instance of Course */
    public CoursePanel()
    {
         super();
    }
   
}
========================
/*
 * StudentSchedule.java
 *
 * Created on January 9, 2005, 3:09 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ExamPanel extends JPanel
{        
    /** Creates a new instance of StudentSchedule */
   public ExamPanel()
    {
         super();
     
  }
}
========================
/*
 * Professor.java
 *
 * Created on January 19, 2005, 8:20 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ProfessorPanel extends JPanel
{
   
    /** Creates a new instance of Professor */
    public ProfessorPanel()
    {
         super();
    }
   
}
====================
/*
 * StudentPanel.java
 *
 * Created on January 9, 2005, 3:09 AM
 */

package sss;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author  Agbeko Komla
 */
public class StudentPanel extends JPanel
{
    private String labels[] =
    {"ID:", "Last Name:", "First Name:", "Student Phone:", "Box #:"};
   
    JTextField last, first, id, phone, box;
    JTextField fields[] = {id, last, first, phone, box};
   
    /** Creates a new instance of StudentPanel */
    public StudentPanel()
    {
         super();
        /*
        for(int i = 0; i < fields.length; i++)
        {
            fields[i] = new JTextField(10);
            add( new JLabel( labels[i], JLabel.CENTER ));
            add( fields[i] );
        }
         */
        JPanel p1 = new JPanel( );
        //add actionListner later
        add(p1);
       
        JPanel p2 = new JPanel( );
        //add actionListner later
        add(p2);
       
    }
   
    public int getLabelLength()
    {
        return labels.length;
    }
}
Avatar of zzynx
zzynx
Flag of Belgium image

Same Q as always: At what line do you get that error?
Sorry, didn't see the pointer ;°)
>> JLayeredPane rootPane = getLayeredPane();                <=============== ERROR MESSAGE

don't you mean
 
Container cont = getContentPane();
This is strange:

        Container studentAndCourseContentPane = getContentPane();            // <<<<<<<< 1st time
        studentAndCourseContentPane.setLayout( new FlowLayout( ) );
        studentAndCourseContentPane.add( student ) ;
        studentAndCourseContentPane.add( course ) ;

        Container professorAndExamContentPane = getContentPane();         // <<<<<<<<<< 2nd time
        professorAndExamContentPane.setLayout( new FlowLayout( ) );
        professorAndExamContentPane.add( professor );
        professorAndExamContentPane.add( exam );

???
Are you aware of the fact that the above equals:

Container c = getContentPane();
c.setLayout( new FlowLayout( ) );
c.add( student ) ;
c.add( course ) ;
c.add( professor );
c.add( exam );
Avatar of komlaaa
komlaaa

ASKER

I am trying to add  studentAndCourseContentPane <<<<<<< #1)
and  professorAndExamContentPane <<<<< #2)
to  JLayeredPane rootPane = getLayeredPane();                

Basically trying to add two container into another one. Not feasable?
>> Basically trying to add two container into another one. Not feasable?
Sure.

What are
studentAndCourseContentPane & professorAndExamContentPane ?

I guess you want:

        JPanel studentAndCoursePanel = new JPanel();
        studentAndCoursePanel.setLayout( new FlowLayout( ) );
        studentAndCoursePanel.add( student ) ;
        studentAndCoursePanel.add( course ) ;

        JPanel professorAndExamPanel = new JPanel();
        professorAndExamPanel.setLayout( new FlowLayout( ) );
        professorAndExamPanel.add( professor );
        professorAndExamPanel.add( exam );

        Container c = getContentPane();
        c.add(studentAndCourseContentPane, BorderLayout.NORTH ) ;
        c.add(professorAndExamContentPane , BorderLayout.CENTER );
        c.add( controls, BorderLayout.SOUTH );
I think that the content pane of a JFrame uses the BorderLayout by default but if you want to be sure,
you can add:

        JPanel studentAndCoursePanel = new JPanel();
        studentAndCoursePanel.setLayout( new FlowLayout( ) );
        studentAndCoursePanel.add( student ) ;
        studentAndCoursePanel.add( course ) ;

        JPanel professorAndExamPanel = new JPanel();
        professorAndExamPanel.setLayout( new FlowLayout( ) );
        professorAndExamPanel.add( professor );
        professorAndExamPanel.add( exam );

        Container c = getContentPane();
        c.setLayout( new BorderLayout() );           // <<<<<<<<<<<<<<<<<<<<<<<< 2B sure
        c.add(studentAndCourseContentPane, BorderLayout.NORTH ) ;
        c.add(professorAndExamContentPane , BorderLayout.CENTER );
        c.add( controls, BorderLayout.SOUTH );
Avatar of komlaaa

ASKER

This version run fine but the panel are being displayed?
why is that?
=========CODE==========
/*
 * StudentInformations.java
 *
 * Created on January 9, 2005, 3:02 AM
 */

package sss;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
 *
 * @author  Agbeko Komla
 */
public class StudentSupportServices extends JFrame {
    private StudentPanel student;
    private CoursePanel course;
    private ProfessorPanel professor;
    private ExamPanel exam;
    private ControlPanel controls;

    private Connection connect;

   
    /** Creates a new instance of StudentInformations */
    public StudentSupportServices() {
       
        super("Student Support Services");
       
        course = new CoursePanel();
        student = new StudentPanel();
        professor = new ProfessorPanel();
        exam = new ExamPanel();
        controls = new ControlPanel();
       
        JPanel StudentCourseProfessorExamPanel = new JPanel();
        StudentCourseProfessorExamPanel.setLayout(new GridLayout(2,2) );
        StudentCourseProfessorExamPanel.add( course );
        StudentCourseProfessorExamPanel.add( student );
        StudentCourseProfessorExamPanel.add( professor );
        StudentCourseProfessorExamPanel.add( exam );
       
        Container c = getContentPane();
        c.setLayout( new BorderLayout() );
        c.add(StudentCourseProfessorExamPanel, BorderLayout.CENTER ) ;
        c.add( controls, BorderLayout.SOUTH );

       
        pack();
        show();
    }
   
  public static void main( String [] args )
    {
       
       (new StudentSupportServices()).addWindowListener
        (
            new WindowAdapter()
            {
                public void windowClosing( WindowEvent e)
                {
                    System.exit( 0 );
                }
            }
        );
    }
}
====================================
/*
 * ControlPanel.java
 *
 * Created on January 9, 2005, 3:10 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ControlPanel extends JPanel
{
    private JButton findData,findSchedule, addData, updateData, clear, help;
   
    /** Creates a new instance of ControlPanel */
    public ControlPanel()
    {
        super();
        setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 10));
        setBorder(new CompoundBorder(
        BorderFactory.createLineBorder(Color.BLACK,1),
        BorderFactory.createBevelBorder(BevelBorder.RAISED)));
       
        findData = new JButton( "Find" );
        //add actionListner later
        add(findData);
       
        findSchedule = new JButton( "Schedule" );
        //add actionListner later
        add(findSchedule);
       
        addData = new JButton( "Add" );
        //add actionListner later
        add(addData);
       
        updateData = new JButton( "Update" );
        //add actionListner later
        add(updateData);
       
        clear = new JButton( "Clear" );
        //add actionListner later
        add(clear);
       
        help = new JButton( "Help" );
        //add actionListner later
        add(help);
    }
   
}

=============================
/*
 * Course.java
 *
 * Created on January 19, 2005, 8:19 AM
 */

package sss;


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

/**
 *
 * @author  Agbeko Komla
 */
public class CoursePanel extends JPanel
{
   
    /** Creates a new instance of Course */
    public CoursePanel()
    {
     JPanel exam = new JPanel();
       exam.setLayout( new FlowLayout() );
       exam.setBorder(new TitledBorder(new EtchedBorder(),"Just Trying"));
       exam.add(new JButton( "Place Holder") );  
    }
   
}
===========================
/*
 * StudentSchedule.java
 *
 * Created on January 9, 2005, 3:09 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ExamPanel extends JPanel
{        
    /** Creates a new instance of StudentSchedule */
   public ExamPanel()
    {
       JPanel exam = new JPanel();
       exam.setLayout( new FlowLayout() );
       exam.setBorder(new TitledBorder(new EtchedBorder(),"Just Trying"));
       exam.add(new JButton( "Place Holder") );
  }

      
   
  }
   
====================================
/*
 * Professor.java
 *
 * Created on January 19, 2005, 8:20 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class ProfessorPanel extends JPanel
{
   
    /** Creates a new instance of Professor */
    public ProfessorPanel()
    {
      JPanel exam = new JPanel();
       exam.setLayout( new FlowLayout() );
       exam.setBorder(new TitledBorder(new EtchedBorder(),"Just Trying"));
       exam.add(new JButton( "Place Holder") );
    }
   
}
=========================================
/*
 * StudentPanel.java
 *
 * Created on January 9, 2005, 3:09 AM
 */

package sss;

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

/**
 *
 * @author  Agbeko Komla
 */
public class StudentPanel extends JPanel
{
    private String labels[] =
    {"ID:", "Last Name:", "First Name:", "Student Phone:", "Box #:"};
   
    JTextField last, first, id, phone, box;
    JTextField fields[] = {id, last, first, phone, box};
   
    /** Creates a new instance of StudentPanel */
    public StudentPanel()
    {
       
        for(int i = 0; i < fields.length; i++)
        {
            fields[i] = new JTextField(10);
            add( new JLabel( labels[i], JLabel.CENTER ));
            add( fields[i] );
        }
         
    }
   
    public int getLabelLength()
    {
        return labels.length;
    }
}
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of komlaaa

ASKER

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
 *
 * @author  Agbeko Komla
 */
public class StudentSupportServices extends JFrame {
    private StudentPanel student;
    private CoursePanel course;
    private ProfessorPanel professor;
    private ExamPanel exam;
    private ControlPanel controls;

    private Connection connect;

   
    /** Creates a new instance of StudentInformations */
    public StudentSupportServices() {
       
        super("Student Support Services");
       
        course = new CoursePanel();
        student = new StudentPanel();
        professor = new ProfessorPanel();
        exam = new ExamPanel();
        controls = new ControlPanel();
       
        JPanel StudentCourseProfessorExamPanel = new JPanel();
        StudentCourseProfessorExamPanel.setLayout(new GridLayout(2,2) );
        StudentCourseProfessorExamPanel.add( course );
        StudentCourseProfessorExamPanel.add( student );
        StudentCourseProfessorExamPanel.add( professor );
        StudentCourseProfessorExamPanel.add( exam );
       
        Container c = getContentPane();
        c.setLayout( new BorderLayout() );
        c.add(StudentCourseProfessorExamPanel, BorderLayout.CENTER ) ;
        c.add( controls, BorderLayout.SOUTH );

       
        pack();
        show();    <<<<<<<<<< I add show here,... no need to apply show in main, I tried that but didn't change much.

    }

=====================
Container c = getContentPane();
        c.setLayout( new BorderLayout() );           // <<<<<<<<<<<<<<<<<<<<<<<< 2B sure
        c.add(studentAndCourseContentPane, BorderLayout.NORTH ) ;
        c.add(professorAndExamContentPane , BorderLayout.CENTER );
        c.add( controls, BorderLayout.SOUTH );
Makes sens but i don't want the GUI to have that look.
Avatar of komlaaa

ASKER

>>>I found out why<<<<<<<<<
i am creating another panel intead of the current one
>>>>>>       JPanel exam = new JPanel();  <<<< shouldn't be there.

public class ExamPanel extends JPanel
{        
    /** Creates a new instance of StudentSchedule */
   public ExamPanel()
    {
       JPanel exam = new JPanel();
       exam.setLayout( new FlowLayout() );
       exam.setBorder(new TitledBorder(new EtchedBorder(),"Just Trying"));
       exam.add(new JButton( "Place Holder") );
  }

What does your getLayeredPane() method look like?
Avatar of komlaaa

ASKER

>What does your getLayeredPane() method look like?
it just like other Container, but i thought that should have the ability to hold other Containers
its name "Layered" means it.


SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.