Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

I need to get Java code to write to file

Here is all my code.  I need to write to a file.in the pool tab.  Where do I put the code ?
I know I'll have to import FileWriter and maybe a couple more but Do I need an other event Handler or can you just include the wrinting into the CalculationButtonHandler ?
package Jerry_McCourt_Final_Project;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import java.text.DecimalFormat;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import javax.swing.JLabel;
import java.awt.Color;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JTextField;

public class Pool extends JPanel
{ 
	ExitButton exitButton;
    //private JFrame mainframe;
    private JButton calculatebutton;
   
    private JTextField lengthfield;
    private JTextField widthfield;
    private JTextField volumefield;
    private JLabel lengthlabel;
    private JLabel widthlabel;
    private JLabel volumelabel;
    private JLabel depthlabel;
    private JTextField depthfield;
public Pool() 
{
	setBackground(Color.YELLOW);

        exitButton = new ExitButton();
        

       
        lengthlabel = new JLabel("Enter the length of the Swimming Pool :");
        lengthfield =  new JTextField(15);
        
        widthlabel = new JLabel("Enter The Width of the Swimming Pool : ");
        widthfield =  new JTextField(15);
        
        depthlabel = new JLabel("Enter the Average Depth of the Swimming Pool :");
        depthfield = new JTextField(15);
        
        volumelabel = new JLabel("Swimming Pool Volume :");
        volumefield = new JTextField(10);
        
        calculatebutton = new JButton("Calculate Volume:");
        calculatebutton.setBackground(Color.BLUE);
       ;

      

        this.add(lengthlabel);
        this.add(lengthfield);
        
        this.add(widthlabel);
        this.add(widthfield);
        
        this.add(depthlabel);
        this.add(depthfield);
        
        
        this.add(volumelabel);
        this.add(volumefield);
        
        
        this.add(calculatebutton);
        this.add(exitButton.getExitButton());

        calculatebutton.setMnemonic('c');
      
   
      CalculateButtonHandler chandler = new CalculateButtonHandler();
      calculatebutton.addActionListener(chandler);

     

      FocusHandler fhandler = new FocusHandler();
      lengthfield.addFocusListener(fhandler);
      widthfield.addFocusListener(fhandler);
      volumefield.addFocusListener(fhandler);
      depthfield.addFocusListener(fhandler);
      setVisible(true); 
      // mainframe.setVisible(true);
}

class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat num = new DecimalFormat(",###.##");
        double width, length, volume, depth;
        String inString;
inString = lengthfield.getText();
        if (inString.equals("")) {
            inString = "0";
            lengthfield.setText("0");
        }
length = Double.parseDouble(inString);

        inString = widthfield.getText();
        if (inString.equals("")) {
            inString = "0";
            widthfield.setText("0");
        }
width = Double.parseDouble(inString);

       inString =depthfield.getText();
       if (inString.equals("")){
    	   inString ="0";
    	   depthfield.setText("0");
       }
  depth = Double.parseDouble(inString);
       

        volume = length * width * depth;
        volumefield.setText(num.format(volume));
    }
//write to the file
	
}
class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class FocusHandler implements FocusListener
{
 public void focusGained(FocusEvent e) 
{
        if (e.getSource() == lengthfield || e.getSource() == widthfield) 
{
         volumefield.setText("");
}
 else if (e.getSource() == volumefield) 
{
calculatebutton.requestFocus();
}
}
public void focusLost(FocusEvent e) 
{
        if (e.getSource() == widthfield) 
{
calculatebutton.requestFocus();
}
}
}}

Open in new window

package Jerry_McCourt_Final_Project;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class FinalTab extends JFrame
{
    public FinalTab()
    {
        //this is the title of the tabbed application
        setTitle("Week Four Lab Assignment");
        
        //let's create a JTabbedPane object to add to the frame
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);
        
        //add our panels to the tabbed pane object
        
        jtp.addTab("Pool Calculator", new Pool());
        
        //add the tabbed pane to the frame content pane
        getContentPane().add(jtp);
        //set the frame size and visibility
        setSize(500,275);
        setVisible(true);        
    }
    
    public static void main(String[] args)
    {
        FinalTab test = new FinalTab();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Open in new window

package Jerry_McCourt_Final_Project;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class ExitButton 
{
    private static JButton exitButton;
    
    public JButton getExitButton()
    {
        exitButton = new JButton("Exit");
        exitButton.setBackground(Color.RED);
        exitButton.setMnemonic('x');
        exitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        
        return exitButton;
    }

		
	}

Open in new window

0
jerry-2112
Asked:
jerry-2112
  • 2
  • 2
1 Solution
 
CEHJCommented:
The standard is to have a File menu with a Save JMenuItem. Use a separate ActionListener class for that and have its actionPerformed do the file writing
0
 
jerry-2112Author Commented:
I added this and I get this in my file no Numbers !
length:, width:, avg depth:, volume:,length:, width:, avg depth:, volume:,length:, width:, avg depth:, volume:,

 
  volumefield.setText(num.format(volume));
    }
//write to the file
{
	
	//Will write to the Data.txt file
	try{
		
		
		FileWriter fileW = new FileWriter("Data.txt", true);
		System.out.println("Writing data to Data.txt file");
		fileW.write("length:");
		fileW.write(lengthfield.getText());
		fileW.write(",");
		fileW.write(" ");
		fileW.write("width:");
		fileW.write(widthfield.getText());
		fileW.write(",");
		fileW.write(" ");
		fileW.write("avg depth:");
		fileW.write(depthfield.getText());
		fileW.write(",");
		fileW.write(" ");
		fileW.write("volume:");
		fileW.write(volumefield.getText());
		fileW.write(",");
		fileW.close();
		
		
		
		FileReader fileR = new FileReader("Data.txt");
		BufferedReader buffIn = new BufferedReader(fileR);
		
		String textData = buffIn.readLine();
		System.out.println(textData);
		buffIn.close();
		System.out.println("\n");

		
		
	}
	catch(IOException e1)
	{
		JOptionPane.showMessageDialog(null,e1.getMessage(), "ERROR",2); //Will display error message if unable to write to file
	}
	
}
	
}

Open in new window

0
 
jerry-2112Author Commented:
figured it out had to move inside brackets before set back to empty
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now