Question
Can someone help me with JAVA? The question is highlighted with red highlight Here's my code import java.awt.event.*; /** @SuppressWarnings(serial) public class MainPanel extends JPanel
Can someone help me with JAVA? The question is highlighted with red highlight
Here's my code
import java.awt.event.*;
/** @SuppressWarnings("serial") public class MainPanel extends JPanel {
private final static String[] list = { "inches/cm" }; private JTextField textField; private JLabel label; private JComboBox combo;
MainPanel() {
ActionListener listener = new ConvertListener();
combo = new JComboBox(list); 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; }
double result = factor * value + offset;
label.setText(Double.toString(result)); } } }
}
----------
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); } }
Show transcribed image text
Expert Answer
- Anonymous answered this
Was this answer helpful?
1
0
102 answers
I have modified MainPanel class to load combo box with currency data from file. Inline comments are added wherever I have made changes. Screenshot of sample output is also attached for your reference. Let me know if further clarification/modification of program is required.
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; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started