[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 728
  • Last Modified:

setText in JLabel not working

I cannot do a setText in my program. There is no compilation error.

There are some String and integer being parsed from another class. the parsing part is ok. i've checked using System.out.println.

So using the value that is parsed, I cannot do a setText to my label. y?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class Order extends JPanel{
    //set layout
    private GridBagLayout layout;
	private GridBagConstraints constraints = new GridBagConstraints();
	//counter for storing array
	private int count = 0;
    //declare variable to be stored from Catalog class
    private int totalQty;
    private double totalPrice;
    private String totalQtyString;
    private String[] orderList = {"test","None ordered at all","None ordered at all","None ordered at all","None ordered at all","None ordered at all"};
					//Declaring JLabels here where they will be updated everytime user inputs new order.
	//Title header
	private JLabel step1, step1_;
	//cake name and orderlist Label
	private String[] cakeName = {"Chocolate Indulgence","Strawberry Cheesecake","Oreo Temptation","Baker's Carrot Cake","Tiramisu Delight","Durian Dream"};
	private JLabel[] cakeLabel;
	private JLabel[] cakeOrder;// = new JLabel[cakeName.length];
	//total quantity and price label
	private JLabel ttlQty = new JLabel();
	private JLabel ttlPrice = new JLabel();
	//declare label if there is any note. Eg. below $50 & durian note
	private JLabel note1 = new JLabel();
	private JLabel note2 = new JLabel();
	//declare title name for User input Text area
	String[] formTitle = {"Username", "Password", "Name", "Username", "Password", "Address", "Contact", "Date", "Time"};
	//declare text area for user to input
	private JTextArea[] userInput = new JTextArea[formTitle.length - 1];
	//declare radio button
	private JRadioButton time1 = new JRadioButton("9am - 1pm");
	private JRadioButton time2 = new JRadioButton("1pm - 6pm");
	private JRadioButton time3 = new JRadioButton("6am - 10pm");
    public Order() {
		//create layout
    	//setPreferredSize(new Dimension(800,500));
    	layout = new GridBagLayout();
					//Creating JLabels & JTextArea & JButtons for all
					//(using both for-loop to create multiple labels and also using individual method of creating labels)
		//Title Header
		step1 = new JLabel("Step 1: Confirm Your Order.");
		step1_ = new JLabel("==========================");
		//cake name labels and order list
		cakeLabel = new JLabel[cakeName.length];
		cakeOrder = new JLabel[cakeName.length];
		for (int x=0; x<cakeName.length;x++ ){
			cakeLabel[x] = new JLabel(cakeName[x]);
			cakeOrder[x] = new JLabel(orderList[x]); //Number of OrderList will be the same as cake name.
		//total quantity and price labels
		JLabel ttlQtyTitle = new JLabel("Total Quantity ordered: ");
		JLabel ttlPriceTitle = new JLabel("Total Price: ");
		//declare step 2 direction
		JLabel step2 = new JLabel("Step 2: New customers please enter your particulars. Existing customers please login.");
		//declare Forms labels and textArea
		JLabel[] formLabel = new JLabel[formTitle.length];
		for(int x=0; x<formTitle.length; x++)
			formLabel[x] = new JLabel(formTitle[x]);
		for(int x=0; x<formTitle.length-1;x++)
			userInput[x] = new JTextArea(1,5);
		JLabel step3 = new JLabel("Step 3: Please provide delivery date and time. Give 3 working days advance notice.");
		JLabel comfirm = new JLabel("Press the comfirm button below when you are satisfied that all informations are correct.");
		//declare 2 buttons
		JButton login = new JButton("Login");
		JButton confirm = new JButton("Confirm");
    	//adding components
		constraints.weightx = 1;
    	constraints.weighty = 1;
    	for(int x=0; x<cakeName.length; x++){
    private void addComponent(Component component, int row, int column, int width, int height){
    	constraints.gridx = column;
    	constraints.gridy = row;
    	constraints.gridwidth = width;
    	constraints.gridheight = height;
    	layout.setConstraints(component, constraints);
    public void setTotalQty(int x){
    	totalQty = x;
    	String y = Integer.toString(x);
    public void setTotalPrice(double x){
    	totalPrice = x;
    public void setOrderList(String x){
    	orderList[0] = x;

Open in new window

  • 5
1 Solution
which labe text is not set?
moombazAuthor Commented:

see line 129 and 138
Did you check that it is not set or is the GUI not updated?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

moombazAuthor Commented:
It is not updated.
moombazAuthor Commented:
Any inputs? I need help.
moombazAuthor Commented:
I already found out. D setText function if put in d listener class, and create a refresh button to call it, the setText works. I should say tt d setText method can b called from d main consructor or listener class and not from other class. Any idea how to do this?  
moombazAuthor Commented:
got the answer. variables declared has to be static

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now