Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 25th, 2005, 1:27 AM   #1
c1ph3r
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 c1ph3r is on a distinguished road
HELP!!! Implementing Case function into 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....any help?


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(SwingConstants.CENTER);
ConversionsJLabel.setHorizontalTextPosition(SwingConstants.CENTER);
ConversionsJLabel.setText("Convert which currency?");
//
// AmountJLabel
//
AmountJLabel.setText("Enter currency amount:");
//
// AmountJTextField
//
AmountJTextField.setHorizontalAlignment(JTextField.CENTER);
//
// ResultAmountJTextField
//
ResultAmountJTextField.setHorizontalAlignment(JTextField.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(ConversionsJList);
//
// 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(input * 1.25));
break;

case '1': // USD to Yen
ResultAmountJTextField.setText(Double.toString(input * 0.25));
break;

case '2': // USD to Pesos
ResultAmountJTextField.setText(Double.toString(input * 12.5));
break;

case '3': // USD to Canadian
ResultAmountJTextField.setText(Double.toString(input * 0.125));
break;

default:
ResultAmountJTextField.setText("Error");

{


System.out.println("\ncalculateJButton_actionPerformed(ActionEvent e) called.");


}



}

}

}
c1ph3r is offline   Reply With Quote
Old Feb 25th, 2005, 6:48 PM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
first, currency is defined as int. instead of:
Float.parseFloat( String.indexOf( CurrecyList.getText() ) );
use Integer.parseInt( String.indexOf( CurrecyList.getText() ) );

then, you only put characters in single quotes, not integers. delete the ' ' so it looks more like:
case 0:
case 1:
etc.
ZenMasterJG is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC