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: JButton issue, please help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JButton issue, please help

    so im coming up with a program that im wanting to have a button, but it isnt showing with my graphics on the frame and i dont know what to do.

    Im trying to pretty much combine these two pictures below and have the button on the bottom.





    Here is the code i have for putting these into the frame.
        public void showInterface()
        {       
     
     
        	//FRAME
            frame.setSize(700, 800);
            frame.setTitle("HangMan");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //PULLS IN STARTING GRAPHICS
            display = new DisplayComponent();
        	JPanel panel = new JPanel();
        	//BUTTON NEEDING TO BE IN THE FRAME, SHOWING GRAPHICS
        	button = new JButton("Guess");
            AddButtonListener listener = new AddButtonListener();
     
        	panel.add(button);
            frame.add(display);
            frame.add(panel);
            button.addActionListener(listener);
     
     
     
            frame.setVisible(true);
        }


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

    Default Re: JButton issue, please help

    I'm not completely sure about this, but I know I did a go-around when I had this problem when I made a card program a long time ago.

    Basically, have 2 JPanels. 1 JPanel will hold your drawing and the other will hold your JButton. Then you just have to add both of them to the JFrame.

    If I am not mistaking, the issue is that the JButton is effectively overriding the JPanel's draw methods when you add it to the JPanel,or hiding the drawing or something. I'm not entirely sure of what exactly is happening, but the JButton trumps the drawing.
    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. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JButton issue, please help

    This part
            frame.add(display);
            frame.add(panel);
    adds the two components to the default CENTER position of the default BorderLayout of a JFrame. Naturally, only the last added is seen.

    Recommended reading:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JButton issue, please help

    Quote Originally Posted by aussiemcgr View Post
    If I am not mistaking, the issue is that the JButton is effectively overriding the JPanel's draw methods when you add it to the JPanel,or hiding the drawing or something. I'm not entirely sure of what exactly is happening, but the JButton trumps the drawing.
    Sorry, you're mistaken. The situation you describe can arise out of wrongly and inappropriately overriding paint(...) of a JComponent. This one's a simple (lack of understanding of ) layout problem.

    db

Similar Threads

  1. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 07:04 PM
  2. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  3. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 11:38 AM
  4. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM