[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Changing fonts in runtime?

I would like to allow the user to change the font of
   everything in my app.
   I do a setFont() on the top frame but two problems occur:
   1) The font of the menu items is never changed.
   2) I have to resize the frame for the fonts to change, Calling repaint() did not help.
0
mco
Asked:
mco
1 Solution
 
heyhey_Commented:
calling validate, invalidate to your component and its parent in a special sequence may help :) (i don't remeber what was the sequence - check the docs ...)

hope this helps
  heyhey


0
 
diakovCommented:
The menu components have their own setFont(), but you may be use it already.

I think I mentioned this in another question, you may have to set the font of each components manually through a call to it's setFont().

Then call validate()/repaint() or doLayout()/repaint() of the parent container (the Frame in your case) which will have to force all subcomponents to relay their layout (the menus are not subcomponents so try to recreate them).

here's an example application that changes font of menu and components.

/*
      This simple extension of the java.awt.Frame class
      contains all the elements necessary to act as the
      main window of an application.
 */

import java.awt.*;

public class Frame1 extends Frame
{
      public Frame1()
      {
            // This code is automatically generated by Visual Cafe when you add
            // components to the visual environment. It instantiates and initializes
            // the components. To modify the code, only use code syntax that matches
            // what Visual Cafe can generate, or Visual Cafe may be unable to back
            // parse your Java file into its visual environment.
            
            //{{INIT_CONTROLS
            setLayout(null);
            setVisible(false);
            setSize(405,305);
            setFont(new Font("SansSerif", Font.PLAIN, 12));
            openFileDialog1 = new java.awt.FileDialog(this);
            openFileDialog1.setMode(FileDialog.LOAD);
            openFileDialog1.setTitle("Open");
            //$$ openFileDialog1.move(40,277);
            label1 = new java.awt.Label("asdfasdfasdfasdf");
            label1.setBounds(60,96,91,43);
            add(label1);
            label2 = new java.awt.Label("asdf");
            label2.setBounds(192,96,174,123);
            add(label2);
            button1 = new java.awt.Button();
            button1.setLabel("Change Font");
            button1.setBounds(24,192,98,35);
            button1.setBackground(new Color(12632256));
            add(button1);
            setTitle("A Basic Application");
            //}}
            
            //{{INIT_MENUS
            mainMenuBar = new java.awt.MenuBar();
            menu1 = new java.awt.Menu("File");
            miNew = new java.awt.MenuItem("New");
            miNew.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_N,false));
            menu1.add(miNew);
            miOpen = new java.awt.MenuItem("Open...");
            miOpen.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_O,false));
            menu1.add(miOpen);
            miSave = new java.awt.MenuItem("Save");
            miSave.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_S,false));
            menu1.add(miSave);
            miSaveAs = new java.awt.MenuItem("Save As...");
            menu1.add(miSaveAs);
            menu1.addSeparator();
            miExit = new java.awt.MenuItem("Exit");
            menu1.add(miExit);
            mainMenuBar.add(menu1);
            menu2 = new java.awt.Menu("Edit");
            miCut = new java.awt.MenuItem("Cut");
            miCut.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_X,false));
            menu2.add(miCut);
            miCopy = new java.awt.MenuItem("Copy");
            miCopy.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_C,false));
            menu2.add(miCopy);
            miPaste = new java.awt.MenuItem("Paste");
            miPaste.setShortcut(new MenuShortcut(java.awt.event.KeyEvent.VK_V,false));
            menu2.add(miPaste);
            mainMenuBar.add(menu2);
            menu3 = new java.awt.Menu("Help");
            mainMenuBar.setHelpMenu(menu3);
            miAbout = new java.awt.MenuItem("About..");
            menu3.add(miAbout);
            mainMenuBar.add(menu3);
            setMenuBar(mainMenuBar);
            //$$ mainMenuBar.move(4,277);
            //}}
            
            //{{REGISTER_LISTENERS
            SymWindow aSymWindow = new SymWindow();
            this.addWindowListener(aSymWindow);
            SymAction lSymAction = new SymAction();
            miOpen.addActionListener(lSymAction);
            miAbout.addActionListener(lSymAction);
            miExit.addActionListener(lSymAction);
            button1.addActionListener(lSymAction);
            //}}
      }
      
      public Frame1(String title)
      {
            this();
            setTitle(title);
      }
      
    /**
     * Shows or hides the component depending on the boolean flag b.
     * @param b  if true, show the component; otherwise, hide the component.
     * @see java.awt.Component#isVisible
     */
    public void setVisible(boolean b)
      {
            if(b)
            {
                  setLocation(50, 50);
            }      
            super.setVisible(b);
      }
      
      static public void main(String args[])
      {
            (new Frame1()).setVisible(true);
      }
      
      public void addNotify()
      {
            // Record the size of the window prior to calling parents addNotify.
            Dimension d = getSize();
            
            super.addNotify();
      
            if (fComponentsAdjusted)
                  return;
      
            // Adjust components according to the insets
            setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
            Component components[] = getComponents();
            for (int i = 0; i < components.length; i++)
            {
                  Point p = components[i].getLocation();
                  p.translate(insets().left, insets().top);
                  components[i].setLocation(p);
            }
            fComponentsAdjusted = true;
      }
      
      // Used for addNotify check.
      boolean fComponentsAdjusted = false;
      
      //{{DECLARE_CONTROLS
      java.awt.FileDialog openFileDialog1;
      java.awt.Label label1;
      java.awt.Label label2;
      java.awt.Button button1;
      //}}
      
      //{{DECLARE_MENUS
      java.awt.MenuBar mainMenuBar;
      java.awt.Menu menu1;
      java.awt.MenuItem miNew;
      java.awt.MenuItem miOpen;
      java.awt.MenuItem miSave;
      java.awt.MenuItem miSaveAs;
      java.awt.MenuItem miExit;
      java.awt.Menu menu2;
      java.awt.MenuItem miCut;
      java.awt.MenuItem miCopy;
      java.awt.MenuItem miPaste;
      java.awt.Menu menu3;
      java.awt.MenuItem miAbout;
      //}}
      
      class SymWindow extends java.awt.event.WindowAdapter
      {
            public void windowClosing(java.awt.event.WindowEvent event)
            {
                  Object object = event.getSource();
                  if (object == Frame1.this)
                        Frame1_WindowClosing(event);
            }
      }
      
      void Frame1_WindowClosing(java.awt.event.WindowEvent event)
      {
            setVisible(false);      // hide the Frame
            dispose();                  // free the system resources
            System.exit(0);            // close the application
      }
      
      class SymAction implements java.awt.event.ActionListener
      {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                  Object object = event.getSource();
                  if (object == miOpen)
                        miOpen_Action(event);
                  else if (object == miAbout)
                        miAbout_Action(event);
                  else if (object == miExit)
                        miExit_Action(event);
                  else if (object == button1)
                        button1_ActionPerformed(event);
            }
      }
      
      void miAbout_Action(java.awt.event.ActionEvent event)
      {
            //{{CONNECTION
            // Action from About Create and show as modal
            (new AboutDialog(this, true)).setVisible(true);
            //}}
      }
      
      void miExit_Action(java.awt.event.ActionEvent event)
      {
            //{{CONNECTION
            // Action from Exit Create and show as modal
            (new QuitDialog(this, true)).setVisible(true);
            //}}
      }
      
      void miOpen_Action(java.awt.event.ActionEvent event)
      {
            //{{CONNECTION
            // Action from Open... Show the OpenFileDialog
            int            defMode                  = openFileDialog1.getMode();
            String      defTitle            = openFileDialog1.getTitle();
            String defDirectory      = openFileDialog1.getDirectory();
            String defFile                  = openFileDialog1.getFile();

            openFileDialog1 = new java.awt.FileDialog(this, defTitle, defMode);
            openFileDialog1.setDirectory(defDirectory);
            openFileDialog1.setFile(defFile);
            openFileDialog1.setVisible(true);
            //}}
      }

      void button1_ActionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
                  
            //{{CONNECTION
            // Set the Frame's Font...
            Component[] cl = getComponents();
            for (int i = 0; i < cl.length; i++)
            {
                  cl[i].setFont(new Font("SansSerif", Font.PLAIN, 20));      
            }
            MenuBar mb = getMenuBar();
            Menu m = mb.getMenu(0);
            m.setFont(new Font("SansSerif", Font.PLAIN, 20));

            setMenuBar(null);
            setMenuBar(mb);

            validate();
            repaint();
            
            //}}
      }
}

cheers.
0
 
mcoAuthor Commented:
The thing that I was missing is that I have to setFont for all components, I thought
the top container was enough.

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now