Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Help with opening a text file upon closing a program.

Have Programming Question? Ask it here!
 
 
 


meagan garrett
Help with opening a text file upon closing a program.
1 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


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[]) {


Advertisement
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
IllegalStateException for file opening
IllegalStateException for file opening  <%@page import...*"%> <%@include file="sessioncheck.jsp"%> <...){ } File requiredFile = new File(strabsoluteFileName
Advertisements
IllegalStateException for file opening
IllegalStateException for file opening  <%@page import...*"%> <%@include file="sessioncheck.jsp"%> <...){ } File requiredFile = new File(strabsoluteFileName
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
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
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
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
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
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
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
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
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
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 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 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
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
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
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. Opening a File in PHP <?php  $string = $_POST["
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
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
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
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
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 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
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
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
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
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
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  
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
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
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
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... in my text file, if the item i entered matches with item in text file.how can i...]; for(int i=0;i File file ; FileReader fr=null ; FileWriter
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
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
Opening Notepad
Opening Notepad  Sir/madam I want to open notepad from java applications and save the contents on it. Can someone please help me to do this. With Regards Vaishnavi Vishwanath   To open the notepad, just go through
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
Write Text into File
Write Text into File       In this example we are writing text into file.In... to write text into file. At last close output file using close() method
Creating a log in a text file - Applet
as an application. Therefore i have added it onto a frame. I need to create a text file... time it is run. I have figured of a way of writing the time to a text file using..."); Label label1 = new Label("Enter the file name:"); TextField text = new
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
save text file - Java Beginners
save text file  hi i have just start programming in java.please guide me if i want to read a text file in java.then the text file is save in which directory
closing connections in finally block
closing connections in finally block  Hi, What will happen if I close connection before closing the resultset in the finally clock? e.g. finally... { ... conn.close(); rs.Close(); } If i do as above ... what will happen
Read Lines from text file
line based on a predefined size... Eg : If the 1st line of the text file... read from the text file and displays the output as desired. Unable to read the rest of the lines within the text files. Any help would be appreciated
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
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
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
Text field save as word file
Text field save as word file  Dear experts At run how to save set of text field contains text into single Word file. To save in our desktop computer. Solve my problem
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
Advertisements
 

 

 

DMCA.com