Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By jim829

Thread: reusable label code

  1. #1
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default reusable label code

    I cant seem to figure this out. It keep telling me "Duplicate local variable lbl" I am trying to pass the label name into it. What am i doing wrong?

    Java Code:
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    
    public class Item_Btn implements ActionListener {	
    	
    	
    	public void label(String lbl,String name, int l, int t, int w, int h ) {
    						
    		JLabel lbl = new JLabel(name);
    		lbl.setHorizontalAlignment(SwingConstants.CENTER);
    		lbl.setFont(new Font("Tahoma", Font.PLAIN, 12));
    		lbl.setBounds(l, t, w, h);		
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: reusable label code

    So you want a String named 'lbl' and you also want a JLabel named 'lbl' ... that confuses the compiler.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: reusable label code

    Java Code:
    public void label(String lbl,  // lbl number 1
                          String name, 
                          int l, int t, int w, int h ) {
                             
            JLabel lbl = new JLabel(name);  // lbl number 2
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: reusable label code

    Im trying to pass the string lbl into the method so i can name the JLabel using the string i passed in.

    EX:
    String lbl would = "First_Button"

    How could I use the string i passed in to place it in place of the yellow block?

    JLabel lbl = new JLabel(name);

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: reusable label code

    The names cannot be the same. If you have two kids, and you give them both the same name, things would get confusing when you call out their name. That's how the compiler feels. He's sad now. Insecure. Then you change the names into something different. Now the compiler is happy again.

    When you did that, call lbl.setText( yourParameterHere ) to actually put the given text in the JLabel.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: reusable label code

    Quote Originally Posted by Madhatter40 View Post
    Im trying to pass the string lbl into the method so i can name the JLabel using the string i passed in.
    I really don't know what that means. If I give you the following version of your code:

    Java Code:
    public class Item_Btn implements ActionListener {  
         
         
        public void label(String name, int l, int t, int w, int h ) {
                             
            JLabel lbl = new JLabel(name);
            lbl.setHorizontalAlignment(SwingConstants.CENTER);
            lbl.setFont(new Font("Tahoma", Font.PLAIN, 12));
            lbl.setBounds(l, t, w, h);     
            
            // ... and now what? What to do with this JLabel object? 
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
             
             
        }
     
    }
    Then the only thing that I still see missing is that the label isn't actually added to anything yet; it won't be shown anywhere in your program.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: reusable label code

    Quote Originally Posted by gimbal2 View Post
    Then the only thing that I still see missing is that the label isn't actually added to anything yet; it won't be shown anywhere in your program.
    No 'yet' about it. The local variable goes out of scope when the method returns, and 'lbl' is no longer accessible.

    Also, Madhatter40 needs to learn this Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing) and not use setBounds(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: reusable label code

    And in Gimbal's example, no text is assigned to the label, so it's blank.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: reusable label code

    Quote Originally Posted by SurfMan View Post
    And in Gimbal's example, no text is assigned to the label, so it's blank.
    Huh? That would be that 'name' parameter
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: reusable label code

    Quote Originally Posted by gimbal2 View Post
    Huh? That would be that 'name' parameter
    No, because in his first code, he had a label as well. That's how the fight started :)
    Java Code:
    public void label(String lbl,String name, int l, int t, int w, int h )
    ...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: reusable label code

    I know, I removed it because it appeared to just be a duplicate, and now there is STILL the name parameter which is actually passed to the JLabel as its displayable text in the original and the edited code.

    JLabel (Java Platform SE 7 )

    I would have changed the naming, but I didn't want to detach the edited code too much from the original.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: reusable label code

    Too many assumptions are being made. For all I know, the String lbl passed as an argument should have been JLabel lbl. So something like this could be done.

    Java Code:
    public void label(JLabel lbl, String name, int l, int t, int w, int h ) {      
        lbl.setHorizontalAlignment(SwingConstants.CENTER);
        lbl.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lbl.setBounds(l, t, w, h);     
    }
    Bottom line is the OP needs to explain what the original arguments are for and what the intent is.

    Regards,
    Jim
    Last edited by jim829; 01-21-2015 at 04:03 PM.
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: reusable label code

    Quote Originally Posted by jim829 View Post
    Too many assumptions are being made. For all I know, the String lbl passed as an argument should have been JLable lbl.
    How's that for an assumption... :)

    I agree, we're all stabbing at the code, pointing out flaws, but no one knows what the purpose is of this code, besides keeping us busy :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: reusable label code

    Quote Originally Posted by SurfMan View Post
    How's that for an assumption... :)

    I agree, we're all stabbing at the code, pointing out flaws, but no one knows what the purpose is of this code, besides keeping us busy :)
    I think you missed my point.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Permanent reusable connection to DB
    By Pumbaa in forum JDBC
    Replies: 1
    Last Post: 11-23-2010, 10:01 AM
  2. Replies: 1
    Last Post: 08-16-2010, 11:49 AM
  3. Mysql reusable
    By Kligham in forum New To Java
    Replies: 1
    Last Post: 09-13-2009, 07:08 AM
  4. First SWT Label
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:38 PM

Posting Permissions

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