Problems with setting text to JTextField

Nothing is showing up on the last 2 TextField on the bottom right.

Page 1 of 1

1 Replies - 1612 Views - Last Post: 04 May 2008 - 06:17 PM Rate Topic: ***-- 1 Votes

#1 C:/Syntax   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Problems with setting text to JTextField

Post icon  Posted 04 May 2008 - 06:04 PM

I am having this problem with most of my codes right now. I have no idea why nothing is comming of the the hPerField, or the tPerField.
The Coin class that I am using works. Basically the class return a string with either "HEAD" or "TAIL".

The problem is in doTossCoins()> in the for loop.

Here is my code:
import java.text.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 * Class FourthTest - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
public class FourthTest extends JApplet
  implements ActionListener
{
  private Coin coin;
  private JLabel coinLabel, hLabel, tLabel, pHLabel, pTLabel;
  private JButton tossButton;
  private JTextField numField, hField, tField, hPerField, tPerField;
  private static final Random random = new Random();
  public void init()
  {
	JPanel topPanel =  new JPanel();
	JPanel bottomPanel =  new JPanel();
	BorderLayout layout = new BorderLayout();  
	numField = new JTextField("0",5);
	coin= new Coin();
	coinLabel = new JLabel(coin.getPic());
	tossButton = new JButton("Toss"+"\n");
	
	hLabel = new JLabel("Heads");
	hField = new JTextField("0",5);
	tLabel = new JLabel("Tails");
	tField = new JTextField("0",5);
	pHLabel = new JLabel("%Heads");
	hPerField = new JTextField("",5);
	pTLabel= new JLabel("%Tails");
	tPerField = new JTextField("",5);
	
	topPanel.add(numField);
	topPanel.add(coinLabel);
	topPanel.add(tossButton);
	tossButton.addActionListener(this);
	
	bottomPanel.add(hLabel);
	bottomPanel.add(hField);
	bottomPanel.add(tLabel);
	bottomPanel.add(tField);
	bottomPanel.add(pHLabel);
	bottomPanel.add(hPerField);
	bottomPanel.add(pTLabel);
	bottomPanel.add(tPerField);
	
	Container window = getContentPane();//window
	window.setLayout(layout);
	
	window.add(topPanel, BorderLayout.NORTH);
	window.add(bottomPanel, BorderLayout.SOUTH);
  }
  public void actionPerformed( ActionEvent ae){
	if( ae.getSource() == tossButton)
	{
	  doTossCoins();
	}
  }
  private void doTossCoins()
  {
	String theToss;
	int rolls = random.nextInt(76)+25, i, head=0, tail=0, perH, perT;
	for(i=0; i<rolls; i++)
	{
	  theToss = coin.toss();
	  numField.setText(""+rolls);
	  if(theToss=="HEAD")
	  {
		head++;
		hField.setText(""+head);
		hPerField.setText(""+((head/rolls)*100));
	  }
	  else if(theToss==("TAIL"))
	  {
		tail++;
		tField.setText(""+tail);
		tPerField.setText(""+((tail/rolls)*100));
	  }
	}
  }  
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with setting text to JTextField

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problems with setting text to JTextField

Posted 04 May 2008 - 06:17 PM

View PostC:/Syntax, on 4 May, 2008 - 06:04 PM, said:

The Coin class that I am using works. Basically the class return a string with either "HEAD" or "TAIL".


OK for String coin.getPic();

and what does the toss() method does ?

One thing for sure

	 theToss = coin.toss();
	  numField.setText(""+rolls);
	  if(theToss=="HEAD")
	  {
		head++;
		hField.setText(""+head);
		hPerField.setText(""+((head/rolls)*100));
	  }
	  else if(theToss==("TAIL"))
	  {



That will never worked.....
The values "HEAD" and "TAIL" have been assigned in another class
So theToss is not the same String as "HEAD" or "TAIL" in this class.
The "HEAD" and "TAIL" constants in FourthTest are surely not the same objects as the "HEAD" and "TAIL" constants in Coin.

you should use.
if(theToss.equals("HEAD")) and if(theToss.equals("TAIL"))

This post has been edited by pbl: 04 May 2008 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1