Currency Converter Using Java Swing With Source Code
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 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
Complain Management using Python with a Graphical User Interface (GUI) Introduction: The Complain Management using Python program designed to manage complaints effectively …
COVID 19 Hospital Management Using Python [Django Framework] Introduction: The COVID-19 Hospital Management is a Python-based application that tracks web applications for Hospitals. …
Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …
Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …
KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …
Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …