Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA HELP NEEDED! Can someone help me with the following program? I need to add the following changes to my code A Reset button should

JAVA HELP NEEDED!

Can someone help me with the following program? I need to add the following changes to my code

  • A Reset button should clear the input and result values shown.

  • A reverse conversion checkbox to make the conversion to work in reverse

Here's my code

PROGRAM

Converter.java

import javax.swing.JFrame; /** * The main driver program for the GUI based conversion program. * * @author mdixon */ public class Converter { public static void main(String[] args) { JFrame frame = new JFrame("Converter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainPanel panel = new MainPanel(); // frame.setJMenuBar(panel.setupMenu()); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }

MainPanel.java

import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Vector; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; @SuppressWarnings("serial") public class MainPanel extends JPanel { // private final static String[] list = { "inches/cm" }; private JTextField textField; private JLabel label; // Declaring combo of type currency private JComboBox combo; /* * Method to load currency data from file */ private void initCombo(String file) { // creating vector to provide as data of combobox Vector model = new Vector<>(); try { // Reading data from file BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); String line; // iterating through the file line by line while ((line = br.readLine()) != null) { // splitting line using comma String arr[] = line.split(","); // creating instance of currency object Currency currency = new Currency(); // setting values to currency object currency.setName(arr[0].trim()); currency.setValue(Double.parseDouble(arr[1].trim())); currency.setSymbol(arr[2].trim()); // adding currency object to model model.add(currency); } // closing reader br.close(); } catch (IOException e) { e.printStackTrace(); } // initializing combo box with elements in the Vector. combo = new JComboBox(model); } MainPanel() { ActionListener listener = new ConvertListener(); initCombo("currency.txt"); // calling method to create combo box combo.addActionListener(listener); // convert values when option changed JLabel inputLabel = new JLabel("Enter value:"); JButton convertButton = new JButton("Convert"); convertButton.addActionListener(listener); // convert values when pressed label = new JLabel("---"); textField = new JTextField(5); add(combo); add(inputLabel); add(textField); add(convertButton); add(label); setPreferredSize(new Dimension(800, 80)); setBackground(Color.LIGHT_GRAY); } private class ConvertListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { String text = textField.getText().trim(); if (text.isEmpty() == false) { double value = Double.parseDouble(text); // the factor applied during the conversion double factor = 0; // the offset applied during the conversion. double offset = 0; // Setup the correct factor/offset values depending on required conversion // switch (combo.getSelectedIndex()) { // // case 0: // inches/cm // factor = 2.54; // break; // } // // Setting factor using the value of selected item factor = ((Currency) combo.getSelectedItem()).getValue(); double result = factor * value + offset; // Setting symbol using the symbol of selected item label.setText(((Currency) combo.getSelectedItem()).getSymbol() + " " + Double.toString(result)); } } } /** * Class to keep currency data read from the file * */ private class Currency { private String name; // name of currency private Double value; // value of currency private String symbol; // symbol of currency // Getters and Setters of the class public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getValue() { return value; } public void setValue(Double value) { this.value = value; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } /* * Overriding toString, the value returned from this method will be shown inside * combobox */ @Override public String toString() { return name; } } }

Currency file currency.txt

Euro (EUR), 1.06, US Dollars (USD), 1.14, $ Australian Dollars (AUD), 1.52, $ Canadian Dollars (CAD), 1.77, $ Icelandic krna (ISK), 130.62, kr United Arab Emirates Dirham (AED), 4.60, . South African Rand (ZAR), 17.96, R Thai Baht (THB), 44.28,

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

4. Explain the strengths and weaknesses of each approach.

Answered: 1 week ago

Question

3. Identify the methods used within each of the three approaches.

Answered: 1 week ago