Results 1 to 6 of 6
  1. #1
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Question I need some help with converter GUI

    Hi, i am working on a converter that converts celsius into fahrenheit, and the other way around. here's the code i got so far it works, but all it does is displays the GUI. now i am trying to get it to actualy work, but i am having some trouble understanding how itemlistener works... I wrote a private class TempListener, but it's not working.......

    Can someone please tell me how to get this to work:confused:

    -- Thanks

    Java Code:
    /*
     * 	TempConverter.java		Developed by: ******************
     * 
     * 	A program that converts celsius into fahrenheit,
     * 	and the other way around.
     */
     
    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    public class TempConverter
    {
    	private class TextPanel extends JPanel
    	{
    		public TextPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    			
    			JTextField field = new JTextField();
    			
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(field);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    		}
    	}
    	
    	private class ButtonPanel extends JPanel
    	{
    		public ButtonPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    			
    			JButton toCelsius = new JButton(" To Celsius  ");
    			JButton toFahrenheit = new JButton("To Fahrenheit");
    			
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(toCelsius);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(toFahrenheit);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    		}
    	}
    	
    	private class TempConverterPanel extends JPanel
    	{
    		public TempConverterPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    			
    			TextPanel txtPanel = new TextPanel();
    			ButtonPanel buttonPanel = new ButtonPanel();
    			
    			add(Box.createRigidArea(new Dimension(0, 5)));
    			add(txtPanel);
    			add(Box.createRigidArea(new Dimension(0, 5)));
    			add(buttonPanel);
    			add(Box.createRigidArea(new Dimension(0, 5)));
    		}
    	}
    	/*
    	private class TempListener implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent event)
    		{
    			String ans;
    			
    			if (toCelsius.isSelected() == true)
    			{
    				String text = field.getText();
    				double f = Double.parseDouble(text);
    				ans = fINTOc(f);
    			}
    			if (toFahrenheit.isSelected() == true)
    			{
    				String text = field.getText();
    				double c = Double.parseDouble(text);
    				ans = cINTOf(c);
    			}
    			
    			field.setText(ans);
    		}
    	}
    	*/
    	public String cINTOf(double c) 
    	{
    		double f = ((1.8 * c) + 32);
    		String ans = ("" + f);
    		return ans;
    	}
    	
    	public String fINTOc(double f)
    	{
    		double c = ((f / 1.8) - 32);
    		String ans = ("" + f);
    		return ans;
    	} 
    	
    	public void run()
    	{
    		JFrame frame = new JFrame ("Temp Converter");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		TempConverterPanel panel = new TempConverterPanel();
    		BufferedImage image = null;
    		
    		try
    		{
    			image = ImageIO.read(frame.getClass().getResource("/temp.gif"));
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    		
    		frame.setIconImage(image); 
    		frame.add(panel);
    		frame.pack();
    		frame.setResizable(false);
    		frame.show();
    	}
    	
    	public static void main(String[]args)
    	{
    		TempConverter converter = new TempConverter();
    		converter.run();
    	}
    }
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't want an ItemListener which is used for lists such as combo boxes but rather you'll want to use an ActionListener.

    In your ActionListener inner class's actionPerformed method, you'll first want to find out which button was pressed either by extracting the actionCommand String from the ActionEvent passed as a parameter (there's a simple getter method for this). Then using String.equals(...) testing, or you can get a reference to the button pressed itself via ActionEvent's getSource() method.

    Also, don't forget of course to add your ActionListener to both of the buttons (of course).
    Last edited by Fubarable; 07-29-2009 at 11:44 PM.

  3. #3
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    in the java book i am using alot of examples use itemlistener....

    BTW this code should work, but i dont know why it does not????? can u explain???

    Java Code:
    /*
     * 	TempConverter.java		Developed by: ******************
     * 
     * 	A program that converts celsius into fahrenheit,
     * 	and the other way around.
     */
     
    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    public class TempConverter
    {
    	private JTextField field = new JTextField();
    	private JButton toCelsius = new JButton(" To Celsius  ");
    	private JButton toFahrenheit = new JButton("To Fahrenheit");
    	
    	private class TextPanel extends JPanel
    	{
    		public TextPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(field);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    		}
    	}
    	
    	private class ButtonPanel extends JPanel
    	{
    		public ButtonPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    			
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(toCelsius);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    			add(toFahrenheit);
    			add(Box.createRigidArea(new Dimension(5, 0)));
    		}
    	}
    	
    	private class TempConverterPanel extends JPanel
    	{
    		public TempConverterPanel()
    		{
    			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    			
    			TextPanel txtPanel = new TextPanel();
    			ButtonPanel buttonPanel = new ButtonPanel();
    			
    			toCListener clistener = new toCListener();
    			toFListener flistener = new toFListener();
    			
    			toCelsius.addItemListener(clistener);
    			toFahrenheit.addItemListener(flistener);
    			
    			add(Box.createRigidArea(new Dimension(0, 5)));
    			add(txtPanel);
    			add(Box.createRigidArea(new Dimension(0, 5)));
    			add(buttonPanel);
    			add(Box.createRigidArea(new Dimension(0, 5)));
    		}
    	}
    	
    	private class toCListener implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent event)
    		{
    			String ans = "";
    			String text = field.getText();
    			double f = Double.parseDouble(text);
    			ans = fINTOc(f);
    			field.setText(ans);
    		}
    	}
    	
    	private class toFListener implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent event)
    		{
    			String ans = "";
    			String text = field.getText();
    			double c = Double.parseDouble(text);
    			ans = cINTOf(c);
    			field.setText(ans);
    		}
    	}
    	
    	public String cINTOf(double c) 
    	{
    		double f = ((1.8 * c) + 32);
    		String ans = ("" + f);
    		return ans;
    	}
    	
    	public String fINTOc(double f)
    	{
    		double c = ((f / 1.8) - 32);
    		String ans = ("" + f);
    		return ans;
    	} 
    	
    	public void run()
    	{
    		JFrame frame = new JFrame ("Temp Converter");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		TempConverterPanel panel = new TempConverterPanel();
    		BufferedImage image = null;
    		
    		try
    		{
    			image = ImageIO.read(frame.getClass().getResource("/temp.gif"));
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    		frame.setIconImage(image); 
    		frame.add(panel);
    		frame.pack();
    		frame.setResizable(false);
    		frame.show();
    	}
    	
    	public static void main(String[]args)
    	{
    		TempConverter converter = new TempConverter();
    		converter.run();
    	}
    }
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by linux1man View Post
    in the java book i am using alot of examples use itemlistener....
    Please show me one example, any example, where you see an ItemListener being used with a JButton. Then please try my recommendations about using the correct listener, an ActionListener.

    BTW this code should work, but i dont know why it does not????? can u explain???
    already explained: You are using the wrong listener.

    If you still don't believe me, then please read the ItemListener tutorial to see where it is used: How to Write an Item Listener (The Java™ Tutorials > Creating a GUI with JFC/Swing > Writing Event Listeners)

    Then read the tutorial on ActionListeners to see where and how to use them: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI with JFC/Swing > Writing Event Listeners)

    Best of luck.

    For e.g., here is a small app with three JButtons, the first two sharing the same ActionListener, the last using an ItemListener. See which listener responds to the events:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FuSwing3 {
      public static final String FUBAR = "FUBAR Button, uses ActionListener";
      public static final String SNAFU = "SNAFU Button, uses ActionListener";
      public static final String TARFU = "TARFU Button, uses ItemListener";
      
      private static void createAndShowGUI() {
        JPanel panel = new JPanel(new GridLayout(1, 0, 10, 0));
        ActionListener btnListener = new ButtonListener();
        
        JButton fubarBtn = new JButton(FUBAR);
        fubarBtn.addActionListener(btnListener);
        panel.add(fubarBtn);
        
        JButton snafuBtn = new JButton(SNAFU);
        snafuBtn.addActionListener(btnListener);
        panel.add(snafuBtn);
        
        JButton tarfuBtn = new JButton(TARFU);
        tarfuBtn.addItemListener(new MyItemListener());
        panel.add(tarfuBtn);
        
        JFrame frame = new JFrame("FuSwing3 Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }
    
    class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals(FuSwing3.FUBAR)) {
          System.out.println("Fubar!!!");
        } else if (command.equals(FuSwing3.SNAFU)) {
          System.out.println("Snafu!!!");
        }
      }
    }
    
    class MyItemListener implements ItemListener {
    
      public void itemStateChanged(ItemEvent arg0) {
        System.out.println("Tarfu!!!");
      }
      
    }
    Last edited by Fubarable; 07-30-2009 at 12:34 AM.

  5. #5
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    ok, I got it to work.
    in the book it was used with checkboxes..... bo wonder it did not work with JButton (I did not know you cant use it for both)

    Thanks for the help!
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 08:23 AM
  2. usb to serial converter
    By freezy in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2009, 11:48 AM
  3. sound converter WAV to AU
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 03-14-2009, 11:25 AM
  4. YaHP Converter 1.2.17
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 04:11 PM
  5. YaHP Converter 1.2.12
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •