Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick Java question

Options
  • 21-05-2006 4:14pm
    #1
    Closed Accounts Posts: 25


    Lads

    This is the code for a VERY simple currency converter in Swing. I am after a fast and dirty way to limit the dollarAmount field to 2 decimal places. Is there any way of specifying it in the constructor?
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    
    public class Converter extends JPanel{
    	JButton convert;
    	JTextField euro;
    	JTextField dollar;
    	JTextField rate;
    	JLabel euroLabel;
    	JLabel dollarLabel;
    	JLabel rateLabel;
    	
    	public Converter (){
    		super();
    		euroLabel = new JLabel ("Euro (€): ");
    		dollarLabel = new JLabel ("Dollar ($): ");
    		rateLabel = new JLabel ("Rate: ");
    		
    		euro = new JTextField ("0.00", 8);
    		dollar = new JTextField ("0.00", 8);
    		dollar.setEditable (false);
    		rate = new JTextField ("1.27", 4);
    		
    		convert = new JButton ("CONVERT!");
    		
    		convert.addActionListener (new ActionListener (){
    			public void actionPerformed (ActionEvent ae) {
    				double euroAmount = Double.parseDouble(euro.getText());
    				double r = Double.parseDouble(rate.getText());
    				double dollarAmount = euroAmount*r;
    				dollar.setText(""+ dollarAmount);
    			}
    		});
    		add(euroLabel);
    		add(euro);
    		add(rateLabel);
    		add(rate);
    		add(convert);
    		add(dollarLabel);
    		add(dollar);
    	}
    	public static void main (String args[]){
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new Converter());
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    
    

    Many thanks in advance...


Comments

  • Closed Accounts Posts: 13 smythmark


    Something like this should do it

    DecimalFormat df = new DecimalFormat("#,###.00");
    double d = 1234.567;
    System.out.println(df.format(d));

    so for example 1234.678 becomes 1,234.57.


  • Registered Users Posts: 58 ✭✭Barry.Commins


    You could use a formatted text field and a decimal formatter.

    You should also use dollar.setValue() instead of dollar.setText() in your ActionListener() if you want to limit the number of digits.

                    //......
                    
                    DecimalFormat df = new DecimalFormat("#,###.##");
                    df.setMinimumFractionDigits(2);
                    df.setMaximumFractionDigits(2);
                    
    		dollar = new JFormattedTextField (df);
    
    

    you will also need to set the number of columns and the the inital value for your formatted text field, but it's pretty straight-forward.

    There are probably easier ways to do it, but this way should work fine.


Advertisement