I'm working on a program that first launches a frame with various text fields. After they are filled in, the information is submitted by simply clicking the submit button. I have the frame down, the text fields and the buttons, and I have the class where all my data and calculations lie.

I'm just having a hard time figuring out how to weave it all together, specifically on how to take that information that's input into the frame and send it all to the class where calculations are made, then return back the result to be displayed on the frame itself (i'll probably change from the printData method to toString method).

Any help would be awesome.

package prjPackage;

import javax.swing.SwingUtilities;

public class ProjectClass {

	public static void main(String[] args)
	{
		Package p;
		PostOffice po;
			
		// create objects
		po = new PostOffice();

		//mail package
		po.getPackage();
		
		//launch the frame
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				new PackageFrame();
			}
		});
		
		// print out results
		po.printData();
				

	}// end main

}// end ProjectClass
//
//PostOffice
//
//The purpose of this class is to represent the Post Office
//rules for mailing a package first class.
//

package prjPackage;

public class PostOffice
{
private Package pkg;
private float cost;
private boolean canShipPackage;
private boolean exceedsWeightLimit;
private boolean exceedsCombinedLimit;

// constants used in determining the cost of shipping the package

// prices per pound
private static final float COSTRANGE1 = 0.75f;
private static final float COSTRANGE2 = 0.45f;
private static final float COSTRANGE3 = 0.40f;
private static final float COSTRANGE4 = 0.35f;
private static final float COSTRANGE5 = 0.30f;  // greater weight than last range

// ranges of weights in pounds
private static final int MAXRANGE1 = 2;         // up to 2 lbs
private static final int MAXRANGE2 = 10;        // greater than 2 to up to 10 lbs
private static final int MAXRANGE3 = 40;        // greater than 10 up to 40 lbs
private static final int MAXRANGE4 = 55;        // greater than 40 up to 55 lbs

// limits
private static final int MAXWTTOSHIP = 70;      // maximum weight allowed to ship
private static final int MAXLENGIRTHTOSHIP = 100;   // maximum len + girth to ship

//
//  PostOffice
//
//  Constructor for the PostOffice class
//
//  Input:  none
//  Return: none
//

public PostOffice()
{
    pkg = new Package();
    updateShipping();
} // end PostOffice

//
//  setPackage
//
//  This method modifies the package attribute
//
//  Input:  p       the new package
//  Return: none
//

public void setPackage(Package p)
{
    pkg = p;
    updateShipping();
} // end setPackage

//
//  getPackage
//
//  this method returns a copy of the package attribute
//
//  Input:  none
//  Return: pkg
//

public Package getPackage()
{
    return(pkg);
} // end getPackage

//
//  getCost
//
//  this method returns a copy of the cost to ship the package
//
//  Input:  none
//  Return: cost
//

public float getCost()
{
    return(cost);
} // end getCost

//
//  getCanShipPackage
//
//  This method returns a copy of the canShipPackage attribute
//
//  Input:  none
//  Return: canShipPackage
//

public boolean getCanShipPackage()
{
    return (canShipPackage);
} // end getCanShipPackage

//
//	getExceedsCombinedLimit
//
//	the purpose of this method is to return a copy of the exceedsCombinedLimit
//	attribute
//
//	Input:	none
//	Return:	exceedsCombinedLimit
//

public boolean getExceedsCombinedLimit()
{
	return(exceedsCombinedLimit);
}// end getExceedsCombinedLimit

//
//	getExceedsWeightLimit
//
//	the purpose of this method is to return a copy of the exceedsWeightLimit
//	attribute
//
//	Input:	none
//	Return:	exceedsWeightLimit
//

public boolean getExceedsWeightLimit()
{
	return(exceedsWeightLimit);
}// end getExceedsWeightLimit


//
//  printData
//
//  this method prints out the attributes for the class
//
//  Input:  none
//  REturn: none
//

public void printData()
{     
    pkg.printData();
    if (canShipPackage == true)
    {
        System.out.println("The package will cost $" + cost + " to ship.");
    }
    else 
    {         
        if (exceedsWeightLimit == true)
        {
            System.out.print("The package cannot be shipped.  ");
            System.out.println("It exceeds weight restrictions");
        }
        
        if (exceedsCombinedLimit == true)
        {
            System.out.print("The package cannot be shipped.  ");
            System.out.println("It exceeds the combined length restrictions");
        }
    }

} // end printData


//
//  sendPackage
//
//  This method allows a package to be checked for shipping cost to send.
//
//  Input:  p       the package to ship
//  Return: none
//

public void sendPackage(Package p)
{
    pkg = p;
    
    // update the shipping information for this new package
    updateShipping();
    
} // end sendPackage

//
//	updateShipping
//
//	The purpose of this method is to update the shipping information
//	for the package
//
//	Input:	none
//	Return:	none
//

public void updateShipping( )
{
	// update the weight status
	checkWeightLimit( );
	
	// update the combined status
	checkCombinedLimit( );
	
	// update the overall shipping status
	if ((exceedsWeightLimit == true) || (exceedsCombinedLimit == true))
	{
		canShipPackage = false;
		cost = 0.0f;
	}
	else
	{
		canShipPackage = true;
		
    	// update the shipping cost
		calcCost();
	}

}// end updateShipping

//
//	checkWeightLimit
//
//	The purpose of this method is to check the weight limit of a package
//
//	Input:	none
//	Return:	none
//

public void checkWeightLimit( )
{
	int wt;
	wt = pkg.getWeight();
	
	if (wt > MAXWTTOSHIP)
	{
		exceedsWeightLimit = true;
	}
	else
	{
		exceedsWeightLimit = false;
	}
}// end checkWeightLimit

//
//	checkCombinedLimit
//
//	The purpose of this method is to check the combined limit of a package
//
//	Input:	none
//	Return:	none
//

public void checkCombinedLimit( )
{
	int len;
	int g;
	int combinedValue;

	// calculate the combined value
	len = pkg.getLength();
	g = pkg.getGirth();
	combinedValue = len + g;
	
	
	if (combinedValue > MAXLENGIRTHTOSHIP)
	{
		exceedsCombinedLimit = true;
	}
	else
	{
		exceedsCombinedLimit = false;
	}
}// end checkCombinedLimit


//
//  calcCost
//
//  this method determines the cost for a package to be shipped.
//
//  Input:  none
//  REturn: none
//

private void calcCost()
{
    int wt;
    
    // get the package attributes
    wt = pkg.getWeight();
            
    // okay to ship the package so calculate cost
    if (wt <= MAXRANGE1)
    {
    	cost = calcRange1Cost(wt);
    }
    else if (wt <= MAXRANGE2)
    {
    	cost = calcRange2Cost(wt);
    }
    else if (wt <= MAXRANGE3)
    {
    	cost = calcRange3Cost(wt); 
    }
    else if (wt <= MAXRANGE4)
    {
    	cost = calcRange4Cost(wt);
    }
    else
    {
    	cost = calcRange5Cost(wt);
    }

}// end calcCost

//
//  calcRange1Cost
//
//  This method calculates the cost for range 1
//
//  Input:  wt              weight in lbs used to calculate cost 
//  Return: rangeCost
//

private float calcRange1Cost(int wt)
{
    float rangeCost;
    
    rangeCost = (float)wt * (float)COSTRANGE1;
    return(rangeCost);
}

//
//  calcRange2Cost
//
//  This method calculates the cost for range 2
//
//  Input:  wt              weight in labs used to calculate cost
//  Return: rangeCost
//

private float calcRange2Cost(int wt)
{
    int overRange1;
    float rangeCost;
    
    // calculate the price for the weight up to Range1
    rangeCost = calcRange1Cost(MAXRANGE1);
    
    // determine the weight over the Range1 limit
    overRange1 = wt - MAXRANGE1;
    
    // calculate the remaining cost
    rangeCost += ((float)overRange1 * COSTRANGE2);
    
    return(rangeCost);

} // end calcRange2Cost

