Java InstantCode. Developing Applications Using Java NIO
Creating the User Interface for the Encoder/Decoder Application
The EncoderDecoder.java file helps create a user interface with a set of labels, text box, text areas, and buttons for the Encoder/Decoder application.
Listing 8-1 shows the contents of the EncoderDecoder.java file:
Listing 8-1: The EncoderDecoder.java File
|
/* Imports java.nio package classes. */ import java.nio.*; import java.nio.charset.*; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.*; import java.nio.channels.FileChannel; import java.nio.charset.UnsupportedCharsetException; /* Imports java.io package classes. */ import java.io.*; import java.io.IOException; /* Imports javax.swing package classes. */ import javax.swing.*; import javax.swing.JOptionPane; /* Imports java.awt package classes. */ import java.awt.*; /* Imports java.awt.event package classes. */ import java.awt.event.*; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; /* class EncoderDecoder - This class creates a GUI for the Encoder/Decoder application. This class also provides the methods to encode the text and decode the encoded text into a readable format. Fields: pan1 - Creates a panel that can contain a text box and two buttons. titleLabel - Creates an Encoder/Decoder Application label. selectLabel - Creates a select file label. encodeLabel - Creates an encoded text label. decodeLabel - Creates a decoded text label. selectText - Creates a select file text field. encodeArea - Creates an Encoded Text text area. decodeArea - Creates a Decoded Text text area. encodePane - Creates a scroll pane for the Encoded Text text area. decodePane - Creates a scroll pane for the Decoded Text text area. browseButton - Creates a Browse button. encodeButton - Creates an Encode button. selectButton - Creates a Select Encoding Scheme button. resetButton - Creates a Reset button. decodeButton - Creates a Decode button. closeButton - Creates a Close button. file_text - Represents a string that stores the contents of a file. file - Represents a file. jfc - Creates a file chooser dialog box. fin - Creates a file input stream. fchan - Creates a file channel. buff_in - Stores the content of the opened files. buff_out - Stores the content of the encoded file that needs to be further encrypted in the hexadecimal format. bbuf - Stores the encoded bytes. cbuf - Stores the decoded bytes. fsize - Contains the size of the file. decoder - Creates a decoder that decodes the encoded text. encoder - Creates an encoder that encodes the text. charset - Creates a charset object. encScheme - Represents an object of the EncodingSchemes class. Methods: keyTyped() - This method is invoked when an end user types any text in the Text to Encode text area. actionPerformed() - This method is invoked when an end user clicks the any button of the Encoder/Decoder application. open() - This method is invoked to open an existing text file that the end user wants to encode. encode() - This method is invoked to encode the text. decode() - This method is invoked to decode the encoded text. main() - This method creates the main window of the application and displays it. */ public class EncoderDecoder extends JDialog implements ActionListener, KeyListener { /* Declares the object of the JPanel class. */ JPanel pan1; /* Declares the objects of the JLabel class. */ JLabel titleLabel; JLabel textLabel; JLabel selectLabel; JLabel encodeLabel; JLabel decodeLabel; /* Declares the object of the JTextField class. */ JTextField selectText; JTextField schemeText; /* Declares the objects of the JTextArea class. */ JTextArea encodeArea; JTextArea decodeArea; JTextArea textArea; /* Declares the objects of the JScrollPane class. */ JScrollPane encodePane; JScrollPane decodePane; JScrollPane textPane; /* Declares the objects of the JButton class. */ JButton browseButton; JButton encodeButton; JButton selectButton; JButton resetButton; JButton decodeButton; JButton closeButton; /* Declares the object of the GridBagLayout class. */ GridBagLayout gbl; /* Declares the object of the GridBagConstraints class. */ GridBagConstraints gbc; /* Declares the objects of the String class. */ String str; String file_text; /* Declares the object of the File class. */ File file; /* Declares the object of the JFileChooser class. */ JFileChooser jfc; /* Declares the object of the FileInputStream class. */ FileInputStream fin; /* Declares the object of the FileChannel class. */ FileChannel fchan; /* Declares the objects of the ByteBuffer class. */ ByteBuffer buff_in; ByteBuffer buff_out; ByteBuffer bbuf; /* Declares the object of the CharBuffer class. */ CharBuffer cbuf; /* Declares the object of the CharsetDecoder class. */ CharsetDecoder decoder; /* Declares the object of the CharsetEncoder class. */ CharsetEncoder encoder; /* Declares the object of the Charset class. */ Charset charset; /* Declares the object of the EncodingSchemes class. */ public EncodingSchemes encScheme; long fsize; /* Defines the default constructor of the EncoderDecoder class. */ public EncoderDecoder() { /* Sets the title of the Encoder/Decoder application. */ setTitle("Encoder / Decoder Application"); /* Sets the size of the Encoder/Decoder application. */ setSize(497,640); /* Sets the visibility of the Encoder/Decoder application to true. */ setVisible(true); /* Sets the re-sizability of the Encoder/Decoder application to false. */ setResizable(false); /* Initializes the object of the GridBagLayout class. */ gbl = new GridBagLayout(); /* Sets the Layout */ getContentPane().setLayout(gbl); /* Creates an object of the GridBagConstraints class. */ gbc = new GridBagConstraints(); /* Initializes the titleLabel object and adds it to the 0, 0, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; titleLabel = new JLabel(" Encoder/Decoder Application"); titleLabel.setFont(new Font("Verdana", Font.BOLD, 20)); getContentPane().add(titleLabel, gbc); /* Initializes and adds a separator to the 0, 1, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the selectLabel object and adds it to the 0, 2, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; selectLabel = new JLabel("Select File to Encode:"); getContentPane().add(selectLabel, gbc); /* Initializes and adds a separator to the 0, 3, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the selectText text field and adds it to the 0, 4, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; selectText = new JTextField(30); selectText.setFont(new Font("Verdana", Font.PLAIN, 12)); getContentPane().add(selectText, gbc); /* Initializes the browse object and adds it to the 1, 4, 1, 1 position with CENTER alignment. */ gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; browseButton = new JButton(" Browse "); browseButton.addActionListener(this); getContentPane().add(browseButton, gbc); /* Initializes the reset object and adds it to the 2, 4, 1, 1 position with EAST alignment. */ gbc.gridx = 2; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.EAST; resetButton = new JButton(" Reset "); resetButton.addActionListener(this); getContentPane().add(resetButton, gbc); /* Initializes and adds a separator to the 0, 5, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the selectText and adds it to the 0, 6, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; schemeText = new JTextField(15); schemeText.setEditable(false); schemeText.setText(encScheme.encodingScheme); schemeText.setFont(new Font("Verdana", Font.PLAIN, 12)); getContentPane().add(schemeText, gbc); /* Initializes the selectButton object and adds it to the 1, 6, 2, 1 position with WEST alignment. */ gbc.gridx = 1; gbc.gridy = 6; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; selectButton = new JButton(" Select Encoding Scheme "); selectButton.addActionListener(this); getContentPane().add(selectButton, gbc); /* Initializes and adds a separator to the 0, 7, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the encodeLabel object and adds it to the 0, 8, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 8; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; textLabel = new JLabel("Text to Encode:"); getContentPane().add(textLabel, gbc); /* Initializes and adds a separator to the 0, 9, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 9; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the textArea and textPane objects. Next, this textArea adds to the textPane and adds it to the 0, 10, 3, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 10; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; textArea = new JTextArea(7, 44); textArea.addKeyListener(this); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.setFont(new Font("Verdana", Font.PLAIN, 12)); textPane = new JScrollPane(textArea); getContentPane().add(textPane, gbc); /* Initializes and adds a separator to the 0, 11, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 11; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the encodeButton object and adds to it to the 1, 12, 2, 1 position with WEST alignment. */ gbc.gridx = 1; gbc.gridy = 12; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.EAST; encodeButton = new JButton(" Encode "); encodeButton.setEnabled(false); encodeButton.addActionListener(this); getContentPane().add(encodeButton, gbc); /* Initializes and adds a separator to the 0, 13, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 13; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the encodeLabel object and adds it to the 0, 14, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 14; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; encodeLabel = new JLabel("Encoded Text:"); getContentPane().add(encodeLabel, gbc); /* Initializes and adds a separator to the 0, 15, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 15; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the encodeArea and encodePane objects. Next, this encodeArea adds to the encodePane and adds it to the 0, 16, 3, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 16; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; encodeArea = new JTextArea(7, 44); encodeArea.setEditable(false); encodeArea.addKeyListener(this); encodeArea.setLineWrap(true); encodeArea.setWrapStyleWord(true); encodeArea.setFont(new Font("Verdana", Font.PLAIN, 12)); encodePane = new JScrollPane(encodeArea); getContentPane().add(encodePane, gbc); /* Initializes and adds a separator to the 0, 17, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 17; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the decodeButton object and adds to it to the 0, 18, 2, 1 position with EAST alignment. */ gbc.gridx = 1; gbc.gridy = 18; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.EAST; decodeButton = new JButton(" Decode "); decodeButton.setEnabled(false); decodeButton.addActionListener(this); getContentPane().add(decodeButton, gbc); /* Initializes and adds a separator to the 0, 19, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 19; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; getContentPane().add(new JSeparator(), gbc); /* Initializes the decodeLabel object and adds it to the 0, 20, 1, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 20; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.WEST; decodeLabel = new JLabel("Decoded Text:"); getContentPane().add(decodeLabel, gbc); /* Initializes and adds a separator to the 0, 21, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 21; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the decodeArea and decodePane objects. Next, this decodeArea adds to the decodePane and adds it to the 0, 22, 3, 1 position with WEST alignment. */ gbc.gridx = 0; gbc.gridy = 22; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; decodeArea = new JTextArea(7, 44); decodeArea.setEditable(false); decodeArea.setLineWrap(true); decodeArea.setWrapStyleWord(true); decodeArea.setFont(new Font("Verdana",Font.PLAIN,12)); decodePane = new JScrollPane(decodeArea); getContentPane().add(decodePane, gbc); /* Initializes and adds a separator to the 0, 23, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 23; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; getContentPane().add(new JSeparator(), gbc); /* Initializes the closeButton object and adds to it to the 0, 24, 3, 1 position with CENTER alignment. */ gbc.gridx = 0; gbc.gridy = 24; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.anchor = GridBagConstraints.CENTER; closeButton = new JButton(" Close "); closeButton.addActionListener(this); getContentPane().add(closeButton, gbc); /* addWindowListener - It contains a windowClosing() method. windowClosing: It is called when the end user clicks the cancel button of the Window. It closes the main window. Parameter: we- Represents the object of the WindowEvent class. Return Value: NA */ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); /* Initializes the object of the EncodingSchemes class. */ encScheme = new EncodingSchemes(this); } /* keyTyped() - This method is called when the end user enters any text in the Encoded Text text area. Parameters: ke - Represent an object of the KeyEvent class that contains the details of the event. Return Value: NA */ public void keyTyped(KeyEvent ke) { if(ke.getSource()== textArea) { /* Enables the Encode button. */ encodeButton.setEnabled(true); } } /* Defines the abstract methods defined in the KeyListener interface. */ public void keyPressed(KeyEvent ke){} public void keyReleased(KeyEvent ke){} /* actionPerformed() - This method is called when the end user clicks the any button. Parameters: ev Represents an object 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 Select Encoding Schemes button. */ if(ev.getSource() == selectButton) { /* Sets the visibility of the Encoding Schemes frame to true. */ encScheme.setVisible(true); } /* This section is executed when the end user clicks the Browse button. */ else if(ev.getSource() == browseButton) { selectText.setText(""); try { /* Initializes the object of the JFileChooser class. */ jfc = new JFileChooser(); /* Sets the file selection mode to FILES_ONLY. */ jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); /* Creates and sets the new file filter for selecting the text files*/ jfc.setFileFilter(new javax.swing.filechooser.FileFilter() { /* accept() - This method allows the user to select only text files from the file chooser dialog box. Parameter: f - Represents a file object. Return Value: boolean */ public boolean accept(File f) { if (f.isDirectory()) { return true; } /* Returns the file name */ String name = f.getName(); /* Checks whether the file name contains .txt or not */ if (name.endsWith(".txt") name.endsWith(".TXT")) { return true; } return false; } /* getDescription() - This method sets the file description. ] Parameter - NA Returns Value - String */ public String getDescription() { return ".txt"; } }); /* Displays the Open dialog box. */ jfc.showOpenDialog(this); /* Gets the file from the selected location. */ file = jfc.getSelectedFile(); if(file==null) { } else { /* Checks the file object is a File type or Directory type. */ if(file.isFile()) { /* Gets the absolute path of the file. */ str = file.getAbsolutePath(); /* Sets the string of absolute path to the Select File to Encode text field. */ selectText.setText(str); /* Calls the open() method. */ open(); } else { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(null, "You have selected an invalid file format!", "Error", _ JOptionPane.WARNING_MESSAGE); } } } catch(Exception e) { System.out.println("Error" + e); } } /* This section is executed when the end user clicks the Encode button. */ else if(ev.getSource() == encodeButton) { if (textArea.getText().equals("")) { /* Displays an Error message dialog box. */ JOptionPane.showMessageDialog(this,"You have selected an empty file!", "Error", JOptionPane.WARNING_MESSAGE); } else { /* Calls the encode() method. */ encode(); /* Enables the Decode button. */ decodeButton.setEnabled(true); /* Disables the Encode button. */ encodeButton.setEnabled(false); } } /* This section is executed when the end user clicks the Decode button. */ else if(ev.getSource()==decodeButton) { if (encodeArea.getText().equals("")) { /* Displays an Error dialog box. */ JOptionPane.showMessageDialog(this, "The file has no content", "Select another File", JOptionPane.WARNING_MESSAGE); } else { /* Calls the decode() method. */ decode(); /* Enables the Encode button. */ encodeButton.setEnabled(true); /* Disables the Decode button. */ decodeButton.setEnabled(false); } } /* This section is executed when the end user clicks the Reset button. */ else if(ev.getSource() == resetButton) { if(textArea.getText().equals("") && encodeArea.getText().equals("") && decodeArea.getText().equals("") && selectText.getText().equals("")) { } else { /* Clears all the text areas, text field, byte buffer and char buffer. */ textArea.setText(""); encodeArea.setText(""); decodeArea.setText(""); selectText.setText(""); buff_in.clear(); bbuf.clear(); cbuf.clear(); /* Disables the Encode button. */ encodeButton.setEnabled(false); /* Disables the Decode button. */ decodeButton.setEnabled(false); } } /* This section is executed when the end user clicks the Close button. */ else if(ev.getSource()==closeButton) { System.exit(0); } } /* open() - This method is invoked when the end user clicks the Browse button to open the file. Parameter - NA Return Value - NA */ public void open() { try { /* Initializes the object of the FileInputStream class. */ fin = new FileInputStream(file); /* Gets the file channel from the file input stream. */ fchan = fin.getChannel(); /* Gets the size of the file. */ fsize = fchan.size(); /* Allocates the size of the Buffer. */ buff_in = ByteBuffer.allocate((int)fsize); /* Reads the buffer from the channel. */ fchan.read(buff_in); /* Rewinds the data in the buffer. */ buff_in.rewind(); /* Stores the contents of the buff_in buffer to a string. */ file_text = new String(buff_in.array()); if(file_text.equals("")) { /* Displays an Error dialog box. */ JOptionPane.showMessageDialog(null, "The file has no content", "Select another File", JOptionPane.WARNING_MESSAGE); } else { /* Displays the content stored in the file_text string into Encoded Text text area. */ textArea.setText(file_text); /* Enables the Encode button. */ encodeButton.setEnabled(true); } /* Closes the file channel. */ fchan.close(); /* Closes the file input stream. */ fin.close(); } catch(IOException ioe) { System.err.println("I/O Error on Open"); } catch(UnsupportedCharsetException e) { System.err.println("The File cannot be encoded"); } } /* encode() - This method is invoked when the end user clicks the Encode button to encode the specified text or a file. Parameter - NA Return Value - NA */ public void encode() { try { /* Initializes the object of the Charset class and sets the encoding scheme. */ charset = Charset.forName(encScheme.encodingScheme); /* Initializes the object of the CharsetEncoder class. */ encoder = charset.newEncoder(); /* Gets the text from the Encoded Text text area. */ String str = textArea.getText(); /* Encodes the text and stores the encoded text in the byte buffer. */ bbuf = encoder.encode(CharBuffer.wrap(str)); buff_out = encoder.encode(CharBuffer.wrap(str)); /* Creates and initializes the object of the StringBuffer class. */ StringBuffer sb = new StringBuffer(); for (int j = 0; buff_out.hasRemaining(); j++) { /* Converts the bytes into hexadecimal string. */ int b = buff_out.get(); int ival = ((int) b) & 0xff; char c = (char) ival; sb.append(Integer.toHexString (ival)); } String content = sb.toString(); /* Clears the buff_out byte buffer. */ buff_out.clear(); /* Displays the encoded text in the Encoded Text text area. */ encodeArea.setText(content); } catch(Exception e) { System.out.println(e); } } /* decode() - This method is invoked when the end user clicks the Decode button to decode the encoded text into readable format. Parameter - NA Return Value - NA */ public void decode() { try { /* Initializes the object of the CharsetDecoder class. */ decoder = charset.newDecoder(); /* Decodes the encoded text into a readable form and stores it into a character buffer. */ cbuf = decoder.decode(bbuf); /* Converts the data stored in cbuf to a string. */ String s = cbuf.toString(); /* Displays the decoded text into the Decoded Text text area. */ decodeArea.setText(s); /* Clears the byte and char buffers. */ bbuf.clear(); cbuf.clear(); } catch(Exception e) { System.out.println(e); } } /* main() - This method creates the main window of the user interface and displays it. Parameters: args[] - Contains any command line arguments passed. Return Value: NA */ public static void main(String[] args) { try { /* Sets the look and feel of the Encoder/Decoder application to window look and feel. */ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch(Exception e) { System.out.println("Unknown Look and Feel." + e); } /* Creates and initializes the object of the EncoderDecoder class. */ EncoderDecoder ed = new EncoderDecoder(); /* Displays the main window of the Encoder/Decoder application. */ ed.show(); } }
|
Download this Listing .
In the above code, the main() method creates an instance of the EncoderDecoder class. This class generates the main window of the Encoder/Decoder application, as shown in Figure 8-2:
To open an existing text file, end users can use the Browse button in the user interface. The Text to Encode text area allows end users to specify the text that is to be encoded. The Encoded Text text area displays the encoded text whereas the Decoded Text text area displays the decoded text.
The methods defined in the above code are:
-
keyTyped() : Acts as an event listener and activates an appropriate method based on the text that is to be encoded. When an end user specifies the text in the Text to Encode text area, this method calls the setEnabled() method to enable the Encode button.
-
actionPerformed() : Acts as an event listener and activates an appropriate class or method based on the button an end user clicks. If the Browse button is clicked, the actionPerformed() method initializes the object of the JFileChooser class and sets the file selection mode to FILES_ONLY. This method then creates and adds a file filter using the JFileChooser object to select only text files. The actionPerformed() method calls the showOpenDialog() method of the JFileChooser class to display the Open dialog box. Next, the actionPerformed() method retrieves the path of the selected file from the file chooser dialog box, displays the path in the Select File to Encode text field, and calls the open() method. If the end user clicks the Reset button, the actionPerformed() method clears the text displayed in the text box and text areas. This method also calls the clear() method of the ByteBuffer class to clear the byte and char buffers. If the end user clicks the Select Encoding Scheme button, the actionPerformed() method calls the setVisible() method of the JFrame class to display the Encoding Schemes window. When the end user clicks the Encode button, the actionPerformed() method calls the encode() method to encode the specified text that is displayed in the Encoded Text text area. If an end user clicks the Decode button, the actionPerformed() method calls the decode() method to decode and display the decoded text in the Decoded Text text area.
-
open() : Initializes the object of the FileInputStream class and gets the file channel from the file input stream. The open() method gets the size of the file and allocates that size to the byte buffer. The open() method then reads the buffer from the channel and rewinds the buffered data. Next, the open() method stores the contents of the byte buffer to a string and sets it to the Text to Encode text area.
-
encode() : Initializes the object of the Charset and CharsetEncoder classes, and sets the encoding scheme. The encode() method gets the text from the Text to Encode text area, encodes it, and stores the encoded text in the byte buffer. The open() method then creates and initializes the object of the StringBuffer class, converts the byte to a hexadecimal string, and displays this string in the Encoded Text text area.
-
decode() : Initializes the object of the CharsetDecoder class. This method decodes the encoded text into readable form and stores it in a character buffer. The decode() method then converts the data stored in the char buffer to a string and displays the string in the Decoded Text text area.
Категории