Selecting Font


The FontChoose.java file allows an end user to select the font of the text in the document to be printed. The user interface of the FontChoose.java file appears when the end user clicks the Font menu item on the Font menu of the Printing application.

Listing 4-4 shows the FontChoose.java file:

Listing 4-4: The FontChoose.java File

start example
 /* Import javax.swing package classes */ import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.JDialog; import javax.swing.BorderFactory; /* Import java.awt package classes */ import java.awt.GraphicsEnvironment; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.FlowLayout; import java.awt.Font; /* Import javax.swing.event package classes */ import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import javax.swing.*; /* class FontChoose - This class creates a Font dialog box that enables the end  user to change the font family, size and type of the text.       Fields:        fontLabel - Contains the content of Font label       sizeLabel - Contains the content of Size label       typeLabel - Contains the content of Type label       previewLabel - Contains the content of preview       label - Contains the preview contents       fontText - Contains the selected font family name       typeText - Contains the selected font type name       sizeText - Contains the selected font size name       fontScroll - Contains the Font list       typeScroll - Contains the Type list       sizeScroll - Contains the Size list       fontList - Contains all the available font family       typeList - Contains all the available types of font style       sizeList - Contains all the available font size       ok - Creates an OK button       cancel - Creates a cancel button       Methods:       getOK() - This method returns the OK button object       getCancel() - This method returns the Cancel button object       valueChanged() - This method is invoked when an end user select the item from the List box.       font() - This method returns the font */ public class FontChoose extends JDialog implements ListSelectionListener {    /* Declare the objects of JPanel class */    JPanel pan1;    JPanel pan2;    JPanel pan3;    /* Declare the objects of JLabel class */    JLabel fontLabel;    JLabel sizeLabel;    JLabel typeLabel;    JLabel previewLabel;    /* Declare the objects of JTextField class */    JTextField label;    JTextField fontText;    JTextField sizeText;    JTextField typeText;    /* Declare the objects of JScrollPane class */    JScrollPane fontScroll;    JScrollPane typeScroll;    JScrollPane sizeScroll;    /* Declare the objects of JList class */    JList fontList;    JList sizeList;    JList typeList;    /* Declare the objects of JButton class */    JButton ok;    JButton cancel;    GridBagLayout gbl;    GridBagConstraints gbc;    /*    getOk() method - This method is invoked when end user click the OK button of the Font dialog box    parameter - NA    return value - ok    */    public JButton getOk()    {       return ok;    }    /*    getCancel() method - This method is invoked when end user click the Cancel button of the Font dialog box    parameter - NA    return value - cancel    */    public JButton getCancel()    {       return cancel;    }    /* Define the default constructor */    public FontChoose()    {       try       {          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       }catch(Exception e)       {          System.out.println("Error in setting WLAF"+e);       }       /* Set the title of the Font dialog */       setTitle("Selecting the Font");       /* Set the size of Font dialog */       setSize(300, 400);       /* Set resizable button to FALSE */       setResizable(false);       /* Initialize the object of GridBagLayout */       gbl = new GridBagLayout();       /* Set the Layout */       getContentPane().setLayout(gbl);       /* Creates an object of GridBagConstraints class */       gbc = new GridBagConstraints();       /* Initialize the Font label object and add it to the 1,1,1,1 position with WEST alignment */       gbc.gridx = 1;          gbc.gridy = 1;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       fontLabel = new JLabel("Fonts: ");       getContentPane().add(fontLabel, gbc);       /* Initialize the Size label object and add it to the 2,1,1,1 position with WEST alignment */          gbc.gridx = 2;          gbc.gridy = 1;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       sizeLabel = new JLabel("Sizes: ");       getContentPane().add(sizeLabel, gbc);       /* Initialize the Types label object and add it to the 3,1,1,1 position with WEST alignment */       gbc.gridx = 3;          gbc.gridy = 1;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       typeLabel = new JLabel("Types: ");       getContentPane().add(typeLabel, gbc);       /* Initialize the Font text field object and add it to the 1,2,1,1 position with WEST alignment */       gbc.gridx = 1;          gbc.gridy = 2;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       fontText = new JTextField("Arial", 12);       getContentPane().add(fontText, gbc);       /* Initialize the Size text field object and add it to the 2,2,1,1 position with WEST alignment */       gbc.gridx = 2;          gbc.gridy = 2;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       sizeText = new JTextField("8", 4);       getContentPane().add(sizeText, gbc);       /* Initialize the Types text field object and add it to the 3,2,1,1 position with WEST alignment */       gbc.gridx = 3;          gbc.gridy = 2;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       typeText = new JTextField("Regular", 6);       getContentPane().add(typeText, gbc);       /* Initialize the Font list object and add it to the Font scroll pane object.        Add this scroll pane object to 1,3,1,1 position with WEST alignment */       gbc.gridx = 1;          gbc.gridy = 3;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();       fontList = new JList(fonts);       fontList.setFixedCellWidth(110);                fontList.addListSelectionListener(this);       fontList.setSelectedIndex(0);       fontScroll = new JScrollPane(fontList);       getContentPane().add(fontScroll, gbc);       /* Initialize the Size list object and add it to the Size scroll pane object.        Add this scroll pane object to 2,3,1,1 position with WEST alignment */       gbc.gridx = 2;          gbc.gridy = 3;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       String[] sizes = {"8", "10", "12", "14", "16", "18", "20", "24", "28", "32", "48", "72"};       sizeList = new JList(sizes);       sizeList.setSelectedIndex(0);       sizeList.setFixedCellWidth(20);        sizeList.addListSelectionListener(this);       sizeScroll = new JScrollPane(sizeList);          getContentPane().add(sizeScroll, gbc);       /* Initialize the types list object and add it to the Types scroll pane object.        Add this scroll pane object to 3,3,1,1 position with WEST alignment. */       gbc.gridx = 3;          gbc.gridy = 3;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       String[] types = {"Regular", "Bold", "Italic", "Bold Italic"};       typeList = new JList(types);       typeList.setFixedCellWidth(60);        typeList.addListSelectionListener(this);       typeList.setSelectedIndex(0);       typeScroll = new JScrollPane(typeList);          getContentPane().add(typeScroll, gbc);       /* Initialize the preview label and add it to 1,4,3,1 position with CENTER alignment */       gbc.gridx = 1;          gbc.gridy = 4;       gbc.gridwidth = 3;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.CENTER;       pan1 = new JPanel();       pan1.setLayout(new FlowLayout());       previewLabel = new JLabel("Preview:");       pan1.add(previewLabel);       getContentPane().add(pan1, gbc);       /* Initialize the preview text field and add it to 1,5,3,1 position with CENTER alignment */       gbc.gridx = 1;          gbc.gridy = 5;       gbc.gridwidth = 3;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.CENTER;       pan2 = new JPanel();       pan2.setLayout(new FlowLayout());       label = new JTextField("AaBaCcDdeEfFgGhHjJ");       label.setEditable(false);       label.setBorder(BorderFactory.createEtchedBorder());       label.setFont(new Font("Verdana",Font.PLAIN,20));       pan2.add(label);       getContentPane().add(pan2, gbc);       /* Initialize the OK and Cancel button. Add these two buttons to the panel. Set layout of       the panel to FlowLayout. Now add this panel to the 1,6,4,1 position with CENTER alignment. */       gbc.gridx = 1;          gbc.gridy = 6;       gbc.gridwidth = 3;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.CENTER;       pan3 = new JPanel();       pan3.setLayout(new FlowLayout());       ok = new JButton("OK");       cancel = new JButton("Cancel");       pan3.add(ok);       pan3.add(cancel);       getContentPane().add(pan3, gbc);    }    /*    valueChanged() - This method is called when the user selects any manyitem from the menubar.    Parameters:   lse - a ListSelectionEvent object containing details of the event.    Return Value: NA    */    public void valueChanged(ListSelectionEvent lse)    {       try       {          /* This section is executed, when end user selects the item from Font list */          if(lse.getSource() == fontList)          {          Font f1 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),          Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));          fontText.setText(String.valueOf(fontList.getSelectedValue()));          label.setFont(f1);          }          /* This section is executed, when end user selects the item from Size list */          else if(lse.getSource() == sizeList)          {             Font f2 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),             Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));             sizeText.setText(String.valueOf(sizeList.getSelectedValue()));             label.setFont(f2);          }          /* This section is executed, when end user selects the item from Type list */          else if(lse.getSource() == typeList)          {             Font f2 = new Font(String.valueOf(fontList.getSelectedValue()),typeList.getSelectedIndex(),             Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));             typeText.setText(String.valueOf(typeList.getSelectedValue()));             label.setFont(f2);          }       }       catch(Exception nfe)       {}    }    /*    font() - This method is set the font of the file text    Parameters:   NA    Return Value: font     */    public Font font()    {       /* Create an object of Font class */       Font font = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(),       Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));       /* Return the font object */       return font;    } } 
end example

Download this listing.

The user interface of the FontChoose.java file provides the Selecting the Font dialog box. The end user can select the style, type, and size of the font from the Selecting the Font dialog box, as shown in Figure 4-7:

this figure shows the selecting the font dialog box to change the font, font size, and font type of selected text.
Figure 4-7: The Selecting the Font Dialog Box

Clicking OK or Cancel on the user interface of the Selecting the Font dialog box invokes the actionPerformed() method. The OK button sets the font of the text in the document to be printed to the selected font. The Cancel button cancels the current selection and the control returns to the user interface of the CommPrinting.java file.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net