Java Swing GUI–Currency Converter

Standard

       This is a Java Swing GUI program for currency conversion. So basically here we convert one currency to other or vise versa. For detailed explanation of how swing works checkout my previous post;

   Here as usual we create a JApplet class and implement an Action Listener. Here we require two text fields, two Combo box, two buttons and offcourse four labels. The rest are heard of but the JComboBox is new.Convc Conce

It is quite similar to a dropdown list only we have to specify the elements included in it, as String array.Here is how you will initialize them;

 

    JComboBox c1,c2;
    String[] currency={"US Dollar","Indian Rupee","British Pound","Euro","Canadian Dollar","Emirati Driham","Chinese Yuan"};
  c1=new JComboBox(currency);
  c1.setSelectedIndex(0);
  c2=new JComboBox(currency);
  c2.setSelectedIndex(1);

 

     Now as you seen in the String array we have all the elements to be shown in the combo box that is the seven currencies. Then we initialize the combo box to String array ‘currency’ and set its index to 0, that is by default it will show up the element at index zero, in this case ‘US Dollar’.

    Here also, to design the GUI we use the GridBagLayout. We position the components as per our requirement and add them on the frame and lastly assign Action Listener to the components with which we are going to interact.

    Now when we complete our GUI we go for action performed; where we specify what happens when the user interacts with a particular component. We first get the amount from the first text field that has to be converted. Very important thing here is that any text field stores the data within it as a string so, to perform mathematical operation on the value we need to convert it from string to double.

    a=Double.valueOf(t1.getText());

 

    This step here is quite important where we need to decide from which currency to which currency we need to convert the amount. The string array here comes in handy. As we read the string array on the basis of index lets us consider; if the user selects index 2 at the combo box c1 and selects index 5 at the combo box c2; that means we need to convert British pounds to Emirati Dirham; so it performs that mathematical operation within the if condition. For example,

if(c1.getSelectedIndex()==2 & c2.getSelectedIndex()==5){
c= a*6.170453;}

 

Similarly to perform all the currency conversion we need to consider all the combinations of the currencies with each other except with itself, that is have to consider almost forty-two combinations. Then again to display this result in the second text field we need to convert the double value to string.

     t2.setText(String.valueOf(c));

 

You can also add the Clear button to clear the text fields;

if(ae.getSource()==b2){
  t1.setText("0000");
  t2.setText("0000");}

Note: The conversion rates were dated on 19th of April 2014 and obviously must have changed the very next day so you need to tinker with that formula if you want accuracy. Conversion rates were obtained from X-Rates.

  • Download the source code of this program here:

Download

     If you have any improvements in this program to make it short and simple or have any doubts regarding this program please let me know by commenting below.

Advertisements

One thought on “Java Swing GUI–Currency Converter

Leave a Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s