Results 1 to 10 of 10
  1. #1
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default 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:
    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);
        }
    Which does nothing. Why?
    Last edited by Exhonour; 01-13-2009 at 07:32 PM.

  2. #2
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    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"

  3. #3
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    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.");
    Like that you mean?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    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...

  5. #5
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Actually it did nothing. No change in font or the alignment. What now?

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

    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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Java 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

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    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.

  10. #10
    Exhonour is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 09:00 PM
  2. Display of tables and labels in the same pane
    By Karanam in forum AWT / Swing
    Replies: 6
    Last Post: 10-20-2008, 09:34 AM
  3. how to set font size for a xml value
    By bala_kj in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2008, 12:00 PM
  4. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 05:09 AM
  5. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:26 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
  •