Question

Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

Swing File Adder:

Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program should not be able to blow up and make good use of exception handling. Handling and providing pop up messages for such conditiions as: input file not found, output file cannot be created, and bad data in the input file, should all be implemented.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Textbox for using Swing:

import javax.swing.*;  

import java.awt.event.*;  

public class TextFieldExample implements ActionListener{  

JTextField tf1,tf2;  

JButton b1;  

TextFieldExample(){  

        JFrame f= new JFrame();        

  tf1=new JTextField();     

     tf1.setBounds(50,50,150,20);        

  tf2=new JTextField();         

tf2.setBounds(50,100,150,20);

tf2.setEditable(false);         

  b1=new JButton("+");        

  b1.setBounds(50,200,50,50);        

  b2=new JButton("-");   

b2.setBounds(120,200,50,50);         

b1.addActionListener(this);   

b2.addActionListener(this);       

   f.add(tf1);

f.add(tf2);

f.add(b1);

f.setSize(300,300);     

     f.setLayout(null);        

  f.setVisible(true);    

  }           

  public void actionPerformed(ActionEvent e) {         

String s1=tf1.getText();        

   if(e.getSource()==b1){             

   tf2.setText(s1);     

   }

   }  

public static void main(String[] args) {    

  new TextFieldExample();

} }  

InputFile using JFIleChooser:

  1. import javax.swing.*;    
  2. import java.awt.event.*;    
  3. import java.io.*;    
  4. public class FileChooserExample extends JFrame implements ActionListener{    
  5. JMenuBar mb;    
  6. JMenu file;    
  7. JMenuItem open;    
  8. JTextArea ta;    
  9. FileChooserExample(){    
  10. open=new JMenuItem("Open File");    
  11. open.addActionListener(this);            
  12. file=new JMenu("File");    
  13. file.add(open);             
  14. mb=new JMenuBar();    
  15. mb.setBounds(0,0,800,20);    
  16. mb.add(file);              
  17. ta=new JTextArea(800,800);    
  18. ta.setBounds(0,20,800,800);              
  19. add(mb);    
  20. add(ta);              
  21. }    
  22. public void actionPerformed(ActionEvent e) {    
  23. if(e.getSource()==open){    
  24.     JFileChooser fc=new JFileChooser();    
  25.     int i=fc.showOpenDialog(this);    
  26.     if(i==JFileChooser.APPROVE_OPTION){    
  27.         File f=fc.getSelectedFile();    
  28.         String filepath=f.getPath();    
  29.         try{  
  30.         BufferedReader br=new BufferedReader(new FileReader(filepath));    
  31.         String s1="",s2="";                         
  32.         while((s1=br.readLine())!=null){    
  33.         s2+=s1+"\n";    
  34.         }    
  35.         ta.setText(s2);    
  36.         br.close();    
  37.         }catch (Exception ex) {ex.printStackTrace();  }                 
  38.     }    
  39. }    
  40. }          
  41. public static void main(String[] args) {    
  42.     FileChooserExample om=new FileChooserExample();    
  43.              om.setSize(500,500);    
  44.              om.setLayout(null);    
  45.              om.setVisible(true);    
  46.              om.setDefaultCloseOperation(EXIT_ON_CLOSE);    
  47. }    
  48. }  
Add a comment
Know the answer?
Add Answer to:
Swing File Adder: Build a GUI that contains an input file, text box and Infile button....
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
  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • The question asks you to assume that your input file is an English text that may...

    The question asks you to assume that your input file is an English text that may contain any character that appears on keyboard of a computer. The task is to read each character from the input file and after encrypting it by adding an integer value n (0 < n < 128) to the character, to write the encrypted character to output file. We are supposed to use command line arguments and utilize argc, and argv parameters in main function...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one...

    Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one receives the length of the sequence, and the second receive the sequence (each number separated by a comma). There may, or may not, be space in between the comma and the number element. Provide a clear button that has an event listener attached to it. When pressed, the two text fields are emptied. The event listener should be defined and implemented as an anonymous...

  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...

    /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate * a course average and a letter grade. In order to calculate * the average and the letter grade, a GradeCalculator must store * two essential pieces of data: the number of grades and the sum * of the grades. Therefore these are declared as object properties * (instance variables). * Each time calcAverage (grade) is called, a new grade is added to *...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • The input file should contain (in order): the weight (a number greater than zero and less...

    The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. The program should also write to an output file (rather than the console, as in Horstmann). So you should also prompt the user for the name of the output file, and then print your results to an output file...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

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