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 8 of 8

Thread: The button wouldn't show on the panel

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default The button wouldn't show on the panel that's inside another panel

    My problem is the the Button"test2" in the constructor won't show up. I have no idea what is wrong and there are no error at all.

    My Goal is to create serveral classes that represent a part of my GUI, which could be put together in my main class. This is a simplify version of my code and has the exact same problem.
    I am sure all I need is a tiny change in my code but I couldn't figure it out. Any help/suggestion will be appreciate.

    import java.awt.*;
    import javax.swing.*;
     
    public class ResPanel extends JPanel{
    	public void ResPanel() {
    		setSize(100,100);
    		setLayout(new FlowLayout());
    		setBackground(Color.blue);
            JButton test2 = new JButton("test2");
    		add(test2);
    	}
     
    	public static void main(String[] args) {
     
    		JFrame frame = new JFrame();
    		JPanel lol = new JPanel();
    		JButton test1 = new JButton("test1");
    		ResPanel r = new ResPanel();
     
    		lol.setLayout(new FlowLayout());
    		lol.setBackground(Color.red);
    		lol.add(test1);
            lol.add(r);
            frame.add(lol);
            frame.setSize(300,300);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    Last edited by nggdowt; November 3rd, 2011 at 11:24 AM. Reason: typo


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: The button wouldn't show on the panel

    There u go add the rest from here:

    import javax.swing.*;
     
     
    public class ResPanel extends JFrame{
     
    	private static final long serialVersionUID = 1L;
    	public JButton test1 = new JButton("test1");
    	public JButton test2 = new JButton("test2");
    	public JPanel lol = new JPanel();
     
    	public ResPanel(){
    		super("Title");
     
    		lol.add(test1);
    		lol.add(test2);
     
    		add(lol);
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		JFrame frame = new ResPanel();
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    }

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: The button wouldn't show on the panel

    First of all, thanks for the quick reply.(for a moment I though my post was deleted lol)

    I am sorry if I didn't make it clear, but I kinda want to keep a certain "style" for the code.
    The new code works perfectly but it won't fit into all my other code unfortunatly.

    What I am doing right now is making my own game and my structure of the code is like :
    Class "game" that contain the main game/animation screen
    class "resource panel" that handle everything about that resouce bar.
    class "info panel" ....
    etc.
    and one last class that contain the frame and the main method which put all the other classes together.
    also, to make the GUI looks sensible, I really want to have Panels inside Panels( just like in the code where I add the panel 'r' into panel 'lol' then add 'lol' to the frame.

    that's the reason I want to extent Jpanel for my ResPanel, so that I could isolate it from the other code.
    Is there really no way to save my original code? I am so close to my goal :<

    I hope I am not asking too much, feel free to say how stubborn I am XD
    Last edited by nggdowt; November 3rd, 2011 at 10:48 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: The button wouldn't show on the panel

    Quote Originally Posted by macko
    There u go add the rest from here:
    macko, please read the forum rules and the following link: http://www.javaprogrammingforums.com...n-feeding.html

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: The button wouldn't show on the panel

    @nggdowt, As an aside, FlowLayout is the default layout of a JPanel, so there isn't a need to set that layout (it doesn't hurt, I am just pointing this out for your benefit).

    Add some println's in your methods, in particular near the code where the button that is not visible is added (in other words, in the method that looks like a constructor, hint hint)

  6. The Following User Says Thank You to copeg For This Useful Post:

    nggdowt (November 6th, 2011)

  7. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: The button wouldn't show on the panel

    Sorry, Keep getting carried away in helping others. Somethings though you do need to agree that viewing somebodys solution can benifit you to see how they have produced the outcome.

    If you are really that stressed out you wouldnt want to code you would just want to get it done, Yes they need to learn but if its something thats due ASAP or they dont want to spend 2 weeks learning. Do you not think this can still help users?

    Also i had only posted back his code, Simply modified the position of the data.. I had not added any panels inside other panels.

    Hence why i had said "Work the rest out yourself"

    I get the basic frame and content on the screen and they can learn the rest, you see where im going?

  8. #7
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: The button wouldn't show on the panel

    As for the rest adding panels inside panels etc..

    try using a for loop

    //for loop to initialize all panels.
    ...
    ...
    //add the panels to another panel you want them in...
    // display that panel on the screen...

    Good luck.

  9. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: The button wouldn't show on the panel

    Thanks for all the great advice!
    right, so the real problem. The constructor of ResPanel didn't get called at all...
    The first thing I tried is changing
    ResPanel r = new ResPanel();
    to
    JPanel r = new ResPanel();
    but it stay the same.
    I could change the whole code up and make it work in another way, but I really want to keep the existing structure of the code.
    is there something I am missing for me to call that constructor? do I need a getter method like
    public ResPanel getResPanel(){
         return new ResPanel();
    }
    or ...
    ===========
    okay i was typing all that and suddenly I found out what is wrong D:
    my code was almost perfect....except....THERE IS A FREAKING VOID IN MY CONSTRUCTOR D:
    i must have been thinking about I need to add void because it returns nothing...
    I removed the void tag and my codes runs fine now.
    @copeg
    thanks for the hint lol..I keep focusing on the main method and trying to find what's wrong and I totally forgot about the constructor.

Similar Threads

  1. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 07:28 AM
  2. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  3. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 01:31 PM
  4. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 07:15 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