Results 1 to 6 of 6
- 07-29-2009, 10:02 PM #1
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
-
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 10:44 PM.
- 07-29-2009, 11:05 PM #3
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
-
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.
already explained: You are using the wrong listener.BTW this code should work, but i dont know why it does not????? can u explain???
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-29-2009 at 11:34 PM.
- 07-29-2009, 11:49 PM #5
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
-
Similar Threads
-
Currency Converter!!!!
By Pascal Nouma in forum AWT / SwingReplies: 2Last Post: 04-02-2009, 07:23 AM -
usb to serial converter
By freezy in forum Advanced JavaReplies: 2Last Post: 03-18-2009, 10:48 AM -
sound converter WAV to AU
By Unome in forum Java AppletsReplies: 5Last Post: 03-14-2009, 10:25 AM -
YaHP Converter 1.2.17
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-06-2007, 03:11 PM -
YaHP Converter 1.2.12
By levent in forum Java SoftwareReplies: 0Last Post: 06-12-2007, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks