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 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 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. Here is what I have so far.

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.util.Scanner;

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();
  
}
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();
       }
   }
  
}
}

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 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...

  • Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...

    Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...

  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • Why when executed my frame does not contain what i have added to it? Here is...

    Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener {    /**    *    */       JLabel name = new JLabel("Name: ");    JLabel address = new JLabel("Address: ");    JLabel phone = new JLabel("Phone: ");    JLabel email = new JLabel("Email: ");...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • tart from the following code, and add Action Listener to make it functional to do the...

    tart from the following code, and add Action Listener to make it functional to do the temperature conversion in the direction of the arrow that is clicked. . (Need about 10 lines) Note: import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import java.text.DecimalFormat; public class temperatureConverter extends JFrame { public static void main(String[] args) {     JFrame frame = new temperatureConverter();     frame.setTitle("Temp");     frame.setSize(200, 100);     frame.setLocationRelativeTo(null);     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setVisible(true); }    public temperatureConverter() {     JLabel lblC = new...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; 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.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • Add appropriate descriptive comments to each line of code in the project explaining why the code...

    Add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. public class FutureValueFrame extends JFrame { private JTextField investmentField; private JTextField interestRateField; private JComboBox yearsComboBox; private JList futureValueList; private DefaultListModel futureValueModel; public FutureValueFrame() { initComponents(); } private void initComponents() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { System.out.println(e); } setTitle("Future Value Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationByPlatform(true); investmentField = new JTextField(); interestRateField = new JTextField();...

  • It's not showing anything. I've tried many things but only a small window pops up. Below...

    It's not showing anything. I've tried many things but only a small window pops up. Below is my code so far. It's creating a Mortgage Calculator with GUI. Having user input the principle, term, and rate. ************************************************************************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Mortgage extends JFrame implements ActionListener { //declaring components private JTextField principle, rate, terms, result; private JButton calculate, reset, exit;       //constructor    public Mortgage() { setLayout(new GridLayout(0,2)); //adding labels and textfields add(new JLabel("Principle:")); principle...

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