    //
//  calcRange3Cost
//
//  This method calculates the cost for range 3
//
//  Input:  wt              weight in labs used to calculate cost
//  Return: rangeCost
//

private float calcRange3Cost(int wt)
{
    int overRange2;
    float rangeCost;
    
    // calculate the price for the weight up to Range2
    rangeCost = calcRange2Cost(MAXRANGE2);
    
    // determine the weight over the Range2 limit
    overRange2 = wt - MAXRANGE2;
    
    // calculate the remaining cost
    rangeCost += ((float)overRange2 * COSTRANGE3);
    
    return(rangeCost);

} // end calcRange3Cost

//
//  calcRange4Cost
//
//  This method calculates the cost for range 4
//
//  Input:  wt              weight in labs used to calculate cost
//  Return: rangeCost
//

private float calcRange4Cost(int wt)
{
    int overRange3;
    float rangeCost;
    
    // calculate the price for the weight up to Range3
    rangeCost = calcRange3Cost(MAXRANGE3);
    
    // determine the weight over the Range3limit
    overRange3 = wt - MAXRANGE3;
    
    // calculate the remaining cost
    rangeCost += ((float)overRange3 * COSTRANGE4);
    
    return(rangeCost);

} // end calcRange4Cost 

//
//  calcRange5Cost
//
//  This method calculates the cost for range 5
//
//  Input:  wt              weight in labs used to calculate cost
//  Return: rangeCost
//

private float calcRange5Cost(int wt)
{
    int overRange4;
    float rangeCost;
    
    // calculate the price for the weight up to Range4
    rangeCost = calcRange4Cost(MAXRANGE4);
    
    // determine the weight over the Range1 limit
    overRange4 = wt - MAXRANGE4;
    
    // calculate the remaining cost
    rangeCost += ((float)overRange4 * COSTRANGE5);
    
    return(rangeCost);

} // end calcRange5Cost


}// end PostOffice
package prjPackage;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PackageFrame implements ActionListener
{
	JTextField height;
	JTextField weight;
	JTextField girth;
	JTextField length;
	JTextField width;
	
		
	PackageFrame()		
	{		
		JFrame jfrm = new JFrame("Text Fields");
		jfrm.setLayout(new BorderLayout());
		jfrm.setSize(400, 400);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		height = new JTextField(10);
		weight = new JTextField(10);
		girth = new JTextField(10);
		length = new JTextField(10);
		width = new JTextField(10); 
		
		//commented this out because i'm unable to reach my final result
		//cost.setEditable(false);
		
		// Set first panel to retrieve data from user
		JPanel inFieldPane = new JPanel();
		inFieldPane.setLayout(new GridLayout(2,2));
		inFieldPane.add(new JLabel("Height"));
		inFieldPane.add(height);
		height.addActionListener(this);
		inFieldPane.add(new JLabel("Weight"));
		inFieldPane.add(weight);
		weight.addActionListener(this);
		jfrm.add(inFieldPane,BorderLayout.NORTH);
		inFieldPane.add(new JLabel("Girth"));
		inFieldPane.add(girth);
		girth.addActionListener(this);
		jfrm.add(inFieldPane,BorderLayout.NORTH);
		inFieldPane.add(new JLabel("Length"));
		inFieldPane.add(length);
		length.addActionListener(this);
		jfrm.add(inFieldPane,BorderLayout.NORTH);
		inFieldPane.add(new JLabel("Width"));
		inFieldPane.add(width);
		width.addActionListener(this);
		jfrm.add(inFieldPane,BorderLayout.NORTH);
		
		//Set second panel to submit data for processing
		JPanel submitPane = new JPanel();
		submitPane.setLayout(new FlowLayout());
		submitPane.add(new JLabel("Package Information"));
		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(this);
		submitPane.add(submitButton);
		jfrm.add(submitPane,BorderLayout.CENTER);
		
		// Set third panel to display processed data
		JPanel outFieldPane= new JPanel();
		outFieldPane.setLayout(new GridLayout(1,2));
		outFieldPane.add(new JLabel("Cost To Ship"));
		
		//commented this out but i know it should go there
		//outFieldPane.add(cost);

		
		jfrm.add(outFieldPane,BorderLayout.SOUTH);		
		//make the frame visible
		jfrm.setVisible(true);
	}
		
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("Submit"))
		{
			//make the calculations and return the total
			//This is where I get stuck. I need to figure out what the cost to ship will be and since I'm not sure on how to run my inputs into the PostOffice class, get the calculations done, and run it back to be displayed on the frame, I can't get further.
		}
	
	}
}

