JAVA: how to use listeners on JMenuItem and Jbutton to calculate rates?

You are about to gain a massive advantage that can increase your earnings by 364% in as little as 1 hour, and best of all – it doesn’t require PPC, SEO or ANY other traffic generation methods. Get it now!

Try this out with the Euros. Should give you a place to get started Currency converting * */ import javax.swing. JFrame; import javax.swing.

JLabel; import javax.swing. JPanel; import javax.swing. JButton; import javax.swing.

JTextField; import java.awt. GridLayout; import java.awt.event. ActionEvent; import javax.swing.

JComboBox; import javax.swing. UIManager; public class CurrencyConverterWin extends JFrame { private JLabel promptLabel; private JTextField amountField; private JButton goButton; private JPanel inputPanel; private JPanel answerPanel; private JLabel answerLabel; private JLabel selectLabel; private JComboBox currencyMenuBar; private JPanel menuPanel; private double result = 0.0; private double euro = 1.22257; private double japYen = 152.073; private double rusRuble = 42.5389; private double usd = 1.5577; public CurrencyConverterWin() { super(); this. SetSize(500, 200); this.

SetTitle("Currency Converter Window"); this. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); this.

SetLayout(new GridLayout(3, 1)); this. SelectLabel = new JLabel("Select a currency to convert to: ", JLabel. RIGHT); this.

AnswerLabel = new JLabel(" ", JLabel. CENTER); currencyMenuBar = new JComboBox(new String{"Euros","Japanese Yen","Russian Rubles","US Dollars"}); this. MenuPanel = new JPanel(); this.menuPanel.

Add(this. SelectLabel); this.menuPanel. Add(this.

CurrencyMenuBar); this. Add(this. MenuPanel); this.

PromptLabel = new JLabel("(select a currency first) ", JLabel. RIGHT); this. AnswerLabel = new JLabel(" ", JLabel.

CENTER); this. AmountField = new JTextField("", 8); this. GoButton = new JButton("GO"); goButton.

AddActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event. ActionEvent evt) { buttonClicked(evt); } }); this. InputPanel = new JPanel(); this.inputPanel.

Add(this. PromptLabel); this.inputPanel. Add(this.

AmountField); this.inputPanel. Add(this. GoButton); this.

Add(this. InputPanel); this. AnswerPanel = new JPanel(); this.answerPanel.

Add(this. AnswerLabel); this. Add(this.

AnswerPanel); } double EuroToSterling() { double total = Double. ParseDouble(amountField.getText()) * euro; return total; } double JapYenToSterling() { double japToSterlingTotal = Double. ParseDouble(amountField.getText()) * japYen; return japToSterlingTotal; } //String currencyEntered = yearField.getText(); public void buttonClicked(ActionEvent evt) { if(currencyMenuBar.getSelectedItem().

Equals("Euros")) { answerLabel. SetText(EuroToSterling() + " Euros"); } if(currencyMenuBar.getSelectedItem(). Equals("Japanese Yen")) { answerLabel.

SetText(JapYenToSterling() + " Japanese Yen"); } } public static void main(String args) { try{UIManager. SetLookAndFeel("com.sun.java.swing.plaf.nimbus. NimbusLookAndFeel");} catch (Exception e){e.printStackTrace();} CurrencyConverterWin win = new CurrencyConverterWin(); win.

SetVisible(true); } }.

Try this out with the Euros. Should give you a place to get started. /* * * Currency converting * */ import javax.swing.

JFrame; import javax.swing. JLabel; import javax.swing. JPanel; import javax.swing.

JButton; import javax.swing. JTextField; import java.awt. GridLayout; import java.awt.event.

