Java Swing Currency Converter
I'm trying to make a simple unit Mass conversion GUI application. I have two combo boxes to select units from, an input box to input a number to convert, and text to display the result of the conversion.
My problem right now is that I'm trying to get the actual push of the button to initiate the calculations, but instead it's getting initiated when I select anything from the list
dropdown combobox. I could enter a number into the textbox, select a unit from the 2nd combo box, and then select something from the first combo box, and it will work because it has the variables it needs.
My code is right below.
I'm trying to make a simple java currency converter GUI. So far I have this:(4 parts) How would I set the values for each item in the jcombbox (ex. Each currency) so that I can use them to calcul. Java Currency Converter. Hi I was wondering if someone could help me write a program to convert Pounds into Euro's. The program should prompt the user to input the number of pounds and output the number of Euros this is equivalent to.
mKorbel1 Answer
You don't want to have the action listener on both lists and the button. Instead, just have it on the button.
The way you have it, the calculation will be performed every time either of the lists is changes (as you've observed). If it's just on the button, it will only be called when the button is clicked.
chamachamaNot the answer you're looking for? Browse other questions tagged javaswingif-statementconverterjcombobox or ask your own question.
I'm trying to make a simple java currency converter GUI. So far I have this :(4 parts)
How would I set the values for each item in the jcombbox (ex. each currency) so that I can use them to calculate the conversion?
Here's the first part (1 combobox):
Aza7 Answers
Maybe try using Map (e.g. HashMap) instead of an array? The key would be a currency name and the value would be currency's value. So instead of:
I would make it:
Michał TaborMichał Taboronly comment, my view about Currency Converter
definition for Currency Pairs, by default is there Base and Variable Currency
definitions for Exange Rate
definition for Buy/Sale
definition for Base/Variable
(put all a.m. points together) then there are four possible combinations
buy Base (EUR 1,000.- at 1.31)
sell Base (EUR 1,000.- at 1.31)
buy Variable (USD 1,000.- at 1.311)
sell Variable (USD 1,000.- at 1.311)
GBP/USD has reverse calculations methods
Currency Converter Oanda
Suggestions of a possible solution:
- I would create a Currency class, one that has a String currencyName field and a double currencyRate field that holds its rate compared to some standard.
- I'd fill my JComboBox's model with objects of Currency.
- I'd give the JCOmboBox a cell renderer so that it shows the Currency name.
- I'd give my GUI a 'convert' JButton
- In that button's action, I'd extract the selected Currencies from both combo boxes by calling
getSelectedItem()
and would use them to calculate an answer. - Make sure that neither of the selected items are null before trying to calculate.
- Or this can be done via an ActionListener added to both combo boxes, but again I'd first have to make sure that the selected values are not null or that the selected indices are not -1.
Create a Currency
class that contains a double value which is the currency's value (you choose how to calculate these).
Have the currency's toString()
return what should be displayed in the combo box, such as 'USD - United States Dollar'.
Now make sure your JComboBox
uses generics, so when you call getSelectedItem()
you don't have to cast it to Currency
, as in new JComboBox<Currency>()
. If you've got your project set on Java 6 you can still use JComboBox generics, even though that was added in Java 7 because of some fancy thing called type erasure. Look it up for details.
I didn't see much in clicking on the URL you provided, so I'm hot sure if this would be an exact fit, but things like this are usually best addressed with Java enum
s. In particular you could use something like the following to store your conversion strings and rates (note that I picked arbitrary conversion rates - substitute the real rates for these):
Then you could add these to your combobox like this:
Java Swing Currency Converter Download
Try this code i have taken indian rupee as the baseit can convertindian rupee us dollarbritish poundjapanese yeneuro
Basically, the way I understand 'Exchange rate' is, how much it costs to exchange one currency for another.
To me, that means that there is a need to associate one currency with another. This can be a 'Currency Pair'
In addition to some of the previous suggestions (eg. Using an enum), as a brute force, a currency pair associated to an exchange rate can be used for the conversion.
For example (Note that the code below will need proper validation and scale/rounding):