jerry-2112
asked on
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 ?
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();
}
}
}}
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);
}
}
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
figured it out had to move inside brackets before set back to empty
:)
ASKER
length:, width:, avg depth:, volume:,length:, width:, avg depth:, volume:,length:, width:, avg depth:, volume:,
Open in new window