Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

setText in JLabel not working

Posted on 2010-08-24
7
708 Views
Last Modified: 2013-11-23
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();
    	setLayout(layout);
    	
					//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;
    	
		addComponent(step1,0,0,2,1);
    	addComponent(step1_,1,0,2,1);
    	
    	for(int x=0; x<cakeName.length; x++){
    		addComponent(cakeLabel[x],x+4,0,1,1);
    		
    	}
    	
    	addComponent(cakeOrder[0],4,1,1,1);
    	addComponent(cakeOrder[1],5,1,1,1);
    	addComponent(cakeOrder[2],6,1,1,1);
    	addComponent(cakeOrder[3],7,1,1,1);
    	addComponent(cakeOrder[4],8,1,1,1);
    	addComponent(cakeOrder[5],9,1,1,1);
    	
    	addComponent(ttlQtyTitle,11,0,1,1);
    	addComponent(ttlPriceTitle,12,0,1,1);
    	addComponent(ttlQty,11,1,1,1);
    	addComponent(ttlPrice,12,1,1,1);
    }
    
    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);
    	add(component);
    }
    
    public void setTotalQty(int x){
    	totalQty = x;
    	String y = Integer.toString(x);
    	ttlQty.setText(y);
		System.out.println(totalQty);
    }
    public void setTotalPrice(double x){
    	totalPrice = x;
		System.out.println(totalPrice);
    }
    public void setOrderList(String x){
    	orderList[0] = x;
    	cakeOrder[0].setText("Hello");
    }
}

Open in new window

0
Comment
Question by:moombaz
  • 5
7 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 33509083
which labe text is not set?
0
 
LVL 1

Author Comment

by:moombaz
ID: 33509094
ttlQty
cakeOrder[0]

see line 129 and 138
0
 
LVL 6

Expert Comment

by:__geof__
ID: 33509169
Did you check that it is not set or is the GUI not updated?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:moombaz
ID: 33509803
It is not updated.
0
 
LVL 1

Author Comment

by:moombaz
ID: 33512952
Any inputs? I need help.
0
 
LVL 1

Author Comment

by:moombaz
ID: 33515266
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?  
0
 
LVL 1

Accepted Solution

by:
moombaz earned 0 total points
ID: 33519694
got the answer. variables declared has to be static
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 56
add projects t working set in maven 2 16
Java Eclipse Loop 3 20
restrict decimal places for double datatype 10 19
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question