Current location: Hot Scripts Forums » Programming Languages » Everything Java » Submit button data written to a .txt file


Submit button data written to a .txt file

Reply
  #1 (permalink)  
Old 08-08-07, 04:41 PM
ziul ziul is offline
New Member
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Submit button data written to a .txt file

I have created an application using Swing that takes data that an user enters into textfields and once they press the submit button, the entered data is written to an external text file (program.txt), which I have placed in the same directory as the application.

Application1.jpg
Application2.jpg

I can then open my application and click 'Display Data' under my Functions menu selection and view the results in that window through a TextArea with a scrollpane, pulling the data from the text file (program.txt) that the data submitted was written to.

Application3.jpg

I am having trouble and have tried various approaches with my code and any help here would be appreciated by someone more knowledgeable.

Here is my code:

Java Code:
  1. import java.awt.*; //declare packages needed
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.JMenuBar;
  7.  
  8.  
  9. public class FinalProgram extends JFrame
  10.     implements ActionListener {
  11.  
  12. static JFrame frame;
  13.  
  14.   public void actionPerformed (ActionEvent e) {
  15.  
  16.       String answer = (e.getActionCommand());
  17.       
  18.         if (answer == "Get Data") {
  19.        
  20.         JInternalFrame iFrame = new JInternalFrame("Get Data . ", false, true, false, false);
  21.         iFrame.setLayout(new GridLayout(8,2));
  22.        
  23.        
  24.        
  25.         JLabel label1 = new JLabel("First Name");
  26.         JLabel label2 = new JLabel("Last Name");
  27.         JLabel label3 = new JLabel("Street");
  28.         JLabel label4 = new JLabel("City");
  29.         JLabel label5 = new JLabel("State");
  30.         JLabel label6 = new JLabel("Zip");
  31.         JLabel label7 = new JLabel("Phone");
  32.         JLabel label8 = new JLabel("Click Button to Submit Form -->");
  33.        
  34.        
  35.         JTextField textfield1 = new JTextField(10);
  36.         JTextField textfield2 = new JTextField(10);
  37.         JTextField textfield3 = new JTextField(10);
  38.         JTextField textfield4 = new JTextField(10);
  39.         JTextField textfield5 = new JTextField(10);
  40.         JTextField textfield6 = new JTextField(10);
  41.         JTextField textfield7 = new JTextField(10);
  42.         JButton button1 = new JButton("Submit Data"); [COLOR="Red"]//here is button1[/COLOR]
  43.        
  44.        
  45.         textfield1.addActionListener(this);
  46.         textfield2.addActionListener(this);
  47.         textfield3.addActionListener(this);
  48.         textfield4.addActionListener(this);
  49.         textfield5.addActionListener(this);
  50.         textfield6.addActionListener(this);
  51.         textfield7.addActionListener(this);
  52.         button1.addActionListener(this);
  53.        
  54.         //textfield1.setSize(10, 10);
  55.        
  56.         iFrame.add(label1);
  57.         iFrame.add(textfield1);
  58.         iFrame.add(label2);
  59.         iFrame.add(textfield2);
  60.         iFrame.add(label3);
  61.         iFrame.add(textfield3);
  62.         iFrame.add(label4);
  63.         iFrame.add(textfield4);
  64.         iFrame.add(label5);
  65.         iFrame.add(textfield5);
  66.         iFrame.add(label6);
  67.         iFrame.add(textfield6);
  68.         iFrame.add(label7);
  69.         iFrame.add(textfield7);
  70.         iFrame.add(label8);
  71.         iFrame.add(button1);
  72.        
  73.     try { [COLOR="Red"]//problem starts here[/COLOR]
  74.       String directoryName = "c:\\javacode\\Notepad"; [COLOR="Red"]//I've created my variable to hold my directory where my .txt file is to be pulled from[/COLOR]
  75.       String fileName = "program.txt"; [COLOR="Red"]//variable that holds my text file name[/COLOR]
  76.       File output = new File(directoryName,fileName); [COLOR="Red"]//File object that points to the directory and file[/COLOR]
  77.       output.createNewFile(); [COLOR="Red"]//if file is not created, create it[/COLOR]
  78.      
  79.       if (! output.isFile()) { [COLOR="Red"]//if it can't be created, output an error message[/COLOR]
  80.     System.out.println("File creation of" + output.getPath()
  81.                 + "failed");
  82.         return;
  83.       }
  84.       
  85.       //put an if statement here
  86.       
  87.       if (e.getSource() == button1) { [COLOR="Red"]//I feel I'm doing something wrong here, I'm trying to get the when the button (button1 created above) is pushed, to then execute the code below.[/COLOR]
  88.     BufferedWriter out = new BufferedWriter(new FileWriter(output.getPath(), true));
  89.                  
  90.                 [COLOR="Red"]//This part here is all test code below, that when the button (button1) is pressed, this code is executed and this text is written to a data file, once I confirm a button press I can move on to taking the data input into each of the separate textFields from the form and writing it, one step at a time, I'm beginner.[/COLOR]
  91.     String[] text = {"This is the text that will be written. \r", //we pass an array, which holds the text to append to the file
  92.              "The text will be written to the file \r",
  93.              "in append mode. If the file does not \r",
  94.              "exist it will be created.\r"};
  95.     for(int i = 0; i < text.length; i++) {
  96.       out.write(text[i] + "\n");
  97.     }
  98.    
  99.     out.close();
  100.     }
  101.      }
  102.      catch(IOException d) {
  103.     System.out.println("Error writing to file " + d);
  104.      }
  105.      [COLOR="Red"]//all other code outside of these red words works fine as you can see in the pictures, I'll accept any suggestions as I'll be working on this still.[/COLOR]
  106.     
  107.    
  108.        
  109.         iFrame.setSize(200, 500);
  110.         iFrame.setVisible(true);
  111.        
  112.         add(iFrame);
  113.        
  114.       }
  115.       
  116.         if (answer == "Display Data") {
  117.        
  118.         JInternalFrame iFrame = new JInternalFrame("Display Data . ", false, true, false, false);
  119.        
  120.         JTextArea text = new JTextArea();
  121.         JScrollPane scroller = new JScrollPane();
  122.         scroller.getViewport().add(text);
  123.        
  124.         iFrame.getContentPane().add(scroller);
  125.         iFrame.setSize(200, 150);
  126.         iFrame.setVisible(true);
  127.         add(iFrame);
  128.         }
  129.  
  130.   }
  131.  
  132.   public FinalProgram() {
  133.     super ("Menu Example");
  134.    
  135.  
  136.     JMenuBar jmb = new JMenuBar();
  137.     JMenu file = new JMenu("Functions");
  138.     JMenuItem item;
  139.     file.add(item = new JMenuItem("Get Data"));
  140.     item.addActionListener (this);
  141.     file.add(item = new JMenuItem("Display Data"));
  142.     item.addActionListener (this);
  143.  
  144.     jmb.add(file);
  145.    
  146.  
  147.    
  148.     addWindowListener(new ExitListener());     
  149.     setJMenuBar (jmb);
  150.   }
  151.  
  152.  
  153.     public static void main(String[] args) {
  154.    
  155.         FinalProgram window = new FinalProgram();
  156.         window.setTitle("Final Program");
  157.         window.setSize(600, 600);
  158.         window.setVisible(true);
  159.        
  160.     }
  161. }

Last edited by UnrealEd; 08-10-07 at 07:35 AM. Reason: please use the [highlight=Java] tag when posting Java code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP upload prob minority ASP 1 06-27-05 09:35 AM
write to beginning of file or reverse an array of binary data abtimoteo Perl 1 01-20-04 11:09 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM


All times are GMT -5. The time now is 01:40 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.