[GUI programming] - setFont() in JFrame


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: [GUI programming] - setFont() in JFrame

  1. #1
    Join Date
    Oct 2004
    Location
    Zuerich (Switzerland)
    Posts
    3

    Question [GUI programming] - setFont() in JFrame

    Good day,

    I'm pretty new in the endless programming world of Java and my question might sound stupid but as I haven't found any answer by myself...

    So, I've created a JFrame composed of several JPanel objects. Each of these latter JPanel contains one or several elements such as buttons or labels. I'd like to use a font (e.g. plain Tahoma 11) being different from the defaulted one for every text being displayed in my JFrame WITHOUT having to apply such a definiton to every element contained in there. In other words, I'd like to get rid of the following code (i.e. loads of redundancy) by replacing it by only 1 line of code that would apply to all components:
    Code:
    btnOpen.setFont(new Font("Tahoma", 0, 11)); 
    btnForward.setFont(new Font("Tahoma", 0, 11)); 
    btnClose.setFont(new Font("Tahoma", 0, 11)); 
    lblName.setFont(new Font("Tahoma", 0, 11)); 
    lblSurame.setFont(new Font("Tahoma", 0, 11));
    Is it possible and, if yes, how could I proceed? Thanks very much in advance for a potential answer!


    Loris

  2. #2
    Join Date
    Oct 2004
    Posts
    151
    hi everyone,


    try this,

    Font f = new Font("Tahoma", 0, 11)

    btnOpen.setFont(f);
    btnForward.setFont(f);
    btnClose.setFont(f);
    lblName.setFont(f);
    lblSurame.setFont(f);

    ps: There is no such thing as a stupid question

    Richard West

  3. #3
    Join Date
    Oct 2004
    Location
    Zuerich (Switzerland)
    Posts
    3
    Hi,

    Cheers for the reply. Unfortunately, the proposed code doesn't really achieve what I want to do. I might have explained my problem quite badly and I'll try to improve that hereafter: I'd like to avoid having to define the font for every component and replace the 5 (or more) following lines of code by only one line that would define the font for every component displaying text in the JFrame.
    Code:
    btnOpen.setFont(new Font("Tahoma", 0, 11)); 
    btnForward.setFont(new Font("Tahoma", 0, 11)); 
    btnClose.setFont(new Font("Tahoma", 0, 11)); 
    lblName.setFont(new Font("Tahoma", 0, 11)); 
    lblSurame.setFont(new Font("Tahoma", 0, 11)); 
    // ... (the list continues and can be quite long)
    While writing this, I've got an idea I'll try out asap: identify all the components and, where appropriate, set the font by applying a loop at the end of the class defining the JFrame.

    More on this later...!


    Loris

  4. #4
    Join Date
    Oct 2004
    Posts
    151

    Question

    Hi everyone,

    You can write a function which has the font instance as an argumen and you can add the components as you usually do to the frame and then call the function at the end. I am assuming that you want the same font for all the jcomponents

    example

    void init (Font f)
    {

    JComponent [ ] comp = Frame.getComponents();

    for(i=0;i<comp.length;i++)
    {
    comp[i].setFont(f);
    }

    }

    I hope this helps you

    Yours Sincerely

    Richard West

  5. #5
    Join Date
    Oct 2004
    Posts
    25

    Exclamation

    @ freesoft_2000

    JFrame has only a JContentPane as a component,

    so, it must be

    JComponent [ ] comps = frame.getContentPane().getComponents();

    floaty

  6. #6
    Join Date
    Oct 2004
    Posts
    151
    hi everyone,

    Yes, floaty is right about the getContentPane() method. I missed that out because i sometimes use awt and got very used it. I apologize.

    The rest of the method remains the same.

    Richard West

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center