0

As a part of an assignment I'm trying to retrieve a value from a JTextField. What I'm trying to get is the interest rate for an account. It should be greater than zero & user should be asked that until he/she gives something greater than zero. Also if the input is something other than a number, like a string of text, there should be an error as well and it should ask the user again.

I've done some of the part but I'm having issues completing the task.

The following is the method that calls the GUI:

//getting the interest rate
protected void getInterest() {

    if( this instanceof flexibleAccount ) {

    }

    else {

        GetInterest getInterest = new GetInterest();
        getInterest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        getInterest.setSize( 350, 100 );
        getInterest.setVisible(true);
    }//end else

}//end getInterest

It goes to here:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.util.InputMismatchException;


public class GetInterest extends JFrame {

private JTextField textField;
private double interestRate;

public GetInterest() {

    super("Banking Application");
    setLayout( new FlowLayout() );

    textField = new JTextField("Enter interest rate",10);
    add(textField);

    TextFieldHandler handler = new TextFieldHandler();
    textField.addActionListener( handler );

}//end GetInterest

private class TextFieldHandler implements ActionListener {

    @Override
    public void actionPerformed( ActionEvent event ) {

        try {

            String string;

            string = textField.getText();

            //string = String.format("%s", event.getActionCommand());

            interestRate = Double.parseDouble(string);

            if( interestRate <= 0 )
                JOptionPane.showMessageDialog( null, "Interest rate should be greater than zero" );
            else if( interestRate > 0 ) {
                JOptionPane.showMessageDialog( null, interestRate );
            }
            //else
                //throw new InputMismatchException;

        }

        catch(InputMismatchException  exception) {

            System.out.println(accountTest.getStackTrace(exception));

        }




    }//end actionPerformed



}//end TextFieldHandler

}//end GetInterest

As you can see I'm getting the value and user is asked again if he/she enters something smaller than or equal to zero.

But the following are missing:

  • returning it to the getInterest method,
  • window disappearing after completion,
  • displaying an error if the user enters a text.

How can I achieve those.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Can
  • 4,516
  • 6
  • 28
  • 50
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) In fact, the symptoms of this problem suggest a modal dialog is just what is needed. – Andrew Thompson Apr 08 '12 at 18:10

2 Answers2

0

displaying an error if the user enters a text. k value will be JTextField text

I have done that problem with try catch

public static void main(String[] args) {
    String k = "a";

    try{
    System.out.println(Integer.parseInt(k));
    }catch(NumberFormatException ex){
        JOptionPane.showConfirmDialog(this, "Is not required data");
    }

window disappearing after completion

After int value = JOptionPane.showMessageDialog( null, interestRate ); method execution it returns an integer. For example if your dialog have ok, no, cancel, you will get as ok value = 0; ok value = 1; cancel value =2; And you will check , if value==2 then close frame;

1st one I have not understood

Ant4res
  • 1,217
  • 1
  • 18
  • 36
Rufatet
  • 51
  • 1
  • 5
0

Why dont you simply silent the alphabet and symbol keys on your keyboard??

try this code, its more classy and nice just register a keyTyped listener on your JTextField

private void jTextField1KeyTyped(KeyEvent evt)
{
    int x = evt.getKeyChar();
    if (x != KeyEvent.VK_BACK_SPACE && x !=KeyEvent.VK_PERIOD) {

        if(x ==KeyEvent.VK_PERIOD)
        {
            if(jTextField7.getText().contains("."))
            {
                evt.consume();
            }
        }

       if (x < 48 || x > 57) {
            evt.consume();
        }
    }

}

this takes care of that user should only enter numbers and it should only contain 1 decimal point

next you will be having submit button that user will be clicking after entering the interest rate inside that

check

    if(jTextField7.getText().trim().isEmpty())
    {
        JOptionPane.showMessageDialog(this,"Cannot be left empty","Error",JOptionPane.ERROR_MESSAGE);
        return;
    }
   else
   {
      Double db= new Double(jTextField7.getText());
      {
           if(db==0.0)
           {
              JOptionPane.showMessageDialog(this,"Cannot be Zero","Error",JOptionPane.ERROR_MESSAGE);
              return;
           }
    }

   getInterest(jTextField1.getText());

Cheers!!

Priyanshu Jha
  • 575
  • 5
  • 11