Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Button not showing on panel?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Button not showing on panel?

    I have three classes.. 1. panel class 2. frame class and 3. tester class

    I am creating the objects like so but when I add button to panel then panel to frame I am not seeing the button. here is my code

    import javax.swing.JButton;
    import javax.swing.JPanel;
     
     
    public class Panel extends JPanel{
     
    	private JPanel p;
    	private JButton b;
     
    	public Panel(){
    		p = new JPanel();
    		b = new JButton("hey there");
    		p.add(b);
    	}
    }

    import javax.swing.JFrame;
     
     
    public class Frame extends JFrame{
     
    	private JFrame frame;
     
     
    	public Frame(){
    		frame = new JFrame();
    		frame.setSize(400,600);
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    }

    public class Tester {
     
     
    	public static void main(String args[]){
     
    		ContactsArrayList list = new ContactsArrayList();
    		Frame f = new Frame();
    		Panel p = new Panel();
     
    		f.add(p);
    	}
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Button not showing on panel?

    The problem is your Frame class. Frame "is-a" JFrame, so you don't need to create a new JFrame inside of it (you are making the same mistake in your panel class).
    Instead, you want to do a super call. So, here is what your frame class should look like:
    public class Frame extends JFrame{
    	public Frame(){
    		super();
    		super.setSize(400,600);
    		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		super.setVisible(true);
    	}
    }
    You need to make a similar change in your Panel class. Try to understand what I did, and apply it to your panel class. If you have trouble, feel free to ask me to clarify.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    camel-man (January 27th, 2014)

  4. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Button not showing on panel?

    Thank you very much sir.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Button not showing on panel?

    BTW Giving your classes the same name as existing Java SE classes is confusing. It's Better to use different names.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Button not showing unicode
    By questionmark32 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 9th, 2013, 08:48 AM
  2. Trick - How to build a button to switch between panel colors
    By vividMario52 in forum Java Swing Tutorials
    Replies: 6
    Last Post: May 23rd, 2013, 06:55 AM
  3. Help on exit button/panel
    By pilyo143 in forum AWT / Java Swing
    Replies: 5
    Last Post: February 13th, 2013, 07:07 AM
  4. [SOLVED] The button wouldn't show on the panel
    By nggdowt in forum AWT / Java Swing
    Replies: 7
    Last Post: November 3rd, 2011, 09:31 PM
  5. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM