Help with opening a text file upon closing a program.

Help with opening a text file upon closing a program.

I need some helping in figuring out something I want to do. I have this program that is a swimming pool volume calculator. It stored that data on a output.txt file but I want that file to open whenever the user exits the program.

My code so far:

import java.io.*; import java.text.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.text.DecimalFormat;

public class PoolVolume { private JFrame mainFrame; private JButton calcButton, exitButton; private JTextField length, width, depth, volume; private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel; public PoolVolume() { mainFrame = new JFrame("Swimming Pool Volume Calculator"); calcButton = new JButton("Calculate Volume"); exitButton = new JButton("Exit"); length = new JTextField(5); width = new JTextField(5); depth = new JTextField(5); volume = new JTextField(5); lengthLabel = new JLabel(" Enter the length of the swimming pool: "); widthLabel = new JLabel(" Enter the width of the swimming pool: "); depthLabel = new JLabel(" Enter the depth dept of the swimming pool: "); volumeLabel = new JLabel(" Swimming pool volume: "); JPanel c=new JPanel(new GridLayout(5,2)); c.add(lengthLabel); c.add(length); c.add(widthLabel); c.add(width); c.add(depthLabel); c.add(depth); c.add(volumeLabel); c.add(volume); c.add(calcButton);

            c.add(exitButton);
            calcButton.setMnemonic('C');
            exitButton.setMnemonic('x');
            mainFrame.setSize(500,200);
            mainFrame.add(c);
        mainFrame.addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e) {System.exit(0);}});
            calcButtonHandler chandler =new calcButtonHandler();
            calcButton.addActionListener(chandler);
            exitButtonHandler ehandler =new exitButtonHandler();
            exitButton.addActionListener(ehandler);
            FocusHandler fhandler =new FocusHandler();
            length.addFocusListener(fhandler);
            width.addFocusListener(fhandler);
            depth.addFocusListener(fhandler);
            volume.addFocusListener(fhandler);
            mainFrame.setVisible(true);
        }
class calcButtonHandler implements ActionListener 
{
    public void actionPerformed(ActionEvent e) 
        {
            DecimalFormat num =new DecimalFormat(",###.##");
            Double cLength, cWidth, cdepth, cVolume, Total;
            String sLength, sWidth, sdepth, sVolume;
            sLength =length.getText();
            cLength = Double.parseDouble(sLength);
            sWidth =width.getText();
            cWidth = Double.parseDouble(sWidth);
            sdepth =depth.getText();
            cdepth = Double.parseDouble(sdepth);
            if(e.getSource() == calcButton) {
            Total = cLength * cWidth * cdepth;
            volume.setText(num.format(Total));
            try{
            String value=volume.getText();
            File file = new File("output.txt");
            FileWriter fstream = new FileWriter(file,true);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
            out.newLine();
            out.close();
        }
        catch(Exception ex){}
        }
    }
}
class exitButtonHandler implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
    }
class FocusHandler implements FocusListener 
{
    public void focusGained(FocusEvent e) 
        {
        }
    public void focusLost(FocusEvent e) 
        {
        }
}

public static void main(String args[]) {

View Answers

December 4, 2010 at 3:22 AM

Not sure how that code came out like that. Sorry! Also, there is a little bit missing from the end. here it is:

new PoolVolume(); } }









Related Tutorials/Questions & Answers:
Help with opening a text file upon closing a program.
Help with opening a text file upon closing a program.  I need some helping in figuring out something I want to do. I have this program... that file to open whenever the user exits the program. My code so far
stand alone program for text file editing
stand alone program for text file editing  steps 1.read a text file origi.txt from a location 2.make a copy of the text file to the same location with name as origi_copy.txt(example) 3.each line of the copy should start from 10
Advertisements
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating a simple program for reading text file and writing data into another text file... and we want to copy the content into another text file from our Java program
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
IllegalStateException for file opening
IllegalStateException for file opening  <%@page import...*"%> <%@include file="sessioncheck.jsp"%> <...){ } File requiredFile = new File(strabsoluteFileName
IllegalStateException for file opening
IllegalStateException for file opening  <%@page import...*"%> <%@include file="sessioncheck.jsp"%> <...){ } File requiredFile = new File(strabsoluteFileName
Example to compile a java file with the help of the program
Example to compile a java file with the help of the program... are describing you the way to compile the java file with the help of the java program. The steps involved for compiling the java file from the program are described
Error Opening File - Java Interview Questions
Error Opening File  Dear, Please if help me to resolve.... But when I try to export it under Runnbale jar file on my desktop and I run this generated file, a message was displayed: Error opening file. But If I
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Help with this program
Help with this program  Write a program that would do the following: 1) Read a String from the user. The String will be the following: "My grade is x" (where x is an integer between 0-9) 2) Get the String to integer by creating
Help with this program
Help with this program  Write a program that would do the following: 1) Read two Strings from the user 2) The user should input two integer numbers. 3) Convert the Strings to integers by creating objects and calling
opening internet browser in java program - Swing AWT
opening internet browser in java program  i want a code to open the internet browser in swing program using JFrame.  Hi Omkar, Use...) { JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage
Opening a browser only with body from a java program
Opening a browser only with body from a java program  Hi all, I got following requirement, Opening a browser window only with body and title bar... bar and etc. And I don't know how to close this browser window. Please help me
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
Exception when opening a excel file in JFrame
Exception when opening a excel file in JFrame  Hello, I have a problem when trying to display a Excel file in a Jframe. The exception is like... (jniwrap.dll) in java.library.path: C:\Program Files\Java\jdk1.7.0_05\bin;C
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import
Help in making a text formatter
Help in making a text formatter  I have a question on how to make a text formatter using Java. I attempted in doing so, but got a really low score on it, and so I'm having to rely on someone kind enough to help me. We use
text processing program
text processing program  how can i compare letter by letter? what i was trying to do is a program that can define what is root word,prefix and suffix. plz help
problem in swing program for opening two windows with same login credentials
problem in swing program for opening two windows with same login credentials  I Face two problems while writing the code in swing program 1.i... tree is to visible. please any one help me i am in the project work urgent post
need help with program
need help with program  To write a program to read a set of words from a file and return the following 1)Each word in the file against its frequency 2) the frequency should be in the descending order of the frequencies
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..  import java.io.*; import... the words in file content.   Program : by HashSet and StringTokenizer
Sorting text file
Sorting text file  Hello friends, i'm writing search page for my local website. i'm storing all file list in text file. And also adding a value... according to begin value. Text File having list like this: 5| 3| can anyone
how to update the text file?
how to update the text file?  if my text file contains a string... the integer value if my input String matches with the string in file. please help me in developing this code which is part of my project
multithreaded program help!!!!
multithreaded program help!!!!  Write a multithreaded program that calculates various statistical values for a list of numbers. This program... value. For example, suppose your program is passed the integers 90 81 78 95 79
need help with program
need help with program  To write a program where we can take two integer array as input,Find the missing number from array"B" has all the numbers from array"A" except one,and find the fastest way in doing
Need help with console program?
Need help with console program?  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). thanks in advance.   Here is an example that repeatedly
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
help in java program
help in java program  please could you help me with my assignment I have to submit it on Monday 29 October please help me :( Write a program to test the Person class defined below. Your test program should
Help with Temperature program
Help with Temperature program  I need help to write a GUI program to calculate the temperature. and here is the format Application that converts temperature between 3 different scales: Fahrenheit, Celsius and Kelvin. ΓΆβ?¬Β
C Program....PLEASE HELP
C Program....PLEASE HELP  For this assignment, you are to write a program that calculates points along a rhodonea curve, a.k.a. a mathematical rose..., and pointers. I am lost and need a little help starting please *int fillArray
Help with a maintenance tracking program.
Help with a maintenance tracking program.  The Pace Trucking Company... maintenance. The trucking company would like a program to allow the user to enter.... After the program asks for the number of trucks to be entered, users will enter
Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The file contains nothing but strings
Convert Text File to PDF file
Convert Text File to PDF file  Here is the way how to covert your Text file to PDF File, public class TextFileToPDF { private static void...(inLine); System.out.println("Text is inserted into pdf file
help with program - Java Beginners
Help with program  Simple Java Program  // Defining class Stars.java to print stars in certain orderclass Stars{// main() functionpublic static void main(String[] args){int a = 1, b = 5, c, d, i; // declaring 5 int type
Reading a text file in java
Reading a text file in java  What is the code for Reading a text file... in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks
Convert text file to XML file in Java
Convert text file to XML file Java In this section, you will learn how to convert the text file into xml file in Java language. To do this, we have used... file. The Parser invoked startElement() method at the beginning of every element
program help - Java Beginners
program help  In the following code, I want to modify class figure to be an abstract class. import java.util.*; public class Lab_sheet5 { static Scanner console=new Scanner(System.in); public static void main
how to convert image file to text file?
how to convert image file to text file?  hi, can anybody tell how to convert image file to text file? plz help me
Convert Text File to PDF file
Convert Text File to PDF file  import java.io.BufferedReader; import...); System.out.println("Text is inserted into pdf file"); document.close... FileReader( Input File)); String inLine = null
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
Steps to read text file in pyspark
Steps to read text file in pyspark  Hi, I am learning to write program in PySpark. I want to simply read a text file in Pyspark and then try some... example at: Read text file in PySpark Hope above code snippet will help you
uploading a text file into a database
uploading a text file into a database  how to upload a text file... { static File file; public static void main(String[] args) throws Exception { JLabel label=new JLabel("Choose File
How to Read file line by line in Java program
? that includes a text. Now first thing a program has to do is to find the file...Programmers have always found it difficult to read a big file (5-10 GB) line by line in Java. But there are various ways that can help read a larger file
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
Opening a file in a HTML form
Opening a file in a HTML form In this example, you will first see that a form has been created in HTML format and the code of opening a file has been written... a file in PHP.ADS_TO_REPLACE_1 Opening a File in PHP <?php  $string
need help with a program - Java Beginners
Java algorithm - need help with a program  Java algorithm - need help with a program
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... i want to update the quantity in my text file, if the item i entered matches...=new float[n1]; for(int i=0;i File file ; FileReader fr=null
Converting Text Files into Bzip File
Converting Text Files into Bzip File  Hi, I am facing the problem during run the program, when converting text files into Bzip file. Please guide me how do i convert the text file into bzip file in PHP. I will welcome, if anyone

Ads