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

 
 


meagan garrett
Help with opening a text file upon closing a program.
1 Answer(s)      2 years and 6 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[]) {

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 Pages:
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
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
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
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
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
help
from eclipse not from word file. That program should show the results also. Also... the programs from eclipse not from word file. That program should show the results also... program to check all above mentioned methods by invoking it. It should display
Executing code upon folder/drive access
Executing code upon folder/drive access  I would be glad if someone could show me how to make your program in jar format execute when the folder... of drive you can use Autorun.inf file. In the Autorun.inf file you can include
Opening a URL from an Applet
used for opening url from an applet. This program is using two functions in which... Opening a URL from an Applet       Introduction This is the example of opening a url in same
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
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
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
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
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
How to make a closing eyes, make a closing eyes, closing eyes
How to make a closing eyes     ... eye to closing eye. You will learn here how is it made possible... ready with this file by clicking bottom of the toolbar to make animation. You
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
Java - Opening a URL from an Applet
used for opening url from an applet. This program is using two functions... Java - Opening a URL from an Applet       This is the example of opening a url in same
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["
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
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
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 format text file? - Java Beginners
How to format text file?  I want the answer for following Query ***(Java code) How to open,read and format text file(notepad) or MS-word document in the same directory that of this required java program????  Hi
Java - Opening a url in new window from an applet
Java - Opening a url in new window from an applet       This is the example of opening a url from an applet. This program shows that how a url is opened in a new document or browser
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
how to store data in a text file - Java Beginners
how to store data in a text file  Hi friends, I want to know, how we can save the data in a .txt file using swings....... for example, i want to store the arraylist data of swing program into a text file.......and also i want
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
need help creating a lift program - Java Beginners
need help creating a lift program  Classes, Methods, Constructors please i need help to create an elevator program Simulating an Elevator write an Elevator class containing various methods. Then, write a program
cannot find java.exe error while opening netbean5.5 - IDE Questions
cannot find java.exe error while opening netbean5.5  HI, 1)i am...\jdk1.6.0_02. (while opening netbean5.5 shortcut in desktop) . my system jdk path is C:\Program Files\Java\jdk1.6.0\j2sdk1.4.2_03\bin\java.exe How can i solve
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 IE from Java Swing Application in Kiosk mode
Opening IE from Java Swing Application in Kiosk mode  Hi All, I want... window is opening. But I want to open the IE window in Kiosk mode or without address bar, foward/back buttons, etc. Please help me to resolve this problem Thanks
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
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
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
datetime picker is not opening
in this date time picker is not opening. The same opens in JSP code. My code is: **function.js** <script language="javascript" type="text/javascript" src... type='text' name='doj' id='demo1'><a href='javascript:NewCal('demo1
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
help me
help me  Create a GUI application that allows a user to select a text file (using the JFileChooser). The program will then allow the user to do... appropriate buttons or selecting from menu): 1) display the contents of the file 2
gradient text, text effect, text
How to make a gradient text       We can make many different type of text by the help... to take a new file with black background as here. Write Text: Select Horizontal
upload a file into database and progrm should support excel and text and csv file formats
upload a file into database and progrm should support excel and text and csv file formats  Hai all, I need a program to upload a file into database table... and the program should support .excel ,.txt ,.csv file formats. can
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
Uncompressing the file in the GZIP format.
and decompress the file.  To make a program on uncompressing a file, firstly you need... that you can understand where gzip file is opening. To open the input file firstly... will be created by using new operator. After opening the input file now we need to open
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
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
closing thread for jdk1.5 or higher versions - Java Beginners
closing thread for jdk1.5 or higher versions  Hi All, I am using jdk1.5 and higher version of java. I tried to destroy thread by using... can I overcome this error. Help me Please. thanks, mln15584
Opening files with .jnlp extension on browser!?!?!
Opening files with .jnlp extension on browser!?!?!  I working... file which gets downloaded on client's side on runtime. I searched into the code... where is has been used. I dont want this file to be downloaded into the client's pc
Search and return lines of text file
Search and return lines of text file  I wrote the following code, that found lines of txt file with contains certain word, but it returns only... it the program copy only the first line) } line = in.readLine(); } } catch
Java Program HELP - Java Beginners
Java Program HELP  Hi I'm having trouble with this program, i keep getting errors. Can you help me. Thanks Write a program to create a file named... from 1 to 100, separated by a comma. After the file has been created, close
java program for writing xml file - Java Beginners
xml file and storet that in particlar location. Please help me out Thanks...java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my
Plain Text Editor in PHP Program
Plain Text Editor Plain text editor is unlike any other editor, which is used to create the simple text file. It’s basically a program that allow user to create or edit plain text files including .txt, .php and many others

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.