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



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


meagan garrett
Help with opening a text file upon closing a program.
1 Answer(s)      3 years and 10 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
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
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["
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
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
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
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
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  
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
Advertisements
 

 

 

DMCA.com