To get data into the class I know of two methods.
1. Create appropriate constructors and pass the data in that way.
See: http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

2. Use set methods.

...
private float cost;
              ...
 
public void setCost(float newCost)
   {    
        cost = newCost;
    } // end setCost

To get data out: return it with the return statement.
You may need to change the access on some of your methods to be able to call them.

See
http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html
and
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Sorry, I'm still utterly confused. I know I need to actually get each variable into the other calculation class after the user hit the submit button, so I guess my question from here is:

How could I store it in the Frame Class after hitting submit, then call it from the frame class (PackageFrame) in the class where I need to make calculations (Package),

I know after that I'll finally return all variables to be displayed onto the GUI itself via a toString method. I'm still a beginner so It's really hard to get this.

//
//Package
//
//the purpose of this class is to represent one package
//
//

package prjPackage;

import javax.swing.JTextField;

public class Package
{
// attributes
private int length;
private int height;
private int width;
private int weight;
private int girth;

//
//  Package
//
//  this is the constructor for the Package class
//
//  Input:  none
//  Return: none
//

public Package()
{
    length = 0;
    height = 0;
    width = 0;
    weight = 0;
    calcGirth();
} // end Package

//
//  Package
//
//  This constructor takes an array of three dimensions and a weight
//  as input parameters.  The array will be sorted to assign the other'
//  dimensions.
//
//  Input:  d       an array containing dimensions
//			0 -width, 1 - height, 2 - length
//			wt			weight
//  REturn: none
//

public Package(int[] d, int wt)
{
    MyUtility.bubbleSort(d);
    length = d[2];
    height = d[1];
    width = d[0];
    weight = wt;
    calcGirth();
} // end Package
//

//
//  setDimensions
//
//  this method lets the length, height, and width be changed
//
//  Input:  dim			array of dimensions
//
//  Return: none
//

public void setDimensions(int[] dim)
{
    MyUtility.bubbleSort(dim);
    length = dim[2];
    height = dim[1];
    width = dim[0];
    calcGirth();
} // end setDimensions

//
//  setWeight
//
//  This method sets the weight of the package
//
//  Input:  wt      new weight value
//  Return: none
//

public void setWeight(JTextField wt)
{
    PackagePanel.weight = wt;
    Integer.parseInt(toString());
    System.out.println("Weight is" + wt);
} // end setWeight
   
//
//  getLength
//
//  This method returns the length of the package
//
//  Input:  none
//  Return: length
//

public int getLength()
{
    return(length);
} // end getLength

 
//
//  getHeight
//
//  This method returns the height of the package
//
//  Input:  none
//  Return: height
//

public int getHeight()
{
    return(height);
} // end getHeight

    
//
//  getWidth
//
//  This method returns the width of the package
//
//  Input:  none
//  Return: width
//

public int getWidth()
{
    return(width);
} // end getWidth

    
//
//  getWeight
//
//  This method returns the weight of the package
//
//  Input:  none
//  Return: weight
//

public int getWeight()
{
    return(weight);
} // end getWeight

    
//
//  getGirth
//
//  This method returns the girth of the package
//
//  Input:  none
//  Return: girth
//

public int getGirth()
{
    return(girth);
} // end getGirth



//
//  calcGirth
//
//  This method calculates the girth value for the package
//
//  Input:  none
//  Return: none
//

private void calcGirth()
{
    girth = (2 * height) + (2 * width);
    
} // end calcGirth

//toString method
//Lists all the information in a single string, which needs to be sent
//back to the frame in order to be displayed
//

public String toString()
{
	String retStr;
	String buff;
	
	buff = new StringBuffer()
	.append("Height\t” + Package.height + “\r\n”")
	.append("Weight\t” + weight + “\r\n”")
	.append("Length\t” + length + “\r\n”")
	.append("Width\t” + Width + “\r\n”")
	.append("Girth\t” + Girth + “\r\n”")
	.toString();
	retStr = buff.toString();
	return(retStr);
}

}// end Package class

Here is my package class. Ultimately the specs are going to be input into each text field, then submitted. Once submitted the info goes to this class, then a calculation is processed, then the result is returned back to the frame to be displayed on screen.

Here is my frame.

package prjPackage;

import javax.swing.*;

import java.awt.*;


public class PostOfficeFrame extends JFrame
{
    private BorderLayout frameLayout;
    private PackagePanel packagePanel;
   // private POButtonPanel buttonPanel;
    private JTextArea resultsMessage;
    private Package thePackage;  
    //private ShapePanel shapePanel;

    // 
    //  PostOfficeFrame
    //
    //  this is the constructor for the Post Office frame
    //
    //  Input:  none
    //  Return: none
    //
    
    public PostOfficeFrame()
    {    
        // initialize other attributes
        thePackage = new Package( );
        
    	// area for display of error messages when a package cannot be sent   	
    	resultsMessage = new JTextArea(2,30);

       // specify the title of the frame
        setTitle("Post Office");
       
        // specify the size of the frame
        setSize(500,300);  
        
        // specify the location of the frame
        setLocation(50,50);     
        
        // create the Layout manager for the frame
        frameLayout = new BorderLayout();
        
        // specify the layout manager to the frame
        setLayout(frameLayout);
 
        // create the panel for the package parameters 
        packagePanel = new PackagePanel();
        // add the panel to the frame
        add(packagePanel);
        
        
        //create the panel for the shapes
        shapePanel = new ShapePanel();
        add(shapePanel, BorderLayout.CENTER);
              

        // create the button panel
        buttonPanel = new POButtonPanel();

        // add the panel to the frame
        add(buttonPanel, BorderLayout.SOUTH);
           
       // add components to the frame
       add(resultsMessage, BorderLayout.CENTER);
 
        // show frame on screen
        setVisible(true);
          
    } // end PostOfficeFrame constructor

    	private void add(PackagePanel packagePanel2) {
		// TODO Auto-generated method stub
		
	}

		//
    //	updateResults
    //
    //	the purpose of this method is to update the results on the frame
    //
    //	Input:	res			the results message to display
    //	Return:	none
    //
    
    public void updateResults(String res)
    {
    	resultsMessage.setText(res);

    }// end updateResults

    
}// end PostOfficeFrame class

I only skimmed over your code.

I sometimes use a class called Global which contains a list of functions defined statically. Any class can then call Global.function(arg0) on any of the Global functions. Not sure if this helps :)

This article has been dead for over six months. Start a new discussion instead.