Question

In: Computer Science

In the Gui need a file in and out writter. That also takes from in file...

In the Gui need a file in and out writter. That also takes from in file and out file. Write errors to no file found or wrong file. Clear button and Process buttons need to work.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class JFileChooserDemo extends JFrame implements ActionListener
{
private JTextField txtInFile;
private JTextField txtOutFile;
private JButton btnInFile;
private JButton btnOutFile;
private JButton btnProcess;
private JButton btnClear;
public JFileChooserDemo()
{
   Container canvas = this.getContentPane();
   canvas.setLayout(new GridLayout (3,1));
     
   canvas.add(createInputFilePanel());
   canvas.add(createOutputFilePanel());
   canvas.add(createButtonPanel());
   this.setVisible(true);
   this.setSize(800, 200);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JPanel createInputFilePanel()
{
   JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
   panel.add(new JLabel("In File"));
   txtInFile = new JTextField(60);
   panel.add(txtInFile);
   btnInFile = new JButton("In File");
   panel.add(btnInFile);
   btnInFile.addActionListener(this);
   return panel;
}
  
public JPanel createOutputFilePanel()
{
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));  
panel.add(new JLabel("Out File"));
txtOutFile = new JTextField(58);
panel.add(txtOutFile);
btnOutFile = new JButton("Out File");
panel.add(btnOutFile);
btnOutFile.addActionListener(this);
return panel;   
}
public JPanel createButtonPanel()
{
   JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
   btnProcess =new JButton("Process");
   btnProcess.addActionListener(this);
   panel.add(btnProcess);
  
   btnClear =new JButton("Clear");
   btnClear.addActionListener(this);
   panel.add(btnClear);
   return panel;
}
public static void main(String[] args)
{
   new JFileChooserDemo();
   Scanner file = null;
   PrintWriter fout= null;
           try {
               file = new Scanner(new File("numbers.txt"));
               fout = new PrintWriter("TotalSums.txt");
               while(file.hasNext())
               {
                  
                   @SuppressWarnings("resource")
                   Scanner line = new Scanner(file.nextLine());
   int totalSum = 0;
   while (line.hasNext()) {
   int number = line.nextInt();
   totalSum += number;
   fout.print(number);
   if (line.hasNext())
   {
   fout.print("+");
   }
   }
   fout.println("=" + totalSum);
   }
           }
           catch (FileNotFoundException e)
           {
              
               System.out.println("NOT FOUND");
           }
           finally
           {
               if(fout!=null)fout.close();
           }
          
           if(file!=null)file.close();
          
       }
      
      
  
  
public void clearInput()
{
txtInFile.setText(""); txtOutFile.setText("");

}
@Override
public void actionPerformed(ActionEvent e)
{
   if(e.getSource() == btnInFile)
   {
       JFileChooser jfcInFile = new JFileChooser();
       if(jfcInFile.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
       {
           File inFile = jfcInFile.getSelectedFile();
           txtInFile.setText(inFile.getAbsolutePath());
       }
       else
       {
          
       }
   }
   if(e.getSource() == btnProcess)
   {
       File file = new File(txtInFile.getText());
       try
       {
           Scanner fin = new Scanner(file);
       }
       catch (FileNotFoundException e1)
       {
          
           e1.printStackTrace();
       }
   }
  
}
}

Solutions

Expert Solution

Please find the code below:

JFileChooserDemo.java

package classess6;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class JFileChooserDemo extends JFrame implements ActionListener
{
   private JTextField txtInFile;
   private JTextField txtOutFile;
   private JButton btnInFile;
   private JButton btnOutFile;
   private JButton btnProcess;
   private JButton btnClear;
   public JFileChooserDemo()
   {
       Container canvas = this.getContentPane();
       canvas.setLayout(new GridLayout (3,1));

       canvas.add(createInputFilePanel());
       canvas.add(createOutputFilePanel());
       canvas.add(createButtonPanel());
       this.setVisible(true);
       this.setSize(800, 200);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public JPanel createInputFilePanel()
   {
       JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
       panel.add(new JLabel("In File"));
       txtInFile = new JTextField(60);
       panel.add(txtInFile);
       btnInFile = new JButton("In File");
       panel.add(btnInFile);
       btnInFile.addActionListener(this);
       return panel;
   }

   public JPanel createOutputFilePanel()
   {
       JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
       panel.add(new JLabel("Out File"));
       txtOutFile = new JTextField(58);
       panel.add(txtOutFile);
       btnOutFile = new JButton("Out File");
       panel.add(btnOutFile);
       btnOutFile.addActionListener(this);
       return panel;   
   }
   public JPanel createButtonPanel()
   {
       JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
       btnProcess =new JButton("Process");
       btnProcess.addActionListener(this);
       panel.add(btnProcess);

       btnClear =new JButton("Clear");
       btnClear.addActionListener(this);
       panel.add(btnClear);
       return panel;
   }
   public static void main(String[] args)
   {
       new JFileChooserDemo();
       Scanner file = null;
       PrintWriter fout= null;
       try {
           file = new Scanner(new File("numbers.txt"));
           fout = new PrintWriter("TotalSums.txt");
           while(file.hasNext())
           {

               @SuppressWarnings("resource")
               Scanner line = new Scanner(file.nextLine());
               int totalSum = 0;
               while (line.hasNext()) {
                   int number = line.nextInt();
                   totalSum += number;
                   fout.print(number);
                   if (line.hasNext())
                   {
                       fout.print("+");
                   }
               }
               fout.println("=" + totalSum);
           }
       }
       catch (FileNotFoundException e)
       {

           System.out.println("NOT FOUND");
       }
       finally
       {
           if(fout!=null)fout.close();
       }

       if(file!=null)file.close();

   }


   public void clearInput()
   {
       txtInFile.setText(""); txtOutFile.setText("");

   }
   @Override
   public void actionPerformed(ActionEvent e)
   {
       if(e.getSource() == btnInFile)
       {
           JFileChooser jfcInFile = new JFileChooser();
           if(jfcInFile.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
           {
               File inFile = jfcInFile.getSelectedFile();
               txtInFile.setText(inFile.getAbsolutePath());
           }
           else
           {

           }
       }

       if(e.getSource() == btnClear)
       {
           txtInFile.setText("");
           txtOutFile.setText("");

       }

       if(e.getSource() == btnOutFile)
       {
           JFileChooser jfcInFile = new JFileChooser();
           if(jfcInFile.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
           {
               File inFile = jfcInFile.getSelectedFile();
               txtOutFile.setText(inFile.getAbsolutePath());
           }
           else
           {

           }
       }

       if(e.getSource() == btnProcess)
       {
           File file = new File(txtInFile.getText());
           try{
               Scanner fin = new Scanner(file);
               File writerFile = new File(txtOutFile.getText());
               try{
                   PrintWriter writer = new PrintWriter(writerFile);
                   while(fin.hasNextLine()){
                       writer.println(fin.nextLine());
                   }
                   JOptionPane.showMessageDialog(null, "Data exported successfully");
                   writer.close();
               }catch(FileNotFoundException e2){
                   JOptionPane.showMessageDialog(null, "Error!!! File not found+ "+writerFile.getName());  
               }
           }
           catch (FileNotFoundException e1)
           {
               JOptionPane.showMessageDialog(null, "Error!!! File not found+ "+file.getName());
               e1.printStackTrace();
           }
       }

   }
}


Related Solutions

JAVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words....
JAVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will...
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Hi, I need the Excel Formula for the below. Thanks much. A business takes out a...
Hi, I need the Excel Formula for the below. Thanks much. A business takes out a 10-year loan for $250,000 at 5.3% interest compounded monthly. What are the formulas to calculate how much interest the business will pay in the first year and how much the business will repay toward the principal? What are the resulting values? Calculate the total cost of the loan in terms of the total interest paid through the l0 years of the loan
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
A family takes out a mortgage for $277,400.00 from the local bank. The loan is for...
A family takes out a mortgage for $277,400.00 from the local bank. The loan is for 30 years of monthly payments at a 4.32% APR (monthly compounding). What will the family’s balance be on the mortgage after 6.00 years?
For this question you will need to upload a file. It is question 3 from the...
For this question you will need to upload a file. It is question 3 from the PDF file I sent you. Please show your work. The following information is given about the market for a normal good. Demand: P = 150-2Qd Supply P = 20+ 0.5 Qs What is the quantity demanded at a price of $80? (1.5 mark) What is the quantity supplied at a price of $80? (1.5 mark) At a price of $80 the market is not...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT