Results 1 to 7 of 7
Thread: AWT label not updating
- 11-20-2010, 03:54 PM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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?
Edit - never mind, got it - forgot to add item listenersJava 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(); } }Last edited by Iron Lion; 11-20-2010 at 04:02 PM.
-
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.
- 11-20-2010, 04:03 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
-
Yeah, don't bother with AWT. Sorry, but this makes me suspect the quality of his book.
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.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.
- 11-20-2010, 04:10 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-20-2010, 04:16 PM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
-
Fair enough.
Yes, but you'll learn them as you learn Swing. My personal opinion is that there's no advantage to learning all of AWT.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.
Similar Threads
-
First SWT Label
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:38 PM -
How to use Break with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
Getting disk label
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:07 AM -
Example of SWT Label
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks