Creating the Encoding Schemes Dialog Box


The EncodingSchemes.java file allows you to create an Encoding Schemes dialog box. This dialog box provides options to select an encoding scheme to encode the text.

Listing 8-2 shows the contents of the EncodingSchemes.java file:

Listing 8-2: The EncodingSchemes.java File
start example
 /* Imports the java.awt package classes. */ import java.awt.*; /* Imports the java.nio.charset package classes. */ import java.nio.charset.Charset; /* Imports the java.util package classes. */ import java.util.SortedMap; import java.util.Map; import java.util.Iterator; import java.util.Collection; import java.lang.Object; import java.util.Enumeration; /* Imports the java.awt.event package classes. */ import java.awt.event.*; /* Imports the javax.swing package classes. */ import javax.swing.*; import javax.swing.ButtonModel; /* class EncodingSchemes - This class creates a window that displays various encoding schemes.  Fields:    title - Creates a title label.    ok - Creates a OK button.    cancel - Creates a Cancel button.    rb - Creates the radio button.     buttonPanel - Creates a panel for button.    schemePanel - Creates a panel to display the encoding scheme.    numberPanel - Creates a panel to display the number of encoding scheme.    mainPanel - Creates a main panel.    encodingScheme - Contains the encoding scheme.    cs- Creates an object of the Charset class.     c - Creates an object of the Collection class.    Methods:    getSchemes() - This method is executed when the EncodingSchemes constructor loads.    actionPerformed() - This method is invoked when an end user clicks any button of the EncodingSchemes window.  */ public class EncodingSchemes extends JDialog implements ActionListener {    /* Declares the object of the SortedMap class. */     SortedMap sm;    /* Declares the object of the JLabel class. */    JLabel title;    /* Declares the objects of the JButton class. */    JButton ok;    JButton cancel;    /* Declares the object of the String class. */    String temp;    /* Declares the object of the ButtonGroup class. */    ButtonGroup bg;    /* Declares the object of the JRadioButton class. */    JRadioButton rb;    /* Declares the object of the Charset class. */    Charset cs;    /* Declares the object of the Collection class. */    Collection c;    /* Declares the objects of the JPanel class. */    JPanel buttonPanel;    JPanel schemePanel;    JPanel numberPanel;    JPanel mainPanel;    JScrollPane s_pane;    /* Declares the object of the Font class. */    Font f;    int s;    public EncoderDecoder ed;    /* Declares the object of the String class. */    public static String encodingScheme = "UTF-16";    /* Defines the default constructor of the EncodingSchemes class. */    public EncodingSchemes(EncoderDecoder ed)    {        this.ed = ed;       /* Sets the title of the Encoding Schemes window. */       setTitle("Encoding Schemes");       /* Sets the size of the Encoding Schemes window. */       setSize(280, 500);       /* Sets the re-sizability of the Encoding Schemes window to false. */       setResizable(false);       /* Initializes the object of the ButtonGroup class. */        bg=new ButtonGroup();       /* Initializes the object of the Font class. */       f=new Font("Verdana", Font.BOLD, 12);       /* Initializes the objects of the JPanel class. */       buttonPanel=new JPanel();       schemePanel=new JPanel();       numberPanel=new JPanel();       mainPanel=new JPanel();       /* Initializes the object of the JLabel class. */       title = new JLabel("Select the Encoding Scheme");       title.setFont(f);       /* Initializes and sets the layout of the button panel to grid layout. */        buttonPanel.setLayout(new GridLayout(1, 2));       /* Initializes and sets the layout of the number panel to border layout. */       numberPanel.setLayout(new BorderLayout());       /* Initializes and sets the layout of the main panel to border layout. */       mainPanel.setLayout(new BorderLayout());       /* Initializes the objects of the JButton class. */       ok=new JButton("OK");       cancel=new JButton("Cancel");       /* Adds the action listener events to the Ok and Cancel buttons. */       ok.addActionListener(this);       cancel.addActionListener(this);       /* Calls the getSchemes() method. */        getSchemes();       /* Adds various swing components on the panels. */       numberPanel.add(title, BorderLayout.NORTH);       buttonPanel.add(ok);       buttonPanel.add(cancel);       mainPanel.add(numberPanel, BorderLayout.NORTH);       mainPanel.add(s_pane, BorderLayout.CENTER);       mainPanel.add(buttonPanel, BorderLayout.SOUTH);       /* Adds the main panel to frame. */       getContentPane().add(mainPanel);       /*       addWindowListener - It contains a windowClosing() method.       windowClosing(): This method is called when the end user clicks the close button of the Window.        Parameter: we- Represents an object of the WindowEvent class.       Return Value: NA       */       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             dispose();          }       });     }    /*    getSchemes() - This method returns the selected encoding scheme.    Parameter - NA    Return Value - NA    */    public void getSchemes()    {       try       {          /* Retrieves the available character schemes. */          sm=cs.availableCharsets();          /* Gets the size of sorted map object. */          s=sm.size();          /* Sets the layout of the schemePanel panel to the GridLayout. */          schemePanel.setLayout(new GridLayout(s, 1));          /* Retrieves the value from the sorted model object. */          c=sm.values();          /* Creates and initializes the object of the Iterator class. */          Iterator i=c.iterator();          while(i.hasNext())          {             temp = i.next().toString();             /* Initializes the object of the JRadioButton class. */             rb = new JRadioButton(temp);             if(temp.equals("UTF-16"))             {                rb.setSelected(true);             }             /* Adds the radio button to the button group. */             bg.add(rb);             /* Adds the radio button group to the schemePanel panel. */             schemePanel.add(rb);             s_pane = new JScrollPane(schemePanel);             }          }catch(Exception e)          {             System.out.println(e);           }       }       /*       actionPerformed() - This method is called when the end user clicks the any button.       Parameters: ev  Represents an objects of the ActionEvent class that contains the details of the event.       Return Value: NA       */       public void actionPerformed(ActionEvent ev)       {          /* This section is executed when the end user clicks the OK button. */          if(ev.getSource()==ok)          {             /* Creates and initializes the object of the Enumeration class to              get the elements from the button group. */             Enumeration enum=bg.getElements();             while(enum.hasMoreElements())             {                JRadioButton button = (JRadioButton)enum.nextElement();                /* Checks the status of a radio button. */                if (button.isSelected())                {                   /* Retrieves the text from the radio button. */                   encodingScheme = button.getText();                   ed.schemeText.setText(encodingScheme);                }             }             /* Disposes the window. */             dispose();          }          /* This section is executed when the end user clicks the Cancel button. */          else if(ev.getSource()==cancel)          {             /* Disposes the window. */             dispose();          }    } } 
end example
 

Download this Listing .

In the above code, the constructor of this class creates the Encoding Schemes dialog box for the Encoder/Decoder application, as shown in Figure 8-3:

this figure shows the encoding schemes dialog box that contains a select encoding scheme radio button group. end users can select an encoding scheme from this box to encode a file.
Figure 8-3: The Encoding Schemes Dialog Box

The methods defined in the above code are:

  • getSchemes() : Retrieves the available character schemes and stores it to the sorted map. The getSchemes() method gets the size of the sorted map in the integer variable, s, and sets the layout of the schemePanel panel to sX1 grid layout. This method then retrieves the value from the sorted model object and initializes the object of the JRadioButton class. Finally, this method adds the radio button to the button group and the radio button group to the schemePanel panel.

  • actionPerformed() : Acts as an event listener and activates an appropriate class or method based on the button the end user clicks. If the OK button is clicked, the actionPerformed() method checks the status of the radio button. This method then gets the text associated with this radio button and stores it to the static variable, encodingScheme. When an end user clicks the Cancel button, the actionPerformed() method calls the dispose() method to close the Encoding Schemes window.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

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