Results 1 to 10 of 10
- 01-13-2009, 06:05 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
How do I set font of labels in an array?
So heres the array:
Java Code:JLabel [] lesson = new JLabel [6]; JLabel [] sublesson = new JLabel [16];
Then I got:
Which does nothing. Why?Java Code:for (int x=0;x>=5;x++) { lesson[x].setFont(new Font("Tw Cen MT", Font.BOLD, 30)); lesson[x].setHorizontalAlignment(SwingConstants.CENTER); } for (int y=0;y>=15;y++) { sublesson[y].setFont(new Font("Tw Cen MT", Font.BOLD, 14)); sublesson[y].setHorizontalAlignment(SwingConstants.CENTER); }Last edited by Exhonour; 01-13-2009 at 06:32 PM.
- 01-13-2009, 06:41 PM #2
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Beause every reference in your array is still null.
You should initialize each object in the array with a new statement. Google "java array initialization"
- 01-13-2009, 06:46 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Like that you mean?Java Code:lesson[0] = new JLabel("Introduction"); sublesson[1] = new JLabel("At the end of this lesson you will be able to do the following:"); sublesson[2] = new JLabel("- Predict whether an object will be attracted to a magnet."); sublesson[3] = new JLabel("- Locate the poles of various types of magnets. "); sublesson[4] = new JLabel("- Observe the attraction and repulsion of magnetic poles."); sublesson[5] = new JLabel("- Identify some uses of magnets.");
- 01-14-2009, 02:31 AM #4
That will certainly help. There were no labels at all, before.
Were you getting a bunch of null pointer exceptions before? You might not have been able to see the exceptions if your code was running on the event dispatcher thread...
- 01-14-2009, 05:55 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Actually it did nothing. No change in font or the alignment. What now?
-
If it were me, I'd create a small program that just tried to do one simple thing, say change the font of a JLabel and display it, and get that program to work. If it works, great, I'd move on to the next thing, say to show an array of JLabel. And if I got stuck, I'd post that small (key on small) compilable and runnable program that demonstrates the problem and hope that someone could help me.
- 01-14-2009, 08:43 AM #7
How many times do you think that loop will be entered?Java Code:for (int x=0;x>=5;x++)
Hint: for how many iterations is x greater than or equal to 5?
db
- 01-14-2009, 08:53 AM #8
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
0,
because x=0 and x is not >=5; the for loop ends
- 01-14-2009, 03:13 PM #9
Just a style note: Typically, programmers use i<6 instead of i<=5
In general, run your program in debug and watch what it is doing. That's a lot faster and more effective than posting on a forum.
- 01-14-2009, 03:48 PM #10
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
Display of tables and labels in the same pane
By Karanam in forum AWT / SwingReplies: 6Last Post: 10-20-2008, 08:34 AM -
how to set font size for a xml value
By bala_kj in forum Advanced JavaReplies: 3Last Post: 04-01-2008, 11:00 AM -
Working with Labels on Panels.
By vargihate in forum AWT / SwingReplies: 2Last Post: 01-04-2008, 04:09 AM -
Aligning Labels
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks