Java Swing Currency Converter

Posted on
  1. Currency Converter Oanda
  2. Java Swing Currency Converter Download

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.

mKorbel
105k14 gold badges112 silver badges275 bronze badges
ay lmaoay lmao

1 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.

chamachama
2,75512 gold badges48 silver badges70 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not 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):

Aza
4,5478 gold badges37 silver badges65 bronze badges
user2348024user2348024

7 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ł TaborJava currency converterMichał Tabor
1,5114 gold badges13 silver badges28 bronze badges

only comment, my view about Currency Converter

  1. definition for Currency Pairs, by default is there Base and Variable Currency

  2. definitions for Exange Rate

  3. definition for Buy/Sale

  4. definition for Base/Variable

  5. (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)

  6. GBP/USD has reverse calculations methods

mKorbelmKorbel

Currency Converter Oanda

105k14 gold badges112 silver badges275 bronze badges

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.
Hovercraft Full Of EelsHovercraft Full Of Eels
264k20 gold badges216 silver badges321 bronze badges

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.

Leo IzenLeo Izen
2,5737 gold badges26 silver badges52 bronze badges

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 enums. 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:

paulk23paulk23

Java Swing Currency Converter Download

Try this code i have taken indian rupee as the baseit can convertindian rupee us dollarbritish poundjapanese yeneuro

Siddharth STSiddharth ST

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):

razboyrazboy

Not the answer you're looking for? Browse other questions tagged javaswingconverterjcomboboxcurrency or ask your own question.