Monday, April 30, 2012

JAVA Font Dialog Program source code using AWT and Frames | Mini Project

Following JAVA snippet (for font dialog box/drop down list) doesn't do any thing special. It just displays list of all available fonts in system as items added to Choice component. This allows user interface through which different fonts can be selected. You can use this code as a module in you java mini project in case you are developing a text editor or anything related to that.


 import java.awt.*;  
 import java.awt.event.*;  
 public class FontDialog extends Frame{  
   Choice c;  
   public FontDialog(){  
     super("Font Dialog | A.T.");  
     addWindowListener(new WindowHandlerNew());  
     setLayout(new BorderLayout(50,50));  
     setSize(800,500);  
     setFont(new Font("Times New Roman",Font.BOLD,24));  
     c=new Choice();  
     String FontList[];  
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
     FontList = ge.getAvailableFontFamilyNames();  
     for(int i = 0; i < FontList.length; i++)  
       c.addItem(FontList[i]);  
     c.setFont(new Font("Times New Roman",Font.PLAIN,16));  
     c.addItemListener(new FontChanger(this));  
     add(c,BorderLayout.NORTH);  
     setVisible(true);  
   }  
   public void paint(Graphics g){  
     g.drawString("Hello Word !!!", 300, 400);  
   }  
  public static void main(String args[]){  
    FontDialog f=new FontDialog();  
  }  
 }  
 class WindowHandlerNew implements WindowListener{  
   public void windowDeactivated(WindowEvent e){  
   }  
   public void windowActivated(WindowEvent e){  
   }  
   public void windowDeiconified(WindowEvent e){  
   }  
   public void windowIconified(WindowEvent e){  
   }  
   public void windowClosing(WindowEvent e){  
     System.exit(0);  
   }  
   public void windowClosed(WindowEvent e){  
   }  
   public void windowOpened(WindowEvent e){  
   }  
 }  
 class FontChanger implements ItemListener{  
   FontDialog f;  
   public FontChanger(FontDialog f){  
     this.f=f;  
   }  
   public void itemStateChanged(ItemEvent e){  
     String fn=f.c.getSelectedItem();  
     Font font=new Font(fn,Font.BOLD,24);  
     f.setFont(font);  
     f.repaint();  
   }  
 }  

No comments:

Post a Comment