ActionEvent; import javax.swing. JComboBox; import javax.swing. UIManager; public class CurrencyConverterWin extends JFrame { private JLabel promptLabel; private JTextField amountField; private JButton goButton; private JPanel inputPanel; private JPanel answerPanel; private JLabel answerLabel; private JLabel selectLabel; private JComboBox currencyMenuBar; private JPanel menuPanel; private double result = 0.0; private double euro = 1.22257; private double japYen = 152.073; private double rusRuble = 42.5389; private double usd = 1.5577; public CurrencyConverterWin() { super(); this.

SetSize(500, 200); this. SetTitle("Currency Converter Window"); this. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); this. SetLayout(new GridLayout(3, 1)); this. SelectLabel = new JLabel("Select a currency to convert to: ", JLabel.

RIGHT); this. AnswerLabel = new JLabel(" ", JLabel. CENTER); currencyMenuBar = new JComboBox(new String{"Euros","Japanese Yen","Russian Rubles","US Dollars"}); this.

MenuPanel = new JPanel(); this.menuPanel. Add(this. SelectLabel); this.menuPanel.

Add(this. CurrencyMenuBar); this. Add(this.

MenuPanel); this. PromptLabel = new JLabel("(select a currency first) ", JLabel. RIGHT); this.

AnswerLabel = new JLabel(" ", JLabel. CENTER); this. AmountField = new JTextField("", 8); this.

GoButton = new JButton("GO"); goButton. AddActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event. ActionEvent evt) { buttonClicked(evt); } }); this.

InputPanel = new JPanel(); this.inputPanel. Add(this. PromptLabel); this.inputPanel.

Add(this. AmountField); this.inputPanel. Add(this.

GoButton); this. Add(this. InputPanel); this.

AnswerPanel = new JPanel(); this.answerPanel. Add(this. AnswerLabel); this.

Add(this. AnswerPanel); } double EuroToSterling() { double total = Double. ParseDouble(amountField.getText()) * euro; return total; } double JapYenToSterling() { double japToSterlingTotal = Double.

ParseDouble(amountField.getText()) * japYen; return japToSterlingTotal; } //String currencyEntered = yearField.getText(); public void buttonClicked(ActionEvent evt) { if(currencyMenuBar.getSelectedItem(). Equals("Euros")) { answerLabel. SetText(EuroToSterling() + " Euros"); } if(currencyMenuBar.getSelectedItem().

Equals("Japanese Yen")) { answerLabel. SetText(JapYenToSterling() + " Japanese Yen"); } } public static void main(String args) { try{UIManager. SetLookAndFeel("com.sun.java.swing.plaf.nimbus.

NimbusLookAndFeel");} catch (Exception e){e.printStackTrace();} CurrencyConverterWin win = new CurrencyConverterWin(); win. SetVisible(true); } }.

But I'm having a little problem with other currencies it seems to only work for "euro" please have a look at the change i've made: else if(currencyMenu.getText(). Equals("Japanes Yen")) { answerLabel. SetText(JapYenToSterling(Double.

ValueOf(amountField.getText())) + "Japanese Yen"); } } – Maxi90 Dec 8 '10 at 18:51 double JapYenToSterling(double japYen){ double japToSterlingTotal = Double. ParseDouble(amountField.getText()) * 152.07; return japToSterlingTotal; } – Maxi90 Dec 8 '10 at 18:53 Ive updated the code above to reflect the JapYenToSterling(). The only parts of the code that you should touch are the buttonClicked function and add your conversion functions.

Other than that, you program is fine. Just look into what is being done with the Euro and the Yen. All other conversion are done in exactly the same way.

– user489041 Dec 8 '10 at 19:10 Also, as others have suggested, you should be using a JComboBox. I have edited the code above again. Look how much simpler your code can be if you use the correct Swing component for the job.

– user489041 Dec 8 '10 at 19:21 thank you very much for the help. I really appreciate it. – Maxi90 Dec 8 '10 at 20:45.

The usual approach is that the menu listener changes the state of the application (i.e. Calls a method that will save the exchange rate in a field). Then the calculation code can read this value and use it.

I would also suggest you use a JComboBox to store the currencies. You would create an object to store both the currency name and the conversion rate. Then when you need to calculate the converted amount you get the selected item from the combo and use its conversion rate in your calculation.

With this approach you can easily expand the number of currencies you support. Check out: How to use Map element as text of a JComboBox for an example to get you start on using an object in the combo box.

I would personally add in an Enumeration to denote the currency conversion type. Eg: public enum ConversionType { DOLLARS, EUROS, RUBLES //ETC... } Using this, you can keep a state variable in the class: private ConversionType fromType; This is what you set in your selection listener. From there it's a matter of doing the different conversions in your action listener based on the state variable (fromType).

Something like this: if( fromType== EUROS ) { convertEurosToSterling( value1, value2 ); } This is sort of a general approach - I hope this helps.

ThanksI forgot to mention that I'm converting any selected currency to sterling. How would I do that with your solution? – Maxi90 Dec 8 '10 at 17:03 I'll edit it towards that.

– javamonkey79 Dec 8 '10 at 18:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions