Question

Java language

(a) Create a class HexEditor with a constructor which creates a 5x10 text area in a Frame. Also add a pull-down menu with a menu item "Load". Copy the class as the answer to this part.

- 10x File Load

(b) Create another class TestHexEditor with a main() method which creates an object anEditor of the class HexEditor and displays the frame in part (a) using setVisible(true). Copy the class as the answer to this part.

(c) Make changes to the class HexEditor so that it implements ActionListener. In the constructor of the class HexEditor, add the current object as the action listener of the "Load" menu item. Write the corresponding method actionPerformed() which displays a dialog box asking for a file name and load content of the file to the text area using FileInputStream. You can assume the file is a byte file and it exists. Copy the changed/added lines and the new method as the answers to this part.

(d) We will use the BorderLayout manager. Modify the constructor to put the text area on the left, add a label containing a space in the middle, add a new text area (with width 20) on the right, and a button at the bottom. The resulting window is showed below (after a file is loaded). Copy the changed/added lines as the answers to this part.

- 10 x File line one 2nd line endl Update hex

(e) Modify the method actionPerformed() so that when the update button is pressed, a hexidecimal version of the file is displayed on the right, in which each byte is represented by two hexidecimal digits and followed by a space. You can use Integer.toHexString('B') to convert a byte 'B' to a hexidecimal string. A sample window is shown below (where '0A' on the hex window corresponds to a newline character). Copy the changed/added lines as the answers to this part.

- 10 x File 41 42 43 0A 31 32 33 ABC 123 Update hex

(f) In the pull-down menu, add a "Save" menu item so that the original file can be replaced by the content of the text area on the left. You need to modify the method actionPerformed() to achieve this. If no file was loaded but the user directly type something on the left text area, ask for the file name using a dialog box and replace any existing file. Copy the changed/added lines as the answers to this part.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. Class HexEditor
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    
    public class HexEditor  extends javax.swing.JFrame implements ActionListener{
        // Variables declaration - do not modify
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jmItemLoad;
        private javax.swing.JTextArea txtPlainTextData;
        private javax.swing.JTextArea txtHexData;
        private javax.swing.JLabel lblSeparator;
        private javax.swing.JButton btnUpdateHex;
    
        public HexEditor() {
            initComponents();
            jmItemLoad.addActionListener(this);
        }
    
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
            this.setSize(450,300);
            txtPlainTextData = new javax.swing.JTextArea();
            txtHexData = new javax.swing.JTextArea();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jmItemLoad = new javax.swing.JMenuItem();
            lblSeparator = new javax.swing.JLabel();
            btnUpdateHex = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setResizable(true);
    
            txtPlainTextData.setColumns(10);
            txtPlainTextData.setRows(5);
            txtHexData.setColumns(20);
            txtHexData.setRows(5);
            lblSeparator.setText(" ");
            jMenu1.setText("File");
            jMenu1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            jMenu1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
    
            jmItemLoad.setText("Load");
            jMenu1.add(jmItemLoad);
            btnUpdateHex.setText("Update hex");
    
            jMenuBar1.add(jMenu1);
    
            setJMenuBar(jMenuBar1);
    
            this.add(txtPlainTextData, BorderLayout.LINE_START);
            this.add(lblSeparator, BorderLayout.CENTER);
            this.add(txtHexData,BorderLayout.LINE_END);
            this.add(btnUpdateHex,BorderLayout.PAGE_END);
    
            pack();
        }
    
    
    
        @Override
        public void actionPerformed(ActionEvent e) {
            NewJDialog getFileDialog = new NewJDialog(this, true);
            fileName = getFileDialog.showDialog();
            // File reading
            File file = new File(fileName);
            FileInputStream fStream = null;
            StringBuffer fileData = new StringBuffer("");
            StringBuffer hexData = new StringBuffer("");
            int ch;
            try
            {
                fStream = new FileInputStream(file);
                while((ch=fStream.read()) != -1)
                {
                    fileData.append((char)ch);
                    hexData.append(String.format("%02X",ch)+" ");
                }
                fStream.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // Load data to text area
            this.txtPlainTextData.setText(fileData.toString());
            this.txtHexData.setText(hexData.toString());
        }
    
        private String fileName;
    }
    
  2. Class TestHexEditor
    public class TestHexEditor {
        public static void main(String[] args) {
            HexEditor hexEditor = new HexEditor();
            hexEditor.setVisible(true);
        }
    }
    
  3. Dialog Box Class
    import javax.swing.border.Border;
    import java.awt.*;
    
    public class NewJDialog extends javax.swing.JDialog {
    
    
        public NewJDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
    
        @SuppressWarnings("unchecked")
    
        private void initComponents() {
            this.setSize(300,200);
            this.setTitle("Load File");
    
            jLabel1 = new javax.swing.JLabel();
            jtxtFile = new javax.swing.JTextField();
            jbtnLoadFile = new javax.swing.JButton();
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setResizable(false);
            jtxtFile.setColumns(30);
            jtxtFile.setText("Byte File Path");
            jLabel1.setText("Enter a file to load");
    
            jbtnLoadFile.setText("Load");
            jbtnLoadFile.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fileName = jtxtFile.getText();
                    dispose();
                }
            });
            this.add(jLabel1, BorderLayout.PAGE_START);
            this.add(jtxtFile,BorderLayout.CENTER);
            this.add(jbtnLoadFile,BorderLayout.LINE_END);
    
    
            pack();
        }
        String showDialog()
        {
            setVisible(true);
            return fileName;
        }
        private javax.swing.JButton jbtnLoadFile;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField jtxtFile;
        private String fileName;
    }
    
    

Note: I haven't added update logic in this. You will have to do that your own. Let me know if you face any issues with above class.

when you provide file name, provide fully qualified path

Screen shots

A3nY3pm70SWRAAAAAElFTkSuQmCCK6uMmjMQJ2AAAAAElFTkSuQmCC

wNbGGDkkZGlRQAAAABJRU5ErkJggg==

Add a comment
Know the answer?
Add Answer to:
Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (b) Write a class Conversion containing the following methods: (i) constructor: which builds the frame shown...

    (b) Write a class Conversion containing the following methods: (i) constructor: which builds the frame shown on the right side. The frame consists of a text field for inputting a NZD amount, a label with 10 spaces for an equivalent HKD amount, and a button to start the calculation. Declare any necessary attributes in the class and add appropriate action listeners for future use. Copy the class, including import statement(s), as the answers to this part Calculate (ii) actionPerformed :...

  • Language : JAVA Part A Create a class CompanyDoc, representing an official document used by a...

    Language : JAVA Part A Create a class CompanyDoc, representing an official document used by a company. Give it a String title and an int length. Throughout the class, use the this. notation rather than bare instance variables and method calls.   Add a default constructor. Add a parameterized constructor that takes a value for title. Use this( appropriately to call one constructor from another. Add a toString(), which returns a string with the title, and the length in parentheses. So...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

  • Java Please - Design and implement a class Triangle. A constructor should accept the lengths of...

    Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Go to the Java API and review the Rectangle class briefly. As directed below, create a...

    Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT