Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Java Swing - JLabel setText() doesn't work

  1. #1
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Java Swing - JLabel setText() doesn't work

    Problem has been solved, it was a typo!

    Hello!

    I'm very new to GUI-coding, and I'm trying to make a GUI to a crawler I've made earlier.

    I have the following GUI-code that runs just fine:

    Java Code:
    	public WineKeeperGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		
    		controller = new Controller();
    		
    		JButton addNewWineBtn = new JButton("L\u00E4gg till nytt vin");
    		addNewWineBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				//HERE GOES CODE FOR ADD NEW WINE
    				artnr = Integer.parseInt(txtArtikelnummer.getText());
    				sartnr = Integer.toString(artnr);
    				lblSBnr.setText(sartnr);
    				try {
    					controller.addSBWineToList(artnr);
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				lblName.setText(controller.getName());
    			}
    		});
    On line 18, the method addSBWineToList is run, which parses information from a website and saves it as a wine object in an ArrayList.
    To try whether or not my code is working, I want the JLabel lblName to be set to what the wine name is. To do so, I use the code on line 23.

    This is the code in the controller:

    Java Code:
    	public void addSBWineToList(int arntr) throws IOException{
    		SBWine wine = new SBWine(artnr);
    		wineList.add(wine);
    	}
    	
    	public String getName(){
    		return wineList.get(0).getNameInformation();
    	}
    I have made sure that it actually saves the wine-object in the ArrayList, because using an index of "1" when trying getNameInformation, I get an index out of bounds.

    The code for getNameInformation:

    Java Code:
    	public String getNameInformation(){
    		String nameInfo = this.name + " " + this.extName;
    		return nameInfo;
    	};

    So what is the big problem? Well, my program runs fine so far without any exceptions. Though, the name is never set to anything except an empty string it seems. Can anyone spot any errors in my code or in my way of executing what I want to achieve.

    Thank you!
    Last edited by Zyril; 07-29-2012 at 08:02 PM.

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

    Default Re: Java Swing - JLabel setText() doesn't work

    Try this for a little debugging:

    Java Code:
    public String getNameInformation() {     
       String nameInfo = "TEST:"+this.name + " " + this.extName;     
       return nameInfo; 
    }
    Do you see that String? If so, name and extName are probably empty.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Swing - JLabel setText() doesn't work

    Quote Originally Posted by JosAH View Post
    Try this for a little debugging:

    Java Code:
    public String getNameInformation() {     
       String nameInfo = "TEST:"+this.name + " " + this.extName;     
       return nameInfo; 
    }
    Do you see that String? If so, name and extName are probably empty.

    kind regards,

    Jos
    Thank you Jos, for the quick reply and input! I was just thinking about how I the most easy way could debug and test this, since a simple println wouldn't work! I tried your suggested method and you were spot on the problem! The strings are both empty, so now I have to look into them!

    Thanks alot,
    Z!

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Swing - JLabel setText() doesn't work

    Alright, quick update since I found the trouble making code!

    Java Code:
    	public WineKeeperGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		
    		controller = new Controller();
    		
    		JButton addNewWineBtn = new JButton("L\u00E4gg till nytt vin");
    		addNewWineBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				//HERE GOES CODE FOR ADD NEW WINE
    				artnr = Integer.parseInt(txtArtikelnummer.getText());
    				sartnr = Integer.toString(artnr);
    				lblSBnr.setText(sartnr);
    				try {
    					controller.addSBWineToList(artnr);
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				lblName.setText(controller.getName());
    			}
    		});
    At line 14, it appears that my integer artnr doesn't get the value of the JTextfield. Is the code really correct here, or am I missing an actionlistener or something?

    The result is that at line 18, the value of artnr is 0, that renders an empty wine-object for me.

    Thanks,
    Z!
    Last edited by Zyril; 07-29-2012 at 07:13 PM.

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Swing - JLabel setText() doesn't work

    Alright, I have debugged it over and over, and decided just to start fresh and look through every line of code of the involving methods.
    Lesson learned, check for typos first... I had the parameter arntr instead of artnr in one crucial line of code.

    Do you guys have any ideas on how to stop typos like this from happening in the first place? Use the auto-fills from Eclipse perhaps more often..?

    Thanks,
    Z!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Swing - JLabel setText() doesn't work

    Quote Originally Posted by Zyril View Post
    Alright, I have debugged it over and over, and decided just to start fresh and look through every line of code of the involving methods.
    Lesson learned, check for typos first... I had the parameter arntr instead of artnr in one crucial line of code.

    Do you guys have any ideas on how to stop typos like this from happening in the first place? Use the auto-fills from Eclipse perhaps more often..?

    Thanks,
    Z!
    Or maybe even easier: don't use both names 'artnr' and 'anrtr' ... a typo is flagged by the compiler then as an unknown symbol.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Java Swing - JLabel setText() doesn't work

    Quote Originally Posted by JosAH View Post
    Or maybe even easier: don't use both names 'artnr' and 'anrtr' ... a typo is flagged by the compiler then as an unknown symbol.

    kind regards,

    Jos
    Yep!

    The thing I did wrong here was that I had useless code that kinda made it harder to notice the error.

    Java Code:
    public class Controller {
    	
    	int artnr;
    	ArrayList<SBWine> wineList = new ArrayList<SBWine>();
    	
    	public Controller(){
    
    	}
    	
    	/**
    	 * System operation that GUI calls to create new SB-wine and add to winelist.
    	 * @param arntr
    	 * @throws IOException
    	 */
    	public void addSBWineToList(int arntr) throws IOException{
    		SBWine wine = new SBWine(artnr);
    		wineList.add(wine);
    	}
    If I hadn't had the useless code in line 3, the compiler would have complained about line 16.

    But, since this is the newbie forum I can gladly say I've learned a lesson! =)

Similar Threads

  1. setText() doesn't work
    By Ratsha in forum New To Java
    Replies: 9
    Last Post: 03-22-2012, 02:37 PM
  2. Replies: 9
    Last Post: 02-26-2012, 09:13 PM
  3. webstart link some time doesn't work after Java upgrade
    By Richard Gillespie in forum Advanced Java
    Replies: 0
    Last Post: 12-21-2010, 04:01 PM
  4. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 04:44 AM
  5. If statement and JLabel.setText() problem
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 07-25-2010, 10:11 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
  •