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

Thread: why wony my label show on gui

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default why wony my label show on gui

    I am trying to add a label to my gui, i am using the following code but it is not showing.

    Can some one tell what i am doing wrong please

     
    		contentPane = this.getContentPane(); 
    		contentPane.setBackground(Color.GREEN);
    		contentPane.setLayout(null);
     
        JLabel greeting = new JLabel("hello");
        greeting.setFont(new Font("SansSerif", Font.PLAIN, 18));
        contentPane.add(greeting);
        greeting.setVisible(true);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: why wony my label show on gui

    Can you show us an SSCCE instead of an incomplete code snippet?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why wony my label show on gui

     
     
    public class game extends JFrame{
     
    private JLabel greeting;
     
    	public static void main(String[] args) {
     
    		new game();
     
    	}
     
    	public game() {
     
     
    		contentPane = this.getContentPane(); 
    		contentPane.setBackground(Color.GREEN);
    		contentPane.setLayout(null);
     
            JLabel greeting = new JLabel ("welkom");
            contentPane.add(greeting, BorderLayout.NORTH);
     
      	setResizable(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: why wony my label show on gui

            contentPane.setLayout(null);    //<<<< Turns OFF layout manager
     
            JLabel greeting = new JLabel ("welkom");
            contentPane.add(greeting, BorderLayout.NORTH);  // sends instructions to layout manager

    There is a conflict in the posted code.
    Which way do you want to go?
    Do your own layout?
    Use the existing layout manager?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why wony my label show on gui

    use the existing layout.

    i thought i need to add another layout because the label wouldnt show

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: why wony my label show on gui

    use the existing layout.
    Then get rid of the call to setLayout()
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why wony my label show on gui

    Quote Originally Posted by Norm View Post
    Then get rid of the call to setLayout()
    without this
     contentPane.setLayout(null);

    my layout of the rest of my gui goes funny

    --- Update ---

    ive solved it, just needed to set the bounds.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: why wony my label show on gui

    Then this is NOT what you are doing:
    use the existing layout.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: why wony my label show on gui

    Quote Originally Posted by newtolearningjava View Post
    I am trying to add a label to my gui, i am using the following code but it is not showing.

    Can some one tell what i am doing wrong please

     
    		contentPane = this.getContentPane(); 
    		contentPane.setBackground(Color.GREEN);
    		contentPane.setLayout(null);
     
        JLabel greeting = new JLabel("hello");
        greeting.setFont(new Font("SansSerif", Font.PLAIN, 18));
        contentPane.add(greeting);
        greeting.setVisible(true);
    Personally, I don't like using JLabels in the format you used. I would normally use them like this
    JFrame frame = new JFrame();
    frame.set(new JLabel("Hi"));

    but to make the format you used work correctly, i'm assuming you would need to add your jframe name.add(greeting); like this

     
    		contentPane = this.getContentPane(); 
    		contentPane.setBackground(Color.GREEN);
    		contentPane.setLayout(null);
     
        JLabel greeting = new JLabel("hello");
       your jframe name.add(greeting);
        greeting.setFont(new Font("SansSerif", Font.PLAIN, 18));
        contentPane.add(greeting);
        greeting.setVisible(true);

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: why wony my label show on gui

    Quote Originally Posted by Leklur View Post
    Personally, I don't like using JLabels in the format you used. I would normally use them like this
    JFrame frame = new JFrame();
    frame.set(new JLabel("Hi"));
    Huh? Can you show us in the API a JFrame.set() method that takes a JLabel as an argument?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. resetting label text alongside gui
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 50
    Last Post: April 15th, 2014, 12:18 PM
  2. My GUI will not show up.
    By fpritt24 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 15th, 2013, 04:49 PM
  3. Problems getting floating point number to show in GUI...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2012, 11:48 PM
  4. New frame should only show GUI once!
    By gerre in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 30th, 2012, 10:34 PM
  5. 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