Problem with updating a JTextField

Input error for java applet

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2669 Views - Last Post: 15 April 2010 - 06:44 AM Rate Topic: -----

#1 Guest_guest*


Reputation:

Problem with updating a JTextField

Posted 12 April 2010 - 07:36 AM

I am trying to make a temp conversion applet. I keep getting an error in my fahrenheit equation. It is telling me that operator + is undefined for the argument type. Any ideas how to fix this? please help thanks
import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import javax.swing.*;

import java.text.DecimalFormat;

public class RMH12_01 extends Applet implements ActionListener{

	
	JLabel output;
	int temp;
	Button button1;
	JTextField input;
	

  public void init()                       
  {  
	
   add(new Button("Fahrenheit")); 
    button1= new Button();   
    button1.addActionListener(this);
    add( new JTextField(10));
    input= new JTextField();
    input.addActionListener(this);
  }
  
  public void actionPerformed(ActionEvent e){
	  if(e.getSource() == button1){
		  temp= 9.0 / 5.0 * (input + 32);   // this is where my error is!!!!!!!!!!!!! 
	  }
  }
  
}

This post has been edited by pbl: 12 April 2010 - 09:59 PM


Is This A Good Question/Topic? 0

Replies To: Problem with updating a JTextField

#2 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Problem with updating a JTextField

Posted 12 April 2010 - 07:47 AM

Because when it is called, there is no value assigned to input so it is trying to add a JTextField Object to the number 32.

EDIT:
Read this

This post has been edited by citus: 12 April 2010 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Problem with updating a JTextField

Posted 12 April 2010 - 07:47 AM

I would hazard a guess that it's because you're trying to sum a textfield and a number literal. I think what you mean to do is add the value entered IN input to the number literal, which can be accomplished with input.getText(); . You'll need to parse it to an integer: Integer.parseInt(input.getText());. Warning: the code WILL throw an error if the input string is not a numeral. Use try-catch statements.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with updating a JTextField

Posted 12 April 2010 - 10:00 PM

Topic title renamed
Please respect rule #5 of this forum
Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"
"Jave help" is kind of useless in this forum
Was This Post Helpful? 0
  • +
  • -

#5 Guest_guest*


Reputation:

Re: Problem with updating a JTextField

Posted 13 April 2010 - 07:33 AM

View Postxor-logic, on 12 April 2010 - 06:47 AM, said:

I would hazard a guess that it's because you're trying to sum a textfield and a number literal. I think what you mean to do is add the value entered IN input to the number literal, which can be accomplished with input.getText(); . You'll need to parse it to an integer: Integer.parseInt(input.getText());. Warning: the code WILL throw an error if the input string is not a numeral. Use try-catch statements.


with that code is there a specific place to put it because i am still getting an error in the same spot.

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import javax.swing.*;

import java.text.DecimalFormat;

public class RMH12_01 extends Applet implements ActionListener{

	
	JLabel output;
	int temp;
	Button button1;
	JTextField input;
	int num;

  public void init()                       
  {  
	
   add(new Button("Fahrenheit")); 
    button1= new Button();   
    button1.addActionListener(this);
    add( new JTextField(10));
    input.getText();
    num= Integer.parseInt(input.getText());
    input.addActionListener(this);
    
   
  }
  
  public void actionPerformed(ActionEvent e){
	  
	  if(e.getSource() == button1){
		  temp= 9.0 / 5.0 * (num + 32);  
	  }
  }
  
}





Should that be placed in the actionPerformed?
Was This Post Helpful? 0

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problem with updating a JTextField

Posted 13 April 2010 - 07:41 AM

yes. better insert that code in the actionPerformed method.
also, make sure that what you type inside the JTextField is an int value (not double\String etc), or you'll get exceptions.
also, make sure you do type something inside.
Was This Post Helpful? 0
  • +
  • -

#7 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Problem with updating a JTextField

Posted 13 April 2010 - 07:41 AM

Yes, put it in actionPerformed. Did you read the link I posted above also? It should give you a better understanding of the methods of JTextField.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_guest*


Reputation:

Re: Problem with updating a JTextField

Posted 13 April 2010 - 08:00 AM

View Postjapanir, on 13 April 2010 - 06:41 AM, said:

yes. better insert that code in the actionPerformed method.
also, make sure that what you type inside the JTextField is an int value (not double\String etc), or you'll get exceptions.
also, make sure you do type something inside.


Ok i finally got it to show up with no errors. now i need to make the result show up in a jlabel. i think i have the right code but nothing will show up. this is the code i have so far...

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class RMH12_01 extends Applet implements ActionListener{

	
	
	JLabel output;
	double temp;
	Button button1;
	JTextField input;
	int num;

  public void init()                       
  {  
	
   add(new Button("Fahrenheit")); 
    button1= new Button("Fahrenheit");   
    button1.addActionListener(this);
    add( new JTextField(10));
    add(new JLabel(""));
    output= new JLabel();
    
    
   
  }
  
  public void actionPerformed(ActionEvent e){
	    input.getText();
	    input.addActionListener(this);
	    num= Integer.parseInt(input.getText());
	    DecimalFormat oneDigit = new DecimalFormat("0.0");
		  output.setText(oneDigit.format(temp));
		  
	  if(e.getSource() == button1){
		  temp = 9.0 / 5.0 * (num + 32);
		  
	  }
	  
  }
  
}





It is not displaying any errors but is not displaying the result in the applet.
Was This Post Helpful? 0

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problem with updating a JTextField

Posted 13 April 2010 - 08:14 AM

