Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Convert Currency using AWT
Next article icon

Java Program to Convert Currency using AWT

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Swing is a part of the JFC (Java Foundation Classes). Building a Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a high level of customization and provide rich functionalities and is used to create window-based applications. Java swing components are lightweight and platform-independent and provide powerful components like tables, scroll panels, buttons, lists, colour choosers, etc.

In this article, we’ll see how to make a currency converter that includes conversions between INR and the Dollar. Two text fields are implemented with the labels Rupees and Dollar.

Note: It is assumed that 1 dollar is equal to 65.25 rupees.

Examples:

Input: INR = 130.5
Output: 2.0
Explanation:
One dollar is 65.25 rupees. So, 130.5 rupees is two dollars.

Input: Dollar = 4.5
Output: 293.625

Approach: To solve this problem, the following steps are followed:

  1. First, we need to create a frame using JFrame.
  2. Then, create two labels, two textfields, and three buttons(the first button is for rupees and the second button is for the dollar) using JLabel, JTextField and JButton.
  3. Name these components accordingly and set their bounds.
  4. Now, to perform the conversion on button click, we need to add Event Handlers. In this case, we will add an ActionListener to perform an action method known as actionPerformed in which first we need to get the values from the text fields which is default as a “string”.
  5. So, to perform mathematical operations, we need to convert them into double data type using Double.parseDouble(Object.getText()) and again convert from double to string to place the final value in the other text field using String.valueOf(object).
  6. Finally, for changing the values, we use Object.setText(object); the second object is for selecting which field we want to replace.

Below is the implementation of the above approach:

Java
// Java program to convert from  // rupee to the dollar and vice-versa  // using Java Swing   import javax.swing.*;  import java.awt.*;  import java.awt.event.*;  public class Geeks { 	// Function to convert from rupee  	// to the dollar and vice-versa  	// using Java Swing  	public static void converter()  	{   		// Creating a new frame using JFrame  		JFrame f = new JFrame("CONVERTER");   		// Creating two labels  		JLabel l1, l2;   		// Creating two text fields.  		// One for rupee and one for  		// the dollar  		JTextField t1, t2;   		// Creating three buttons  		JButton b1, b2, b3;   		// Naming the labels and setting  		// the bounds for the labels  		l1 = new JLabel("Rupees:");  		l1.setBounds(20, 40, 60, 30);  		l2 = new JLabel("Dollars:");  		l2.setBounds(170, 40, 60, 30);   		// Initializing the text fields with  		// 0 by default and setting the  		// bounds for the text fields  		t1 = new JTextField("0");  		t1.setBounds(80, 40, 50, 30);  		t2 = new JTextField("0");  		t2.setBounds(240, 40, 50, 30);   		// Creating a button for INR,  		// one button for the dollar  		// and one button to close  		// and setting the bounds  		b1 = new JButton("INR");  		b1.setBounds(50, 80, 60, 15);  		b2 = new JButton("Dollar");  		b2.setBounds(190, 80, 60, 15);  		b3 = new JButton("close");  		b3.setBounds(150, 150, 60, 30);   		// Adding action listener  		b1.addActionListener(new ActionListener() {  			public void actionPerformed(ActionEvent e)  			{  				// Converting to double  				double d  					= Double.parseDouble(t1.getText());   				// Converting rupees to dollars  				double d1 = (d / 65.25);   				// Getting the string value of the  				// calculated value  				String str1 = String.valueOf(d1);   				// Placing it in the text box  				t2.setText(str1);  			}  		});   		// Adding action listener  		b2.addActionListener(new ActionListener() {  			public void actionPerformed(ActionEvent e)  			{  				// Converting to double  				double d2  					= Double.parseDouble(t2.getText());   				// converting Dollars to rupees  				double d3 = (d2 * 65.25);   				// Getting the string value of the  				// calculated value  				String str2 = String.valueOf(d3);   				// Placing it in the text box  				t1.setText(str2);  			}  		});   		// Action listener to close the form  		b3.addActionListener(new ActionListener() {  			public void actionPerformed(ActionEvent e)  			{  				f.dispose();  			}  		});   		// Default method for closing the frame  		f.addWindowListener(new WindowAdapter() {  			public void windowClosing(WindowEvent e)  			{  				System.exit(0);  			}  		});   		// Adding the created objects  		// to the form  		f.add(l1);  		f.add(t1);  		f.add(l2);  		f.add(t2);  		f.add(b1);  		f.add(b2);  		f.add(b3);   		f.setLayout(null);  		f.setSize(400, 300);  		f.setVisible(true);  	}   	// Driver code  	public static void main(String args[])  	{  		converter();  	}  }  

Steps to Run the Program

  • Step 1: Navigate to the folder in which we want to create the program and create a new text file.
  • Step 2: Open that file (in any text editor like notepad) write the above Java code in that file.
  • Step 3: Now save this file with the .java extension for example, Geeks.java
  • Step 4: Now open the terminal and navigate to the folder in which we created the program file.
  • Step 5: Now compile the Java file using the below command:

javac Geeks.java

Step 6: Now run the compiled Java program using the below command

java Geeks

Now it will open a output window as shown in the below image and we can interact with the window.

Output:

1. The window displayed on running the program:


2. Converting from INR to the Dollar, i.e., when INR button is clicked:


3. Converting from the Dollar to INR, i.e., when the dollar button is clicked:


Next Article
Java Program to Convert Currency using AWT

V

vageeshadatta
Improve
Article Tags :
  • Java
  • Write From Home
  • java-swing
  • Java-AWT
  • java-advanced
Practice Tags :
  • Java

Similar Reads

    Convert String to Double in Java
    In this article, we will convert a String to a Double in Java. There are three methods for this conversion from String to Double, as mentioned below in the article.Methods for String-to-Double ConversionDifferent ways for converting a String to a Double are mentioned below:Using the parseDouble() me
    3 min read
    Java Program for Coin Change
    Write a Java program for a given integer array of coins[ ] of size N representing different types of denominations and an integer sum, the task is to count the number of coins required to make a given value sum. Examples: Input: sum = 4, coins[] = {1,2,3}, Output: 4Explanation: there are four soluti
    8 min read
    Simple Calculator using TCP in Java
    Prerequisite: Socket Programming in Java Networking just doesn't conclude with a one-way communication between the client and server. For example consider a time telling server which listens to request of the clients and respond with the current time to the client. Real-time applications usually fol
    5 min read
    DecimalFormat getCurrency() method in Java
    The getCurrency() method is a built-in method of the java.text.DecimalFomrat class in Java and is used to return the currency which is used while formatting currency values by this currency. It can be null if there is no valid currency to be determined or if no currency has been set previously. Synt
    2 min read
    Output of Java Programs | Set 21 (Type Conversions)
    Prerequisite - Type Conversions in Java with Examples 1) What is the output of the following program? Java public class Test { public static void main(String[] args) { int value = 554; String var = (String)value; //line 1 String temp = "123"; int data = (int)temp; //line 2 System.out.print
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences