
February 25th, 2005, 01:16 AM
|
|
Registered User
|
|
Join Date: Feb 2005
Posts: 1
Time spent in forums: 5 m 49 sec
Reputation Power: 0
|
|
|
Implementing case function in JList/ArrayList
well i basically made a currency calculator that convert dollars into different currencies, using the JList, you can choose which currency, you to convert. So i decided to make a case function in order to convert the different currencies..I started a bit on the case function, but im having problems to get it to work.... Quote: /************************************************** **************/
/* CurrencyCalculator */
/* */
/************************************************** **************/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class CurrencyCalculator extends JFrame
{
// Variables declaration
private JLabel ConversionsJLabel;
private JLabel AmountJLabel;
private JTextField AmountJTextField;
private JTextField ResultAmountJTextField;
private JList ConversionsJList;
private JScrollPane ConversionsJScrollPane;
private JPanel contentPane;
private JButton calculateJButton;
public CurrencyCalculator()
{
super();
initializeComponent();
this.setVisible(true);
}
private void initializeComponent()
{
ArrayList CurrencyList;
ConversionsJLabel = new JLabel();
AmountJLabel = new JLabel();
AmountJTextField = new JTextField();
ResultAmountJTextField = new JTextField();
ConversionsJList = new JList();
ConversionsJScrollPane = new JScrollPane();
calculateJButton = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// ConversionsJLabel
//
ConversionsJLabel.setHorizontalAlignment(SwingCons tants.CENTER);
ConversionsJLabel.setHorizontalTextPosition(SwingC onstants.CENTER);
ConversionsJLabel.setText("Convert which currency?");
//
// AmountJLabel
//
AmountJLabel.setText("Enter currency amount:");
//
// AmountJTextField
//
AmountJTextField.setHorizontalAlignment(JTextField .CENTER);
//
// ResultAmountJTextField
//
ResultAmountJTextField.setHorizontalAlignment(JTex tField.CENTER);
ResultAmountJTextField.setEditable(false);
//
// ConversionsJList
//
CurrencyList = new ArrayList();
CurrencyList.add("USD to Canadain");
CurrencyList.add("USD to Pesos");
CurrencyList.add("USD to Yen");
CurrencyList.add("USD to Euros");
ConversionsJList.setListData(CurrencyList.toArray( ));
ConversionsJList.setFocusable(false);
//
// ConversionsJScrollPane
//
ConversionsJScrollPane.setViewportView(Conversions JList);
//
// calculateJButton
//
calculateJButton.setText("CONVERT");
calculateJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
calculateJButton_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, ConversionsJLabel, 9,27,145,18);
addComponent(contentPane, AmountJLabel, 20,168,146,18);
addComponent(contentPane, AmountJTextField, 97,195,107,82);
addComponent(contentPane, ResultAmountJTextField, 374,199,119,78);
addComponent(contentPane, ConversionsJScrollPane, 33,49,179,23);
addComponent(contentPane, calculateJButton, 244,205,101,60);
//
// CurrencyCalculator setttings
//
this.setTitle(":: C u r r e n c y C a l c u l a t o r ::");
this.setLocation(new Point(35, 85));
this.setSize(new Dimension(665, 372));
}
// components w/o layout manager
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
private void calculateJButton_actionPerformed(ActionEvent e)
{
int currency = Float.parseFloat( String.indexOf( CurrecyList.getText() ) );
// Test the selected index number
switch ( currency )
{
case '0': // USD to Euros
ResultAmountJTextField.setText(Double.toString(inp ut * 1.25));
break;
case '1': // USD to Yen
ResultAmountJTextField.setText(Double.toString(inp ut * 0.25));
break;
case '2': // USD to Pesos
ResultAmountJTextField.setText(Double.toString(inp ut * 12.5));
break;
case '3': // USD to Canadian
ResultAmountJTextField.setText(Double.toString(inp ut * 0.125));
break;
default:
ResultAmountJTextField.setText("Error");
{
System.out.println("\ncalculateJButton_actionPerformed(ActionEvent e) called.");
}
}
}
} |
|