why all that code in the actionPerformed method?
type all code that you want to execute if the button1 is pressed inside the if block.
move all declaration of variables that you may use in other methods out of the actionPerformed.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Problem with updating a JTextField

Posted 13 April 2010 - 02:41 PM

The reason it isn't displaying is this:
 output.setText(oneDigit.format(temp));



temp has no value...yet...move it below the claulation

Also, since you want the number to look like a decimal, you will want to Parse to a double, not an int:

public void actionPerformed(ActionEvent e){
            // as japanir said, these lines are useless...
            //input.getText();
            //input.addActionListener(this);

            double num;

            try {
                num = Double.parseDouble(input.getText());
            } catch (NumberFormatException e) {
                // Let user know that they must enter a double.  
                return; 
            }
            DecimalFormat oneDigit = new DecimalFormat("0.0");
                  
            // You should move your if to encompass all of this code instead.
            temp = 9.0 / 5.0 * (num + 32);    
           
            // Now that temp has a value, format it.
            output.setText(oneDigit.format(num));
          
  }


Was This Post Helpful? 0
  • +
  • -

#11 sumit kothalikar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 10

Re: Problem with updating a JTextField

Posted 13 April 2010 - 02:48 PM

View Postguest, on 12 April 2010 - 06:36 AM, said:

<snip>


Edited byDogstopper: useless post interrupted the flow of posts.

This post has been edited by Dogstopper: 13 April 2010 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Problem with updating a JTextField

Posted 13 April 2010 - 03:02 PM

sumit kothalikar, please don't quote someone's post and have NOTHING to say about it... thanks...
Was This Post Helpful? 0
  • +
  • -

#13 Guest_guest*


Reputation:

Re: Problem with updating a JTextField

Posted 14 April 2010 - 07:10 AM

View PostDogstopper, on 13 April 2010 - 01:41 PM, said:

The reason it isn't displaying is this:
 output.setText(oneDigit.format(temp));



temp has no value...yet...move it below the claulation

Also, since you want the number to look like a decimal, you will want to Parse to a double, not an int:

public void actionPerformed(ActionEvent e){
            // as japanir said, these lines are useless...
            //input.getText();
            //input.addActionListener(this);

            double num;

            try {
                num = Double.parseDouble(input.getText());
            } catch (NumberFormatException e) {
                // Let user know that they must enter a double.  
                return; 
            }
            DecimalFormat oneDigit = new DecimalFormat("0.0");
                  
            // You should move your if to encompass all of this code instead.
            temp = 9.0 / 5.0 * (num + 32);    
           
            // Now that temp has a value, format it.
            output.setText(oneDigit.format(num));
          
  }



I think i have the code like you said to put it but it is still not displaying the answer in the Jlabel. Is there a specific way i should be calling the output so that it should be displayed in the Jlabel on the applet? also i am receiving an error after the catch. it is telling me to duplicate parameter e but i do not know what that means.

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class RMH12_01 extends Applet implements ActionListener{

	
	
	JLabel output;
	double temp;
	Button button1;
	JTextField input;
	

  public void init()                       
  {  
	
   add(new Button("Fahrenheit")); 
    button1= new Button("");   
    button1.addActionListener(this);
    add( new JTextField(10));
    add(new JLabel(""));
    output= new JLabel();
    
    
   
  }
  
  public void actionPerformed(ActionEvent e){ //// also i am receiving an error on this line 
	   
	    double num;
	    if(e.getSource() == button1){
	    try {
            num = Double.parseDouble(input.getText());
        } catch (NumberFormatException e) {
            // Let user know that they must enter a double.  
            return; 
        }
        DecimalFormat oneDigit = new DecimalFormat("0.0");
	  
		  temp = 9.0 / 5.0 * (num + 32);
		
		  
		  output.setText(oneDigit.format(temp));
	  }
	  
  }
  
}


Was This Post Helpful? 0

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Problem with updating a JTextField

Posted 14 April 2010 - 07:37 AM

You keep randomly adding anonymous elements to your Applet. when you say add(new JLabel());, there is no way to reference that JLabel later. You have to instantiate and add elements that you can use later. Also, name the exception something other than e:

    import java.awt.*; 
    import java.applet.*; 
    import java.awt.event.*;
    import javax.swing.*;

import java.text.DecimalFormat;

    public class RMH12_01 extends Applet implements ActionListener{

            
            
            JLabel output;
            double temp;
            Button button1;
            JTextField input;
            

      public void init()                       
      {  
             
        button1= new Button("");   
        button1.addActionListener(this);
        add(button1);
        input = new JTextField(10);
        add( input);
        output= new JLabel("");
        add(output);
        
        
       
      }
      
      public void actionPerformed(ActionEvent e){ //// also i am receiving an error on this line 
               
        double num;
        if(e.getSource() == button1){
            try {
            	num = Double.parseDouble(input.getText());
            } catch (NumberFormatException exception) {
            	JOptionPane.showMessageDialog(this, "Has to be a double");
            	return; 
            }
            DecimalFormat oneDigit = new DecimalFormat("0.0");
          
            temp = (9.0 / 5.0) * (num + 32);
                      
            output.setText(oneDigit.format(temp));
         }
          
      }
      
   }


Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Problem with updating a JTextField

Posted 14 April 2010 - 07:57 AM

dogstopper said:

You have to instantiate and add elements that you can use later.

I'd like to expand on what Dogstopper said. If you want to use Components later, then you really should declare them before using them, as it is a pain to hunt through your Container to obtain them. Nine out of ten times, you will not have a Component if you do not plan on modifying its display. Even if you are not modifying it in this class or this program, you may want to extend the class and use those Components in the subclass. Anonymous objects inhibit this.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2