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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Quick Java question

  • 21-05-2006 3: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, Registered Users 2 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