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: Java Swing:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Java Swing:

     
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.border.Border;
     
    public class TempConvertor extends JFrame implements ActionListener
    {
    	JButton Convert;
    	JButton Reset;
    	JTextField Cel;
    	JTextField Fah;
    	JLabel CelLbl;
    	JLabel farenheight;
     
    	TempConvertor()
    	{
    		JFrame j = new JFrame("Temp Convertor");
    		j.setSize(200,175);
    		j.setResizable(false);
    		j.setLayout(new FlowLayout());
    		j.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		//create buttons
    		Convert = new JButton("Convert");
    		Reset = new JButton("Reset");
     
    		Convert.addActionListener(this);
    		Reset.addActionListener(this);
     
    		//create labels
    		CelLbl = new JLabel("Celsius");
    		farenheight = new JLabel("Farenheight");
     
    		CelLbl.setVerticalAlignment(SwingConstants.BOTTOM);
    		farenheight.setVerticalAlignment(SwingConstants.BOTTOM);
    		Border border = BorderFactory.createEtchedBorder();
    		CelLbl.setBorder(border);
    		farenheight.setBorder(border);
     
    		JPanel cp = ((JPanel) j.getContentPane());
    		cp.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
     
    		//create text fields
    		Cel = new JTextField(5);
    		Fah = new JTextField(5);
     
    		Cel.setActionCommand("Cel");
    		Fah.setActionCommand("Fah");
     
    		//add TextFields
    		j.add(CelLbl);
    		j.add(Cel);
    		j.add(farenheight);//label
     
    		j.add(Fah);
     
    		//add Buttons
    		j.add(Convert);
    		j.add(Reset);
     
    		j.setVisible(true);	
    	}
     
    	public void actionPerformed(ActionEvent ae)
    	{
    		if(ae.getActionCommand().equals("Convert"))
    		{
    			if(Cel != null)
    			{
    			//convert to Cel
    			int jml = Integer.parseInt(Cel.getText());
    			int result = jml * 9/5 + 32;
    			Fah.setText(Integer.toString(result));
    			}
    			else if(Fah != null)
    			{
    				//convert to Fah
    				int jml = Integer.parseInt(Fah.getText());
    				int result = (jml - 32) * 5/9;
    				Cel.setText(Integer.toString(result));
    			}
    		}
    		else
    		{
    			Cel.setText("");
    			Fah.setText("");
    		}
    	}
     
     
    	public static void main(String[] args)
    	{
    		new TempConvertor();
    	}
    }

    I am learning java swing and I am having an issue on line 78. I want to convert Celsius to Fahrenheit which works. However when I try to convert F to C I get the following errors:

    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

    I thought my if statement would handle this for me but only my Cel JTextField seems to be working. Any Advice?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Swing:

    You have some minor confusion about what Cel and Fah are. First, since neither is a class name, their names should begin with lowercase letters as in cel and fah. The same is true of most (all?) of your other variable names.

    Even without the name correction, Cel and Fah are objects, instances of JTextField and will never be null once they are properly initialized. Your if conditions checking if they are not null will always return true. What I believe you mean to do is to check if the text assigned to or in the JTextFields Cel and Fah is empty, something like:

    if( !Cel.getText().equals( "" ) )

    There are other logic issues to clean up after you've made those changes, but this should get you on the way.

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

    jocdrew21 (May 17th, 2014)

  4. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Java Swing:

    Yes that is what I want trying to do. Thank you very much.

Similar Threads

  1. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  2. Java Swing
    By arvin17 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 09:40 AM
  3. [SOLVED] Java Swing
    By Thamizharasan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 10th, 2013, 07:33 AM
  4. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  5. java swing help
    By JM_4ever in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 06:42 AM