Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 25th, 2005, 01:16 AM
C1Ph3r C1Ph3r is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 C1Ph3r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.");


}



}

}

}

Reply With Quote
  #2  
Old March 2nd, 2005, 02:26 PM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m
Reputation Power: 5
What is your problem with the code ?
It looks fine to me, allthough I would not write
Code:
case '1' :

but just plain
Code:
case 1 :

But let us know what your problem is.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Implementing case function in JList/ArrayList


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT