Question

In: Computer Science

Create a Java Swing form that will have two Buttons: (1) the first linked to an...

Create a Java Swing form that will have two Buttons: (1) the first linked to an event handler to write data from two TextFields on the form to an output file; and (2) the other with an event handler to read data from the same file as input and write output to a binary ObjectOutputStream file.

Solutions

Expert Solution

Hi, here is the solution. Go through the comments in the program. Let me know if you require any help. The program will create two files the in program root directory. output.txt is first file which is created from the data of text fields. ObjectFile.bin is the other one created from the output text created. The Write to Object Output Stream button will be disabled initially. You need to enter some text in textfields and click "Write to File" button first. once the output.txt is created the program will enable the the button.

Source & Screens: Demo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Demo
{
    // JFrame
    private  JFrame frame;
    public Demo()
    {
        // JFrame object
        frame = new JFrame("Demo");
        // Text fields
        JTextField textField1 = new JTextField(30);
        JTextField textField2 = new JTextField(30);
        // buttons
        JButton btnWriteToFile = new JButton("Write to File");
        JButton btnWriteToObjectOS = new JButton("Write to Object Output Stream");
        // action listener for buttons
        btnWriteToFile.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent actionEvent)
            {
                // File object for file output.txt
                File file = new File("output.txt");
                try
                {
                    // file write object acquired on file object
                    FileWriter fileWriter = new FileWriter(file);
                    // buffered writer object on file writer object
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    // get text from textfield 1 and write to file
                    bufferedWriter.write(textField1.getText());
                    //write new line before appending next text
                    bufferedWriter.write("\n");
                    // get text from textfield 2 and write to file
                    bufferedWriter.write(textField2.getText());
                    // closing buffered writer
                    bufferedWriter.close();
                    // notify the user using message box
                    JOptionPane.showMessageDialog(frame,"Data written to output.txt");
                    // clear text fields
                    textField1.setText("");
                    textField2.setText("");

                } catch (IOException e)
                {
                    JOptionPane.showMessageDialog(frame,"Error while writing to file");
                    e.printStackTrace();
                }
            }
        });
        btnWriteToObjectOS.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent actionEvent)
            {
                try
                {
                    // File object on output.txt for reading
                    File file = new File("output.txt");
                    // File Reader object on file object
                    FileReader fileReader = new FileReader(file);
                    // get buffered reader on file reader
                    BufferedReader bufferedReader = new BufferedReader(fileReader);
                    // FileOutputStream object on new binary file 
                    FileOutputStream fileOutputStream = new FileOutputStream("ObjectFile.bin");
                    // get object output stream object on fileOutputstream
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                    String line;
                    // read line by line from bufferedReader
                    while((line = bufferedReader.readLine())!=null)
                    {
                        // write it to new file using object outputstream
                        objectOutputStream.writeObject(line);
                    }
                    // close file streams
                    bufferedReader.close();
                    objectOutputStream.close();
                    JOptionPane.showMessageDialog(frame,"Data written to ObjectFile.bin");
                } catch (IOException e)
                {
                    JOptionPane.showMessageDialog(frame,"Error while writing to file");
                    e.printStackTrace();
                }
            }
        });
        // set size of window
        frame.setSize(new Dimension(400,200));
        // add layout to the frame
        frame.getContentPane().setLayout(new FlowLayout());
        // add components to frame
        frame.getContentPane().add(textField1);
        frame.getContentPane().add(textField2);
        frame.getContentPane().add(btnWriteToFile);
        frame.getContentPane().add(btnWriteToObjectOS);

    }
    public void show()
    {
        frame.setVisible(true);
    }
    public static void main(String [] args)
    {
        Demo demo = new Demo();
        demo.show();
    }
}
Screens: Output


Related Solutions

Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Python Create a tkinter GUI application that has two buttons on it. The first button should...
Python Create a tkinter GUI application that has two buttons on it. The first button should have in red text the word "Red" on it and the second button should have in blue text the word "Blue" on it. When the red button is clicked you should change the background color of the application window to red and when the blue button is pressed you should change the background color of the application window to blue. You should place your...
Using C#: Create a Form that consists of a PictureBox and 3 radio buttons. Assign an...
Using C#: Create a Form that consists of a PictureBox and 3 radio buttons. Assign an image of your choice to the PictureBox. Each radio button should be associated with a specific PictureBox size mode. When the user clicks on a specific radio button, the PictureBox's size mode should be changed to reflect the selection.
Using C#: Create a Form that consists of 4 checkboxes, three radio buttons, a label and...
Using C#: Create a Form that consists of 4 checkboxes, three radio buttons, a label and a button. The 4 checkboxes represent pizza toppings. The three radiobuttons should represent three sizes of pizza, namely “small”, “medium” and “large”. When the user clicks the button you need to calculate the price of the pizza. The calculation should be done as follows: count up the cost of the selected ingredients (you may assign your own prices for the ingredients) and then multiply...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
(Java) Create a new linked list from two given arrays with the greater element from each...
(Java) Create a new linked list from two given arrays with the greater element from each corresponding array element placed into the linked list. Given two arrays of varying size initialized with integers of varying values, the task is to create a new linked list using those arrays. The condition is that the greater element value from each corresponding array element will be added to the new linked list in the list position that maintains the integers in ascending order....
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT