Results 1 to 20 of 20
  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default JFrame JLabel and JPane

    I'm writing a code that I want to contain six buttons, that perform no particular functions, a fairly simple program. The problem: No buttons are appearing.
    The method I used is fairly similar to what is presented in the book, so I'm not sure why this is not working. Any thoughts?

    Java Code:
    import javax.swing.*;
    
    public class flowLayout{
    
    	public static void main(String[] args) {
    		
    		FlowLayoutDemo flow = new FlowLayoutDemo();	
    		
    		flow.setTitle("Flow Layout Demo");
    		flow.setSize(750 , 120);
    		flow.setLocationRelativeTo(null);
    		flow.setVisible(true);			
    		flow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class FlowLayoutDemo extends JFrame {
    	public FlowLayoutDemo(){
    		
    		super("Java Flow");	//Sets the title of the JFrame
    		setLayout(new FlowLayout());  
    		
    		JPanel p1 = new JPanel();
    		p1.setLayout(new GridLayout(1,3));
    		
    		JPanel p2 = new JPanel();
    		p2.setLayout(new GridLayout(1,3));
    		
    	//Panel 1 Buttons
    		p1.add(new JButton("Button 1"));
    		p1.add(new JButton("Button 2"));
    		p1.add(new JButton("Button 3"));
    	//Panel 2 Buttons
    		p2.add(new JButton("Button 4"));
    		p2.add(new JButton("Button 5"));
    		p2.add(new JButton("Button 6"));
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: JFrame JLabel and JPane

    Well you're adding buttons to two panels p1 and p2, but what are you adding the p1 and p2 panels to?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    I'm adding them to the JFrame that is created in main method, or at least that's what I thought I was doing.

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

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    The method I used is fairly similar to what is presented in the book
    Just curious. Did the book's example extend JFrame?

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

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    I'm adding them to the JFrame that is created in main method, or at least that's what I thought I was doing.
    Which line is doing that then?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by jim829 View Post
    Just curious. Did the book's example extend JFrame?

    Regards,
    Jim
    Yes, they did extend JFrame in the book. The example I referenced from the book:
    JFrame JLabel and JPane-screen-shot-2014-03-26-11.20.15-am.jpg

  7. #7
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    You didn't add the two panels to the Frame

    Java Code:
    ...
    //Panel 2 Buttons
            p2.add(new JButton("Button 4"));
            p2.add(new JButton("Button 5"));
            p2.add(new JButton("Button 6"));
    
    add(p1);
    add(p2);

  8. #8
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    Thank you guys.

    Cg33k, that fixed it. What is the difference between p2.add and simply add(p1)...? I assumed what I did was just a condensed version of creating the button and adding it.
    Last edited by javaStooge; 03-26-2014 at 04:26 PM.

  9. #9
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    Thank you guys.

    Cg33k, that fixed it. What is the difference between p2.add and simply add(p1)...? I assumed what I did was just a condensed version of creating the button and adding it.
    when you use panel.add(Component); you are actually adding a certain component inside the panel...
    when you use add(cmp) or this.add(cmp) you are adding a component (in this case the panels) to the main frame of your program hence they become visible.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: JFrame JLabel and JPane

    I wish you had figured that out yourself though, by reading up on it and reasoning about it. A perfectly good learning moment wasted by spoonfeeding of code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default BorderLayout

    Thank you guys very much for the help. Seems like a simple fix that was negligently overlooked. Appreciate all the work you guys do in answering our questions.

    I do have one other question, regarding BorderLayout. I have created two panels which contains 3 buttons on each panel and I would like to set them up as .CENTER and .SOUTH, but the output only contains a single button (button 3) that extends across .SOUTH in the window.

    Java Code:
    super("Border Layout Demo");
    		setLayout(new BorderLayout(20, 20));
    		//Create Two Panels
    		JPanel p1 = new JPanel();
    		p1.setLayout(new BorderLayout());
    		JPanel p2 = new JPanel();
    		p2.setLayout(new BorderLayout());
    		
    		//Create Six Buttons In Locations Determined By BorderLayout
    		p1.add(new JButton("Button 1"), BorderLayout.SOUTH);
    		p1.add(new JButton("Button 2"), BorderLayout.SOUTH);
    		p1.add(new JButton("Button 3"), BorderLayout.SOUTH);
    		
    		p2.add(new JButton("Button 4"), BorderLayout.CENTER);
    		p2.add(new JButton("Button 5"), BorderLayout.CENTER);
    		p2.add(new JButton("Button 6"), BorderLayout.CENTER);
    		
    		add(p1);
    		add(p1);

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    Yes, they did extend JFrame in the book. The example I referenced from the book:
    It's not unusual (sadly) to see that in examples, but it's generally considered poor code. You are, after all, not extending the functionality of the JFrame in any way. Also, if you hadn't extended JFrame and simply created a JFrame then you possibly would have noticed you hadn't added the panels to anything.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: BorderLayout

    As for your new question, read the second sentence of the BorderLayout API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default Re: BorderLayout

    Quote Originally Posted by Tolls View Post
    As for your new question, read the second sentence of the BorderLayout API.
    I understand and I see that it is not necessary to place the BorderLayout.____ for each button. Instead would I use BorderLayout for the panel? I haven't been able to find anything on this in our textbook or the internet.
    Last edited by javaStooge; 03-26-2014 at 06:17 PM.

  15. #15
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    103
    Rep Power
    0

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by Tolls View Post
    It's not unusual (sadly) to see that in examples, but it's generally considered poor code. You are, after all, not extending the functionality of the JFrame in any way. Also, if you hadn't extended JFrame and simply created a JFrame then you possibly would have noticed you hadn't added the panels to anything.
    :)
    Thank you for the criticisms. Most places I have looked used "extends JFrame" when creating a typical window. Do you typically build your frame/window from ground zero?

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

    Default Re: BorderLayout

    Quote Originally Posted by javaStooge View Post
    I haven't been able to find anything on this in our textbook or the internet.
    From the internet. How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

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

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    I understand and I see that it is not necessary to place the BorderLayout.____ for each button. Instead would I use BorderLayout for the panel? I haven't been able to find anything on this in our textbook or the internet.
    This is the start of the second sentence in the API:
    "
    Each region may contain no more than one component...
    "
    Consequently your attempt to add 3 buttons to the SOUTH region (for example) ended up with only the last button being acknowledged.

    Quote Originally Posted by javaStooge View Post
    Thank you for the criticisms. Most places I have looked used "extends JFrame" when creating a typical window. Do you typically build your frame/window from ground zero?
    It's no more ground zero than your code. I simply add stuff to a JFrame object I create, along the lines of:
    Java Code:
    private void display() {
        JFrame frame = new JFrame();
        populateFrame(frame);  // This will add components to the frame.
        ... other stuff for setting up the frame and actually showinng it ...
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: JFrame JLabel and JPane

    Quote Originally Posted by javaStooge View Post
    Do you typically build your frame/window from ground zero?
    Nope! Here is what I normally do. Others may do it slightly differently.

    Java Code:
    public class MyClass extends JPanel {
    
       JFrame              frame;
       
       public MyClass(int width, int height) {
          frame = new JFrame();
          setPreferredSize(new Dimension(width, height));
          frame.add(this);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       } 
       // rest of class here
    }
    This has the added advantage of sizing the Panel to exactly width x height. In the earlier example, you sized the frame and thus reduced the actual panel real estate by the border insets.

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

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

    Default Re: JFrame JLabel and JPane

    Ugh, what? A JFrame created as part of the creation of a JPanel which will then become a child of that JFrame? My brain hurts.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: JFrame JLabel and JPane

    Yeah. The Frame instance just holds the panel. I could also just make a separate class that extends JPanel. But I usually don't even subclass JPanel unless I want to do some graphics.

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

Similar Threads

  1. JLabel Array not showing in JFrame
    By kkid in forum AWT / Swing
    Replies: 12
    Last Post: 04-12-2013, 10:21 PM
  2. Make a JLabel (and obviously how to add it to a JFrame...)
    By watachiaieto in forum AWT / Swing
    Replies: 6
    Last Post: 05-23-2012, 09:18 AM
  3. Cannot refresh a JLabel on a JFrame
    By carnotan in forum AWT / Swing
    Replies: 11
    Last Post: 03-25-2011, 03:02 PM
  4. [SOLVED] Adding JLabel to JFrame
    By mlfatty in forum AWT / Swing
    Replies: 3
    Last Post: 03-04-2009, 11:33 PM
  5. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 07:44 AM

Posting Permissions

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