Solve Java JLabel Problem: Display Output on mainPanel

  • Context: Comp Sci 
  • Thread starter Thread starter clook
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The forum discussion addresses a Java JLabel issue where output labels for package ID, weight, and shipping cost are not displaying on the mainPanel. The provided code initializes the GUI components correctly but fails to refresh the display after adding new JLabel instances in the displayOutput method. To resolve this, the mainPanel.revalidate() and mainPanel.repaint() methods must be called after adding new components to ensure they are rendered properly.

PREREQUISITES
  • Java Swing for GUI development
  • Understanding of event handling in Java
  • Basic knowledge of Java data types and parsing
  • Familiarity with NetBeans IDE for GUI design
NEXT STEPS
  • Implement mainPanel.revalidate() and mainPanel.repaint() in the displayOutput method
  • Explore Java Swing layout managers for better component positioning
  • Learn about Java exception handling for input validation
  • Investigate NetBeans GUI Builder for easier Swing application development
USEFUL FOR

Java developers, particularly those working on GUI applications, and anyone troubleshooting JLabel display issues in Swing.

clook
Messages
32
Reaction score
0
i am trying to display the output in a jlabel. I have created individual jlabels for the package ID, weight, and shipping cost and tried to display them with the mainPanel:

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));
	
			
	}
}

here is the code but the output labels aren't showing up. what gives? does it have something to do with positioning?
 
Last edited:
Physics news on Phys.org
Have you tried NetBeans?? You can graphically make the applet/gui of the program you are writing, it is a lot easier than just writing code. All you have to do is make it and add functionality to the buttons.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 6 ·
Replies
6
Views
3K