Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Adding more than one JPanel to my JFrame(North,South)

Adding more than one JPanel to my JFrame(North,South)

Scheduled Pinned Locked Moved Java
dockerhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    I want to add two Jpanel on my tabbed pane ,one should be in south with buttons,and other should be in north where I have Text fields and Labels. My problem is that the buttons on container Jpanel is not showing in south. Here is the code:

    public class Mainframe extends JFrame
    { /**
    *
    */
    private static final long serialVersionUID = 1L;
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel jp1 = new JPanel();
    JPanel container = new JPanel();
    JFrame frame = new JFrame("TEST");
    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");
    JButton button4 = new JButton("Button 4");
    JButton button5 = new JButton("Button 5");
    /**
    * @param args
    */
    private Mainframe()
    {

    	Container contentPane = getContentPane();
    	container.setLayout(new FlowLayout());				
    	BufferedImage myPicture = null;
    	try
    	{			
    		myPicture = ImageIO.read(new File("FINAL.jpg"));
    	} 
    	catch (IOException e1)
    	{
    		
    		e1.printStackTrace();
    	}		
    	
    	JLabel lbl = new JLabel(new ImageIcon(myPicture));		
    	container.add(lbl);
    	container.add(button1);
    	container.add(button2);
    	container.add(button3);
    	container.add(button4);
    	container.add(button5);
    	jp1.add(container,BorderLayout.SOUTH);
                frame.add(tabbedPane);
    	tabbedPane.addTab("First Tab",jp1);	
    frame.setPreferredSize(new Dimension(1500, 1210));
    
        frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE);
        frame.pack();
        frame.setResizable(true);
        frame.setVisible(true);
    

    }
    }

    L 1 Reply Last reply
    0
    • C chdboy

      I want to add two Jpanel on my tabbed pane ,one should be in south with buttons,and other should be in north where I have Text fields and Labels. My problem is that the buttons on container Jpanel is not showing in south. Here is the code:

      public class Mainframe extends JFrame
      { /**
      *
      */
      private static final long serialVersionUID = 1L;
      JTabbedPane tabbedPane = new JTabbedPane();
      JPanel jp1 = new JPanel();
      JPanel container = new JPanel();
      JFrame frame = new JFrame("TEST");
      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 2");
      JButton button3 = new JButton("Button 3");
      JButton button4 = new JButton("Button 4");
      JButton button5 = new JButton("Button 5");
      /**
      * @param args
      */
      private Mainframe()
      {

      	Container contentPane = getContentPane();
      	container.setLayout(new FlowLayout());				
      	BufferedImage myPicture = null;
      	try
      	{			
      		myPicture = ImageIO.read(new File("FINAL.jpg"));
      	} 
      	catch (IOException e1)
      	{
      		
      		e1.printStackTrace();
      	}		
      	
      	JLabel lbl = new JLabel(new ImageIcon(myPicture));		
      	container.add(lbl);
      	container.add(button1);
      	container.add(button2);
      	container.add(button3);
      	container.add(button4);
      	container.add(button5);
      	jp1.add(container,BorderLayout.SOUTH);
                  frame.add(tabbedPane);
      	tabbedPane.addTab("First Tab",jp1);	
      frame.setPreferredSize(new Dimension(1500, 1210));
      
          frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE);
          frame.pack();
          frame.setResizable(true);
          frame.setVisible(true);
      

      }
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It looks as though you have too many containers. Try simplifying your setup by adding one element at a time to see where it's going wrong. Start with the tab, then add a panel to the tab, then a single item to the panel etc.

      Veni, vidi, abiit domum

      C 1 Reply Last reply
      0
      • L Lost User

        It looks as though you have too many containers. Try simplifying your setup by adding one element at a time to see where it's going wrong. Start with the tab, then add a panel to the tab, then a single item to the panel etc.

        Veni, vidi, abiit domum

        C Offline
        C Offline
        chdboy
        wrote on last edited by
        #3

        Here is how I see it can be simplified ,and still the problem exists.

        import java.awt.BorderLayout;
        import java.awt.Container;
        import java.awt.Dimension;
        import java.awt.FlowLayout;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;

        import javax.imageio.ImageIO;
        import javax.swing.Box;
        import javax.swing.BoxLayout;
        import javax.swing.ImageIcon;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.JTabbedPane;
        import javax.swing.JTextField;
        import javax.swing.UIManager;

        public class Mainframe extends JFrame
        { /**
        *
        */
        private static final long serialVersionUID = 1L;
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel jp1 = new JPanel();
        JPanel Firstpanel = new JPanel();
        static JFrame frame = new JFrame("Create Company");
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JButton button3 = new JButton("Button 3");
        JButton button4 = new JButton("Button 4");
        JButton button5 = new JButton("Button 5");
        /**
        * @param args
        */
        private Mainframe()
        {

        	BufferedImage myPicture = null;
        	try
        	{			
        		myPicture = ImageIO.read(new File("FINAL.jpg"));
        	} 
        	catch (IOException e1)
        	{
        		
        		e1.printStackTrace();
        	}		
        	
        	JLabel lbl = new JLabel(new ImageIcon(myPicture));		
        	frame.add(tabbedPane);
        	jp1.add(Firstpanel,BorderLayout.SOUTH);
        	tabbedPane.addTab("New Employer",jp1);		
        	Firstpanel.add(button1);
        	Firstpanel.add(button2);
        	Firstpanel.add(button3);
        	
        	}
        public static void main(String\[\] args) 
        {
        	frame.setPreferredSize(new Dimension(1500, 1210));
            
            frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE);
            frame.pack();
            frame.setResizable(true);
            frame.setVisible(true);
        	new Mainframe();
        	// TODO Auto-generated method stub
        
        }
        

        }

        S L 2 Replies Last reply
        0
        • C chdboy

          Here is how I see it can be simplified ,and still the problem exists.

          import java.awt.BorderLayout;
          import java.awt.Container;
          import java.awt.Dimension;
          import java.awt.FlowLayout;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;

          import javax.imageio.ImageIO;
          import javax.swing.Box;
          import javax.swing.BoxLayout;
          import javax.swing.ImageIcon;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          import javax.swing.JTabbedPane;
          import javax.swing.JTextField;
          import javax.swing.UIManager;

          public class Mainframe extends JFrame
          { /**
          *
          */
          private static final long serialVersionUID = 1L;
          JTabbedPane tabbedPane = new JTabbedPane();
          JPanel jp1 = new JPanel();
          JPanel Firstpanel = new JPanel();
          static JFrame frame = new JFrame("Create Company");
          JButton button1 = new JButton("Button 1");
          JButton button2 = new JButton("Button 2");
          JButton button3 = new JButton("Button 3");
          JButton button4 = new JButton("Button 4");
          JButton button5 = new JButton("Button 5");
          /**
          * @param args
          */
          private Mainframe()
          {

          	BufferedImage myPicture = null;
          	try
          	{			
          		myPicture = ImageIO.read(new File("FINAL.jpg"));
          	} 
          	catch (IOException e1)
          	{
          		
          		e1.printStackTrace();
          	}		
          	
          	JLabel lbl = new JLabel(new ImageIcon(myPicture));		
          	frame.add(tabbedPane);
          	jp1.add(Firstpanel,BorderLayout.SOUTH);
          	tabbedPane.addTab("New Employer",jp1);		
          	Firstpanel.add(button1);
          	Firstpanel.add(button2);
          	Firstpanel.add(button3);
          	
          	}
          public static void main(String\[\] args) 
          {
          	frame.setPreferredSize(new Dimension(1500, 1210));
              
              frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE);
              frame.pack();
              frame.setResizable(true);
              frame.setVisible(true);
          	new Mainframe();
          	// TODO Auto-generated method stub
          
          }
          

          }

          S Offline
          S Offline
          Shubhashish_Mandal
          wrote on last edited by
          #4

          I didn't see any layout set against the JPanel jP1. see this http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html[^]

          Regards Shubhashish

          C 1 Reply Last reply
          0
          • C chdboy

            Here is how I see it can be simplified ,and still the problem exists.

            import java.awt.BorderLayout;
            import java.awt.Container;
            import java.awt.Dimension;
            import java.awt.FlowLayout;
            import java.awt.image.BufferedImage;
            import java.io.File;
            import java.io.IOException;

            import javax.imageio.ImageIO;
            import javax.swing.Box;
            import javax.swing.BoxLayout;
            import javax.swing.ImageIcon;
            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JPanel;
            import javax.swing.JTabbedPane;
            import javax.swing.JTextField;
            import javax.swing.UIManager;

            public class Mainframe extends JFrame
            { /**
            *
            */
            private static final long serialVersionUID = 1L;
            JTabbedPane tabbedPane = new JTabbedPane();
            JPanel jp1 = new JPanel();
            JPanel Firstpanel = new JPanel();
            static JFrame frame = new JFrame("Create Company");
            JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
            JButton button3 = new JButton("Button 3");
            JButton button4 = new JButton("Button 4");
            JButton button5 = new JButton("Button 5");
            /**
            * @param args
            */
            private Mainframe()
            {

            	BufferedImage myPicture = null;
            	try
            	{			
            		myPicture = ImageIO.read(new File("FINAL.jpg"));
            	} 
            	catch (IOException e1)
            	{
            		
            		e1.printStackTrace();
            	}		
            	
            	JLabel lbl = new JLabel(new ImageIcon(myPicture));		
            	frame.add(tabbedPane);
            	jp1.add(Firstpanel,BorderLayout.SOUTH);
            	tabbedPane.addTab("New Employer",jp1);		
            	Firstpanel.add(button1);
            	Firstpanel.add(button2);
            	Firstpanel.add(button3);
            	
            	}
            public static void main(String\[\] args) 
            {
            	frame.setPreferredSize(new Dimension(1500, 1210));
                
                frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE);
                frame.pack();
                frame.setResizable(true);
                frame.setVisible(true);
            	new Mainframe();
            	// TODO Auto-generated method stub
            
            }
            

            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You need to tell the panel to use a border layout like:

            	jp1.setLayout(new BorderLayout());
            	jp1.add(Firstpanel,BorderLayout.SOUTH);
            	tabbedPane.addTab("New Employer",jp1);		
            	Firstpanel.add(button1);
            	Firstpanel.add(button2);
            	Firstpanel.add(button3);
            
            	jp1.add(Secondpanel, BorderLayout.NORTH);
            	Secondpanel.add(button4);
            	Secondpanel.add(button5);
            

            Veni, vidi, abiit domum

            C 1 Reply Last reply
            0
            • L Lost User

              You need to tell the panel to use a border layout like:

              	jp1.setLayout(new BorderLayout());
              	jp1.add(Firstpanel,BorderLayout.SOUTH);
              	tabbedPane.addTab("New Employer",jp1);		
              	Firstpanel.add(button1);
              	Firstpanel.add(button2);
              	Firstpanel.add(button3);
              
              	jp1.add(Secondpanel, BorderLayout.NORTH);
              	Secondpanel.add(button4);
              	Secondpanel.add(button5);
              

              Veni, vidi, abiit domum

              C Offline
              C Offline
              chdboy
              wrote on last edited by
              #6

              Thank you for your help :)

              1 Reply Last reply
              0
              • S Shubhashish_Mandal

                I didn't see any layout set against the JPanel jP1. see this http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html[^]

                Regards Shubhashish

                C Offline
                C Offline
                chdboy
                wrote on last edited by
                #7

                Yes now I know my mistake,thank you :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups