i am trying to display the output in jlabels for this shipping cost program i have coded. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel. I had no problems making labels for the input in my designFrame method, but when I try to make new labels for the mainPanel in displayoutput they simply do not display. here is the code:

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

public class shippingCharge extends JFrame implements ActionListener

	//objects and data types created here
	JPanel mainPanel = new JPanel();
	JTextField packageIdentificationTextField = new JTextField(6);
	JTextField poundsTextField = new JTextField(10);
	JTextField ouncesTextField = new JTextField(10);
	JButton displayButton = new JButton("Calculate  ");
	String packageIdentificationString;
	double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;

	public static void main(String[] args)
		shippingCharge shippingTotal = new shippingCharge();
	public shippingCharge()
	public void designFrame()
		mainPanel.add(new JLabel("Package ID"));
		mainPanel.add(new JLabel("Pounds"));
		mainPanel.add(new JLabel("Ounces"));
		//add listener to the  object
	public void getInput()
		packageIdentificationString = packageIdentificationTextField.getText();
		poundDouble = Double.parseDouble(poundsTextField.getText()); 
		ouncesDouble = Double.parseDouble(ouncesTextField.getText());
	public void calculateShipping()
		final double SHIPPING_RATE = .12;
		final double OUNCES_PER_POUND = 16;
		poundToOunceOuncesDouble = poundDouble * OUNCES_PER_POUND;
		shippingCostDouble = (poundToOunceOuncesDouble + ouncesDouble) * SHIPPING_RATE;

	public void actionPerformed(ActionEvent evt)
	public void displayOutput()
	mainPanel.add(new JLabel("Package ID:" + packageIdentificationString));
        mainPanel.add(new JLabel("Weight:" + poundDouble + "lbs" + ouncesDouble + "oz."));
        mainPanel.add(new JLabel("Shipping Cost:" + shippingCostDouble));

After adding the labels, be sure to call validate() on "mainPanel".

After adding the labels, be sure to call validate() on "mainPanel".

what would that do? i haven't seemed to learn that in my course yet.

would that make the JLabels in displayOutput() show up? The other labels on mainpanel are showing up fine, I just need to know why I cannot display more inside the method displayOutput.

Did you try adding the validate() call as suggested? Did you read the api documentation on that method in the link that masijade posted?

Learning to take advantage of the documentation available is one of the most important things to programming in any language.

how would i call validate with the created jlabels?

You call validate on the container you added them to - "mainPanel" in your case. Masijade posted that as the very first answer to your question. Obviously you did not try it.

ahh, ok. i called validate and it all works now. but i have another question.

i tried to format the labels like so:

i tried to use the setVerticalAlignment method to do so, but I can't get the inputted values under the corresponding labels.

JLabel packageLabel =new JLabel("Package ID  " , JLabel.LEFT);
		JLabel weightLabel = new JLabel("Weight  ", JLabel.CENTER);
        JLabel shippingCostLabel = new JLabel("Shipping Cost  ", JLabel.RIGHT);
        JLabel packageIdentificationStringLabel = new JLabel(packageIdentificationString + " ", JLabel.LEFT);
        JLabel weightIntLabel = new JLabel(poundDouble + " " + "lbs" + ", " + ouncesDouble + " " + "oz." + " ", JLabel.CENTER);
        JLabel shippingCostDoubleLabel = new JLabel(" " + shippingCostDouble, JLabel.RIGHT);

The "alignment" values for a label, are only for the text's alignment within the label, not the alignment of one label with another, or the label with it's container.

If you think you're up to the task, try using a JTable. If not, then try using a GridLayout (instead of the default FlowLayout in your panel), or even better, since the "field" widths should vary, a GridBagLayout (although I believe this one is definately beyond you, but don't feel bad, many experienced programmers have a bear of a time getting it right, if they ever do).