aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes  how to print from 1 to 9 in  a label ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " how to print from 1 to 9 in  a label ?" Watch " how to print from 1 to 9 in  a label ?" New topic
Author

how to print from 1 to 9 in a label ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

i m trying to print 1 to 10 in a jlabel but the code end up with just 9 in the frame and it skipped all the integers from 1 to 8
now how to print from 1 to 9 in a label ?



The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You are having only one label. So it shows the last set value using the setText() method in the for loop. You could either create 10 labels or add a button and an action listener to modify the text of the label to increment every time the button is clicked.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

John Jai wrote:You are having only one label. So it shows the last set value using the setText() method in the for loop. You could either create 10 labels or add a button and an action listener to modify the text of the label to increment every time the button is clicked.


yes but if i go for 10 label and single for loop to print values all the 10 labels will end up with 9 9 9 the end values so alternate way is to create 10 labels and 10 for loops each with different end values so that label 1 will set 1 (which is an end value of 1st for loop ) label 2 will set 2 (which is an end value of 2nd for loop ) and so on ....but i want only 1 for loop and to print 1 to 10 on 10 different labels or on 1 single label
do you have any idea for that ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
an array of JLabels, then in your loop

label[x].setText(""+x);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

is slightly better.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote: is slightly better.



it is showing me null pointer exception ...can you tell me why
i know some where i have not created an object unknowingly and i m using that
can you help me detecting

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4492
    
    8

It's always easier for us to explain exceptions if you tell us where the exception is. The JVM tells you exactly which line it happens on, so pass on the information!

However, in this case you're creating two AppThread objects. You initialise one of them...and call mai() on the other.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I will point you where the error is. Hint - Are you initializing and setting text for the same array variable j?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

I only get an ArrayIndexOutOfBoundsException when you try to access j[11]. That's because the array indexes go from 0 to 10.

After I've fixed that I do get a NullPointerException. The reason is simply - you call init() one one instance and mai() on a completely different instance. That second instance never is initialized, and therefore the entire array is still null. The fix is simple:
A better alternative is to replace the init() method with a constructor.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

John Jai wrote:I will point you where the error is. Hint - Are you initializing and setting text for the same array variable j?


thanks for the exception , but still it is printing only 10 in the JFrame. and not 1 2 3 4 5 6 ...
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Did you add all the labels in the panel?
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

John Jai wrote:Did you add all the labels in the panel?


thanks mate ..njoyd
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print from 1 to 9 in a label ?