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  ");
	
	
	//Variables
	String packageIdentificationString;
	double poundDouble, ouncesDouble, poundToOunceOuncesDouble, shippingCostDouble;


	public static void main(String[] args)
	{
		shippingCharge shippingTotal = new shippingCharge();
	}
		
	public shippingCharge()
	{
		designFrame();
		setSize(500,500);
		setVisible(true);
		
	}
	
	public void designFrame()
	{
		mainPanel.add(new JLabel("Package ID"));
		mainPanel.add(packageIdentificationTextField);
		mainPanel.add(new JLabel("Pounds"));
		mainPanel.add(poundsTextField);
		mainPanel.add(new JLabel("Ounces"));
		mainPanel.add(ouncesTextField);
		mainPanel.add(displayButton);
		
		
        add(mainPanel);
		//add listener to the  object
        packageIdentificationTextField.addActionListener(this);
		displayButton.addActionListener(this);
		
	}
	
	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)
	{
		getInput();
		calculateShipping();
		displayOutput();
		
		
	}
	
	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:
[IMG]http://files.worthlessforums.com/lol/format.jpg[/IMG]

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);
		packageLabel.setVerticalAlignment(JLabel.TOP);
        mainPanel.add(packageLabel);
		
		JLabel weightLabel = new JLabel("Weight  ", JLabel.CENTER);
		weightLabel.setVerticalAlignment(JLabel.TOP);
        mainPanel.add(weightLabel);
        
        JLabel shippingCostLabel = new JLabel("Shipping Cost  ", JLabel.RIGHT);
        shippingCostLabel.setVerticalAlignment(JLabel.TOP);
        mainPanel.add(shippingCostLabel);
        
        JLabel packageIdentificationStringLabel = new JLabel(packageIdentificationString + " ", JLabel.LEFT);
        packageIdentificationStringLabel.setVerticalAlignment(JLabel.BOTTOM);
        mainPanel.add(packageIdentificationStringLabel);
        
        JLabel weightIntLabel = new JLabel(poundDouble + " " + "lbs" + ", " + ouncesDouble + " " + "oz." + " ", JLabel.CENTER);
        weightIntLabel.setVerticalAlignment(JLabel.BOTTOM);
        mainPanel.add(weightIntLabel);
        
        JLabel shippingCostDoubleLabel = new JLabel(" " + shippingCostDouble, JLabel.RIGHT);
        shippingCostDoubleLabel.setVerticalAlignment(JLabel.BOTTOM);
        mainPanel.add(shippingCostDoubleLabel);
        validate();

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.