Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-13-2009, 07:05 PM
Member
 
Join Date: Jan 2009
Posts: 21
Rep Power: 0
Exhonour is on a distinguished road
Default How do I set font of labels in an array?
So heres the array:
Code:
  JLabel [] lesson = new JLabel [6];  
  JLabel [] sublesson = new JLabel [16];

Then I got:
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);
    }
Which does nothing. Why?

Last edited by Exhonour; 01-13-2009 at 07:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-13-2009, 07:41 PM
Member
 
Join Date: Dec 2008
Location: Italy
Posts: 79
Rep Power: 0
raffaele181188 is on a distinguished road
Default
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"
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-13-2009, 07:46 PM
Member
 
Join Date: Jan 2009
Posts: 21
Rep Power: 0
Exhonour is on a distinguished road
Default
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.");
Like that you mean?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-14-2009, 03:31 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 964
Rep Power: 2
Steve11235 is on a distinguished road
Default
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...
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-14-2009, 06:55 AM
Member
 
Join Date: Jan 2009
Posts: 21
Rep Power: 0
Exhonour is on a distinguished road
Default
Actually it did nothing. No change in font or the alignment. What now?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-14-2009, 06:59 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,381
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-14-2009, 09:43 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 715
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
Code:
for (int x=0;x>=5;x++)
How many times do you think that loop will be entered?

Hint: for how many iterations is x greater than or equal to 5?

db
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-14-2009, 09:53 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
0,
because x=0 and x is not >=5; the for loop ends
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-14-2009, 04:13 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 964
Rep Power: 2
Steve11235 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-14-2009, 04:48 PM
Member
 
Join Date: Jan 2009
Posts: 21
Rep Power: 0
Exhonour is on a distinguished road
Thumbs up
Thx for ur help that solved it
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change font/ font color etc in a graphic object using JCombobox? JavaInLove AWT / Swing 5 04-25-2009 09:00 PM
Display of tables and labels in the same pane Karanam AWT / Swing 6 10-20-2008 09:34 AM
how to set font size for a xml value bala_kj Advanced Java 3 04-01-2008 12:00 PM
Working with Labels on Panels. vargihate AWT / Swing 2 01-04-2008 05:09 AM
Aligning Labels Java Tip Java Tips 0 01-02-2008 07:26 PM


All times are GMT +2. The time now is 06:59 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org