Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Listener trouble

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Listener trouble

    I'm just wondering what is wrong with this? All it needs to do is calculate farenheight from celcius upon pressing the enter key. It's displaying the entered temp but it's not displaying the calculated ceclius temp. I added the test label to verify that setText() was indeed working upon pressing enter(which it does)...mysterious.

    I tried doing something like output.setText(String.format("%.2f", math)); and output.setText("" + celcius); ...What the heck...?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TempetureConvert extends JFrame {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 5966229927379459783L;
    	private JTextField f;
    	private JLabel output;
    	private JLabel test;
    	private String user;
    	private JPanel panel;
    	private double celcius, math;
    	String text;
    	public TempetureConvert(){				
    		f = new JTextField("");
    		f.setPreferredSize(new Dimension(35, 25));				
    		panel = new JPanel();
    		this.setTitle("Temperature Converter");
    		this.setSize(300, 80);
    		output = new JLabel("");
    		test = new JLabel("");
    		panel.add(test);		
    		panel.add(f);
    		panel.add(output);
    		this.add(panel);
    		f.addKeyListener(new KeyAdapter() {
    			 public void keyReleased(KeyEvent e) {
    			      //JTextField f = (JTextField) e.getSource();
    				 if(e.getKeyCode() ==  KeyEvent.VK_ENTER){
    			      math = new Double(f.getText());
    			      test.setText("" + math);
    			      celcius = (5/9)*(math-32);
    			      text = Double.toString(celcius);
    			      output.setText(text);
    				 }
    			 }
     
    			 public void keyTyped(KeyEvent e) {
    			 }
     
    			 public void keyPressed(KeyEvent e) {
    			 }
    	    });
    		this.setVisible(true);
     
    	}	
     
    	public String getUser() {
    		return user;
    	}
     
    	public void setUser(String user) {
    		this.user = user;
    	}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Listener trouble

    Check the integer division: 3/4 = 0 vs 3/4.0 = 0.75
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Sfrius (June 15th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    23
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Listener trouble

    Norm what the heck! That worked perfectly. Funny rule.....

Similar Threads

  1. Having Trouble With My Key Listener
    By Hurleyboarder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 8th, 2014, 05:26 PM
  2. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  3. Listener Configurations in DD
    By tcstcs in forum Java Servlet
    Replies: 0
    Last Post: August 3rd, 2011, 01:47 AM
  4. JButton listener trouble...
    By sparky5783 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 13th, 2011, 01:19 PM
  5. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM