Add a Save button( Yes or No) to the GUI that saves the content of the array to a C:\data\Weight.dat file.
I have been using JOptionPane throughout multiple different variations of this program so a little late to change as this is due tomorrow. So I setup a JOptionPane.ConfirmDialog (hope that is correct). I am utterly confused on how to assign the "yes" button to save to the path listed above.
Here is my code:
package guiprogrampart2;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class GuiProgramPart2{
public static void main(String[] args) throws Exception{
//pulls icon from Web for display on MessageDialog
final ImageIcon icon = new ImageIcon(new URL("http://www.veryicon.com/icon/preview/Nature/Solar%20System/Earth%20Icon.jpg"));
String[] name = new String[4];
double[] weight = new double[4];
Weight [] object = new Weight[4];
UIManager cl = new UIManager ();
File inputFile = new File("C:\\data\\Weight.dat");
int ObjectWeight;
for (int i = 0; i <object.length; i++)
{
boolean error = false;
//request the name of an object from user
String ObjectName = JOptionPane.showInputDialog("What is the Objects name?");
do {
//request the weight of an object
ObjectWeight = Integer.parseInt(JOptionPane.showInputDialog( "What is the Objects Weight?"));
//determines if user entered a correct number
if( ObjectWeight > 99999 || ObjectWeight < 0 )
{
cl.put("OptionPane.messageForeground", Color.red);
//error message determined by IF statement variables
JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 99,999", "Error", JOptionPane.ERROR_MESSAGE);
error = true;
}
else
{
error = false;
}
}
while( error );
object[i] = new Weight();
object[i].setObjectName(ObjectName);
object[i].setWeight(ObjectWeight);
cl.put("OptionPane.messageForeground", Color.blue);
// display result in a JOptionPane message dialog
JOptionPane.showMessageDialog( null, "The " + ObjectName + " weighs " + ObjectWeight + " lbs on Earth" + "\nOn the Moon it weighs " + object[i].calcMoon() + " lbs "+ "\nOn Mercury it weighs " + object[i].calcMercury() + " lbs "+ "\nOn Jupiter it weighs " + object[i].calcJupiter() + " lbs ","Weight of Object",JOptionPane.INFORMATION_MESSAGE, icon );
}
JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
int confirm = JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
}
}
The changes I made to original was adding:
JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
int confirm = JOptionPane.showConfirmDialog(null, "Save these results?", "Save it?", JOptionPane.YES_NO_OPTION);
and
File inputFile = new File("C:\\data\\Weight.dat");
and I am at a loss on how to proceed. Hours of research online and through what books I have handy have proved useless. Any help would be greatly appreciated!

New Topic/Question
Reply



MultiQuote



|