Saturday, April 27, 2013

Currency Conversion program

This is a simple currency conversion program. The program allows you to convert from USD amount to Riel amount and vise versa and from EURO amount to USD amount and vise versa. In this program, you will learn to use  the JTabbedPane class, and JComboBox lass, and to write Java code to detect key typed.

CurrencyConversion source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class  CurrencyConversion extends JFrame implements ActionListener{
            JTabbedPane tbpane;
            JPanel panelt1;
            JLabel lblus;
            JLabel lblkh;
            JLabel lblcbkh;
            JTextField txtus;
            JTextField txtkh;
            JComboBox cbratekh;
            JButton btuskh;
            JPanel panelt2;
            JLabel lbleuro;
            JLabel lbleurous;
            JLabel lblcbus;
            JTextField txteurous;
            JTextField txteuro;
            JComboBox cbrateus;
            JButton bteurous;   

1                      setTitle("Currency Conversion Program");
2                      setDefaultCloseOperation(EXIT_ON_CLOSE);
3                      setSize(300,250);
4                      setResizable(false);
5                      Container cont=getContentPane();

                        //USD to Riel conversion interface
6                      lblus=new JLabel("USD:");
7                      lblkh=new JLabel("Riel:");
8                      lblcbkh=new JLabel("Select exchange rate:");
9                      btuskh=new JButton("OK");
10                    btuskh.addActionListener(this);               
11                    txtus=new JTextField(10);
12                    txtus.addKeyListener(new KeyList());
13                    txtkh=new JTextField(10);
14                    txtkh.addKeyListener(new KeyList());
15                    Object[] ratekh={3800,3900,4000,4100,4200};
16                    cbratekh=new JComboBox(ratekh);
17                    cbratekh.setEditable(true);
18                    cbratekh.getEditor().getEditorComponent().addKeyListener(new KeyList());
19                    panelt1=new JPanel();
20                    panelt1.add(lblcbkh);
21                    panelt1.add(cbratekh);
22                    panelt1.add(lblus);
23                    panelt1.add(txtus);
24                    panelt1.add(lblkh);
25                    panelt1.add(txtkh);
26                    panelt1.add(btuskh);
27                    panelt1.setLayout(new GridLayout(4,1));
                        //EURO and USD conversion interface
28                    lbleuro=new JLabel("Euro:");
29                    lbleurous=new JLabel("USD:");
30                    lblcbus=new JLabel("Select exchange rate:");
31                    bteurous=new JButton("OK");
32                    bteurous.addActionListener(this);                       
33                    txteuro=new JTextField(10);
34                    txteuro.addKeyListener(new KeyList());
35                    txteurous=new JTextField(10);
36                    txteurous.addKeyListener(new KeyList());
37                    Object[] rateus={1.25,1.28,1.30,1.32,1.35,1.40};
38                    cbrateus=new JComboBox(rateus);
39                    cbrateus.setEditable(true);
40                    cbrateus.getEditor().getEditorComponent().addKeyListener(new KeyList());
41                    panelt2=new JPanel();
42                    panelt2.setLayout(new GridLayout(4,1));
43                    panelt2.add(lblcbus);
44                    panelt2.add(cbrateus);
45                    panelt2.add(lbleuro);
46                    panelt2.add(txteuro);
47                    panelt2.add(lbleurous);
48                    panelt2.add(txteurous);
49                    panelt2.add(bteurous);
                        //Create JTabbedPane object and add panelt1 and panelt2
50                    JTabbedPane tbpane=new JTabbedPane();
51                    tbpane.add(panelt1, null, 0);
52                    tbpane.add(panelt2, null, 1);
                        //Set title to each tab
53                    tbpane.setTitleAt(0,"USD and Riel");
54                    tbpane.setTitleAt(1,"EURO and USD");

                        //Add tbpane JTabbedPane object to the container
55                    cont.add(tbpane);
56                    setVisible(true);                   

57 public void actionPerformed(ActionEvent e){
                        double rate;
                        double usdamount;
                        double khamount;
                        double euroamount;
                                    if(!checkBlank(txtus,txtkh, cbratekh))
                                                if(txtkh.getText().equals("")){ //USD to Riel conversion

                                                else if(txtus.getText().equals("")) { //Riel to USD conversion
                                                showMessage("Please leave the result text box blank");
                        else if(e.getSource()==bteurous){
                                    if(!checkBlank(txteurous,txteuro, cbrateus))
                                                if(txteurous.getText().equals("")){ //Euro to USD conversion

                                                else if(txteuro.getText().equals("")) { // USD to EURO conversion
                                                showMessage("Please leave the result text box blank");

Currency Conversion program in Java

Code Explanation:
1 Set the title of the program window (Currency Conversion Program).
2 Let the program window close when the user clicks the close button.
3 Specify the width and height of the program window when it opens.
4 Disable program window resizing.
5 Create a container object to act as the controls placeholder.
6-27 Construct the interface for USD and Riel conversion. We need three labels, one combobox, two text boxes, and one button. These controls are grouped together in the panelt1 object. The combobox allows the user to select conversion rate. The text boxes are for inputting USD or Riel  amount. Inputting in both text boxes in one operation is not valid. If you input USD, the conversion will be made from USD currency to Riel  currency and vise versa. The two text boxed are registered to the key event so that restriction can be placed on its key typed. The button object is registered to the action event to enable button-click action.
28-49 The construction of  the interface for EURO and USD conversion is done in the same way as the interface for USD and Riel  conversion except different names of controls.
50 Create a JTabbedPane object tbpane.
51 Place the panelt1 to the first tab.
52 Place the panelt2 tot the second tab.
53 Set title of the first tab to "USD and Riel " text.
54 Set title of the second tab to "EURO and USD" text.
55 Add the tbpane oject to the container.
56 Make user the program window is visible.
57 The actionPerformed method is rewritten to enable the button-click action. When the user clicks btuskh from the first tab, the conversion is made from USD amount to Riel  amount or vise versa. If you want to convert from USD to Riel , input the USA amount and leave the Riel  text box blank and vise versa.
If the user clicks the bteurous button from the second tab, the conversion is for EURO to USA currency. It works the same way as the previous button click except for different currencies conversion.
 58 The KeyList class extends the KeyAdapter class to rewrite the keyTyped method. This method defines code to restrict key typed in all text boxes. Only number, dot, backspace, and delete keys are accepted.
59 The checkBlank method is implemented to check whether the combobox conversion rate or the two text boxes are not blank before conversion can be made.
60 The usdToKH method is invoked to convert from USD amount to Riel  amount.
61 The khToUSD method is invoked to convert from Riel  amount to USD amount.
62 The euroToUSD method is invoked to convert from EURO amount to USD amount.
63 The usdToEURO method is invoked to convert from USD amount to EURO amount.
65 The showMessage method is invoked to show a message dialog to inform the user when an invalid action is being made.
66 The CurrencyConversion Program has the main method to start the program.


  1. Hello Sir/ can i add this code to your QuizProgram?
    if its possible.....
    please help me...thank you so much.

  2. The blog or and best that is extremely useful to keep I can share the ideas
    of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    tanki online | 2048 game| tanki online game

  3. eToro is the #1 forex broker for beginner and professional traders.

  4. Superb article and valuable information because it’s really helpful for me, Thank you so much for share this wonderful information with us.
    Jeans Manufacturers in Delhi

  5. Such a very superb article, Very interesting to read this article, I would like to thank you for useful had made for writing this awesome article.
    Top Commercial Vehicle Painters

  6. Awesome article, Very interesting to read this article, I would like to thank you for useful had made for writing this superb article share with us.
    Web design company

  7. Great article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
    Motorcycle Tour in Mumbai

  8. Good article, Very interesting to read this article, I would like to thank you for writing this helpful article share with us.
    Lifestyle Magazine

  9. Thanks for this awesome article Very interesting to read this article, Thank you sharing with us.
    Shipping Company in India

  10. I am really happy to say this I am deeply read your article, I am searching like this type valuable information, it’s really helpful for me, I am happy to found it, thank you so much for share this blog, great work, keep sharing like this type of article, thank you so much for read my comment, if any one searching website designing company in India please visit my website
    Website Designing Company in Delhi

  11. That was such an awesome content to read and going through it.Thanks for such a good information.our product related for Servo Stabilizer Manufacturer in india and transformer.

  12. Nice Blog, keep it up for more updates about this type of blog.Carolina Classics is the manufacturer of best F-100 Classic Ford Truck Parts| Buy Ford F100 truck parts online at Carolina Classics.
    F-100 Ford Truck Parts

  13. Nice Blog, keep it up for more updates about this type of blog.
    get the best Russian Belly Dancer in Delhi

  14. Book our affordable, quick & on-time best silver service taxi Melbourne with instant booking and enjoy your seamless travel with our Silver Service Cabs Melbourne.

    silver taxi service
    silver service taxi
    silver service cabs
    silver service taxi melbourne
    silver service cab melbourne

  15. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You Wanna Party

  16. Buy Medicines Online from Ship From CA. No prescription is required for Buy Hydrocodone, Oxycodone, Percocet, Vicodin and Oxycontin Online Overnight from us.

  17. Buy hydrocodone online at first-class prices. Hydrocodone or Dihydrocodeinone is used for the treatment of moderate to severe level ache. Hydrocodone dosage is used to treat those people, who need medicine around-the-clock and for folks that do not get pain comfort from other medicinal drugs.

  18. At ShipFromCA, you can Buy oxycodone online. Oxycodone is a reasonably effective opioid ache remedy. It is generally usually taken for the remedy of mild to extreme pain. Oxycodone improves the fine of the existence of someone with many varieties of ache. The doctor prescribes oxycodone dose from 10 mg to 80 mg relying on the intensity of ache and patients’ scientific history.

  19. Vicodin is an aggregate of narcotic hydrocodone and non-narcotic ache reliever acetaminophen. This drug is helpful for relieving mild to moderately severe acne. It is a prescribed narcotic drug. Vicodin is likewise used as a cough suppressant.If overused, this drug may be addictive. Buy vicodin online
    at a low price and rapid transport from Shipfromca.Com

  20. Norco is an aggregate of acetaminophen and hydrocodone. Hydrocodone is an opioid pain medicine while Acetaminophen is a less potent pain reliever which will increase the effectiveness of hydrocodone. This drug is prescribed for slight to moderately extreme pain. The dosage of this majorly relies upon the age factor. Buy norco online from ShipFromCA at Low Prices.

  21. Buy percocet online. Percocet is a mixture of acetaminophen & oxycodone. This is an opioid/non-opioid pain reliever appropriate for both slight as nicely as extreme pain and also situations associated with pain. Buy Percocet online safely without annoying about the prescription, right here at Ship From CA.

  22. ShipFromCA allows you to reserve oc 5 – 80 mg online without RX to cast off your ache. Opioid analgesic is the brand name for oxycontin containing the active factor oxycodone. The same is also observed in Percocet and Percodan. If you are stricken by ache with cancer, backache, or arthritis, then Oxycontin could be a powerful medicine for you. Buy oxycontin online from ShipFromCA pharmacy.

  23. Here at the ShipFromCA web site, we tend to pride ourselves on giving all of our website visitors access to a spread of unengaged to use guides, articles and up to date news stories concerning the Buy adderall online.

  24. Ambien is used to deal with a sure sleep problem (insomnia) in adults. If you have hassle falling asleep, it facilitates you go to sleep faster, so that you can get a higher night’s rest. Zolpidem belongs to a category of drugs referred to as sedative-hypnotics. It acts in your brain to produce a relaxing effect. For that, you can Buy ambien online.

  25. I visit your website I am very much impressed by the blogs on your website. I recently purchased a split ac and your blog provides me great insight into AC Repair and Services .It also allows me to enrich my knowledge about various types of ac. I am a big fan of yours and find your website highly informative. Thank you for posting such unique and amazing blogs.

  26. Best Deals at Safemartshop, an international prescription service provider, which contracts with International dispensaries and USA pharmacies, is a leader in referring orders for prescription and non-prescription medications on behalf of customers throughout the world
    buy hydrocodone online
    buy oxycodone online
    buy oxycontin online
    buy percocet online
    buy vicodin online
    buy norco online
    buy adderall online
    buy xanax online