Results 1 to 6 of 6
  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    93
    Rep Power
    0

    Default Adding JPanel to JPanel not showing

    I am trying to add a JPanel (using an ActionListener on a JButton) to a JPanel. I have tried a few different ways but keep getting the same result. I put in a println and am able to see my method getting called, I just cant figure out why the JPanel in the addNewBill() method isn't showing up. Any suggestions on where to look would be greatly appreciated.

    Java Code:
    package com.bb.ryan;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class BillBoards extends Container{
    	
    	private JLabel pay, paid, title;
    	private JButton addBill;
    	private JPanel payPanel, paidPanel, main, header, p;
    	
    	public BillBoards(){
    		header = new JPanel();
    		header.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(5,5,5,5);
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = i;
    		title = new JLabel();
    		title.setForeground(Color.WHITE);
    		title.setFont(new Font("Arial", Font.BOLD, 24));
    		title.setText("BILL BOARDS");
    		header.add(title, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = i;
    		addBill = new JButton("Add Bill");
    		addBill.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent ae){
    				addNewBill();
    			}
    		});
    		header.add(addBill, c);
    		
    		header.setBackground(Color.BLACK);
    		header.setLocation(300, 0);
    		header.setSize(200, 100);
    		add(header);
    		
    		main = new JPanel();
    		main.setBackground(Color.BLACK);
    		main.setLocation(0, 00);
    		main.setSize(800, 600);
    		add(main);
    	}
    
    	private void addNewBill(){
    		p = new JPanel();
    		p.setLocation(350, 300);
    		p.setSize(100, 100);
    		p.setBackground(Color.WHITE);
    		System.out.println("New Bill Added");
    		add(p);
    	}	
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Adding JPanel to JPanel not showing

    Are you missing some code? Where is the JFrame that will hold the the top level JPanel? And where is your main entry point declared?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    93
    Rep Power
    0

    Default Re: Adding JPanel to JPanel not showing

    I appologize. I didnt add my starter.
    Java Code:
    package com.bb.ryan;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class Starter {
    
    	private JFrame frame;
    	private BillBoards bb;
    	private Dimension d = new Dimension(800,600);
    	
    	public Starter(){
    		EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				frame = new JFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setMinimumSize(d);
    				frame.setMaximumSize(d);
    				frame.setPreferredSize(d);
    				frame.setResizable(false);
    				frame.setLocationRelativeTo(null);
    				
    				bb = new BillBoards();				
    				frame.add(bb);
    				frame.pack();
    				
    				frame.setVisible(true);
    			}
    		});
    	}
    	public static void main(String[] args){
    		new Starter();
    	}
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Adding JPanel to JPanel not showing

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Adding JPanel to JPanel not showing

    Whenever you add / remove components to / from an already visible container you need to call revalidate() and repaint().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    93
    Rep Power
    0

    Default Re: Adding JPanel to JPanel not showing

    Thank you so much DB. I simply added
    Java Code:
    p.revalidate();
    p.repaint();

Similar Threads

  1. Showing pictures on JPanel
    By Kenan_89 in forum Java 2D
    Replies: 18
    Last Post: 03-31-2012, 10:44 AM
  2. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  3. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  4. JPanel not showing up in JFrame
    By rlindsey in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2010, 07:21 AM
  5. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •