Currency Converter Using Java Swing

currency exchange

Introduction:

In this article, we will demonstrate how to develop Currency Converter using Java Swing library. This Currency Converter allows you to convert the currency using a graphical user interface (GUI) application. This application is build using the Java Swing library which gives a flexible set of components that can be use to build a Graphical user interface(GUI). This application has an easy and simple design with button to convert the entered currency amount in targeted currency. So take a seat, pour a cup of coffee and start using currency converter.

Explanation:

This Currency Converter is a Graphical user interface(GUI) system that allows user to easily convert the entered currency amount in the targeted currency using button. Users may very quickly convert the currency using the system’s simple design. The Currency Converter has simple design with TextField, combo box and button. The user can enter the currency in Text field then select the targeted currency and can convert using convert button.

The users may convert the currency using ‘convert’ button. The user can enter the currency in Text field then select the targeted currency and can convert using convert button.

The Java Swing library which gives a flexible and powerful set of components is used to develop the currency Converter. The system makes use of Swing components such as JLabel, JTextField, JComboBox and JButton among many other Swing components. Button event is handled by the ActionListener interface and button events are defined using ActionListener’s actionPerformed() method.

Methods used in the project:

actionPerformed():

The action or events of the buttons are handled using this method. Buttons in the system to convert the currency is handled using this method. This method describe how the components should behave after the button is click.

Source Code:

				
					import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

public class Currency_Converter extends JFrame {
    private JLabel amountLabel, fromLabel, toLabel, resultLabel;
    private JTextField amountField;
    private JComboBox<String> fromComboBox, toComboBox;
    private JButton convertButton;
    private DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");

    private final String[] currencies = {"USD", "EUR", "JPY", "GBP", "CAD", "AUD", "CHF", "CNY","INR"};
    private double[] exchangeRates = {1.00, 0.84, 109.65, 0.72, 1.27, 1.30, 0.92, 6.47,87.14};

    public Currency_Converter() {
        setTitle("Currency Converter");
        setLayout(new GridLayout(4, 2));

        amountLabel = new JLabel("Amount:");
        add(amountLabel);

        amountField = new JTextField();
        add(amountField);

        fromLabel = new JLabel("From:");
        add(fromLabel);

        fromComboBox = new JComboBox<>(currencies);
        add(fromComboBox);

        toLabel = new JLabel("To:");
        add(toLabel);

        toComboBox = new JComboBox<>(currencies);
        add(toComboBox);

        convertButton = new JButton("Convert");
        add(convertButton);

        resultLabel = new JLabel();
        add(resultLabel);

        convertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double amount = Double.parseDouble(amountField.getText());
                    String fromCurrency = (String) fromComboBox.getSelectedItem();
                    String toCurrency = (String) toComboBox.getSelectedItem();
                    double exchangeRate = exchangeRates[getIndex(toCurrency)] / exchangeRates[getIndex(fromCurrency)];
                    double result = amount * exchangeRate;
                    resultLabel.setText(decimalFormat.format(result) + " " + toCurrency);
                } catch (Exception ex) {
                    resultLabel.setText("Invalid input");
                }
            }
        });

        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private int getIndex(String currency) {
        for (int i = 0; i < currencies.length; i++) {
            if (currency.equals(currencies[i])) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        new Currency_Converter();
    }
}
				
			

Output:

Find More Projects

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes