Results 1 to 7 of 7
  1. #1
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default AWT label not updating

    This applet is supposed to display an array of 32 checkboxes, each of which corresponds to the value of one of the bits of an int. When one is checked/unchecked, the program should loop through each checkbox and, if it is checked, add its value to num, which is then displayed in lbl.

    However, although the checkboxes are displayed as intended, the text displayed by the label never changes. What am I doing wrong?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class AppletTest extends Applet implements ItemListener {
    	Checkbox[] cb = new Checkbox[32];
    	Label lbl = new Label("0");
    	int num;
    	public void init() {
    		GridBagLayout gbl = new GridBagLayout();
    		GridBagConstraints gbc = new GridBagConstraints();
    		setLayout(gbl);
    		gbc.weightx = 2.0;
    		gbc.weighty = 2.0;
    		gbc.anchor = (GridBagConstraints.NORTHWEST);
    		
    		for(int i = 0; i < 32; i++) {
    			cb[i] = new Checkbox(String.valueOf(1 << i));
    			add(cb[i]);
    			gbc.gridx = i / 8;
    			gbl.setConstraints(cb[i], gbc);
    			gbc.gridy = i % 8;
    			gbl.setConstraints(cb[i], gbc);
    		}
    		gbc.gridheight = GridBagConstraints.REMAINDER;
    		gbc.gridwidth = GridBagConstraints.REMAINDER;
    		gbc.gridx = 0;
    		gbc.gridy = 9;
    		add(lbl);
    		gbl.setConstraints(lbl, gbc);
    		
    	}
    	
    	public void itemStateChanged(ItemEvent ie) {
    		num = 0;
    		for(int i = 0; i < 32; i++) {
    			if(cb[i].getState()) {
    				num += 1 << i;
    			}
    		}
    		lbl.setText(String.valueOf(num));
    		repaint();
    	}
    }
    Edit - never mind, got it - forgot to add item listeners
    Last edited by Iron Lion; 11-20-2010 at 04:02 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you adding the listener to the checkboxes somewhere?

    Also, why AWT and not Swing? Sorry, but I don't know AWT too well.

    edit: also, no need to call repaint() after setting the Label's text. You may wish to make the label larger to handle bigger numbers, and you may wish to use BigIntegers so as not to have int overflow errors.
    Last edited by Fubarable; 11-20-2010 at 04:03 PM.

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    I'm trying to learn from Herbert Schildt's book, in which he recommends learning AWT before Swing. Do you think I shouldn't bother with it?

    Yeah, just noticed the size of the label - I meant to have it along the entire bottom row. I haven't encountered any int overflow errors, since it treats cb[31] as -2147483648.
    Last edited by Iron Lion; 11-20-2010 at 04:05 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Iron Lion View Post
    I'm trying to learn from Herbert Schildt's book, in which he recommends learning AWT before Swing. Do you think I shouldn't bother with it?
    Yeah, don't bother with AWT. Sorry, but this makes me suspect the quality of his book.

    Yeah, just noticed the size of the label - I meant to have it along the entire bottom row. I haven't encountered any int overflow errors, since it treats cb[31] as -2147483648.
    That's the problem -- you won't see any exceptions when this occurs, but rather int will happily and quietly roll over into negative numbers.

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

    Default

    Quote Originally Posted by Iron Lion View Post
    I'm trying to learn from Herbert Schildt's book, in which he recommends learning AWT before Swing.
    Herbert Schildt is just a shame and is only in it for the money; all his books belong in the fire place; get yourself another book; the book you have now is just a waste of trees.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    That's the expected behaviour, as I was using the checkboxes to simulate the bits of a signed int.

    Thanks for your suggestion regarding Swing - I'll skip over the rest of the AWT stuff. It's a reference rather than a tutorial, which I suppose explains why it needs to be in there, but he does mention that Swing uses various AWT classes so it makes sense to know them.
    Last edited by Iron Lion; 11-20-2010 at 04:54 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Iron Lion View Post
    That's the expected behaviour, as I was using the checkboxes to simulate the bits of an unsigned int.
    Fair enough.


    Thanks for your suggestion regarding Swing - I'll skip over the rest of the AWT stuff. It's a reference rather than a tutorial, which I suppose explains why it needs to be in there, but he does mention that Swing uses various AWT classes so it makes sense to know them.
    Yes, but you'll learn them as you learn Swing. My personal opinion is that there's no advantage to learning all of AWT.

Similar Threads

  1. First SWT Label
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:38 PM
  2. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  3. Getting disk label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:07 AM
  4. Example of SWT Label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:02 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
  •