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

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

  • 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 a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

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