May someone give me an idea of how can I scan integer in the text field and compare it to use in an if else statement.
Thank you for the time.

Recommended Answers

All 3 Replies

Member Avatar for harsh2327
TextField abc = new TextField();
String text = abc.getText();

try {
    int num = Integer.parseInt(text);
}
catch(NumberFormatException e) {
}

I will show you my code for you to get it easier but first thank you for your quick reply.

------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class java extends JPanel implements ActionListener {
  Button button1;
   String bOk = "Ok";
   String bClose = "Close";
   JLabel bInquiry;
   JLabel tOption;
   JLabel mDeposit;
   JLabel mWithdraw;
   JTextField textField;
   
    public java() {
       tOption = new JLabel("Transaction Options:");
       bInquiry = new JLabel("(1) Balance Inquiry");
       mDeposit = new JLabel("(2) Deposit");
       mWithdraw = new JLabel("(3) Withdraw");
       textField = new JTextField(20);
     
       add(tOption);
       add(bInquiry);
       add(mDeposit);
       add(mWithdraw);
       add(textField); 
      
      
       JComponent buttonPane = createButtonPanel();
    }   
	JComponent createButtonPanel() {
        JPanel panel = new JPanel(new GridLayout(0,1));
        JButton okButton = new JButton("Ok");
        JButton closeButton = new JButton("Close");

        okButton.setActionCommand(bOk);
        closeButton.setActionCommand(bClose);
        okButton.addActionListener(this);
        closeButton.addActionListener(this);

       
        add(okButton);
        add(closeButton);
	return closeButton;

   
          }
  public void actionPerformed(ActionEvent event) {
	  String cmd = event.getActionCommand();

      if (bOk.equals(cmd)) { 
    	 ??????????????????????????
                    
      } else {
          JOptionPane.showMessageDialog(textField,
             "The transaction will now close. Thank You!");
          System.exit(1);
      }
  }


  private static void createAndShowGUI() {
      JFrame frame = new JFrame("WELCOME TO INPROLA BANK INC.");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new java());
      frame.pack();
      frame.setVisible(true);
              } ;
 
public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
           
        	  createAndShowGUI();
          }
      });
  }
}

-----------------------------------------------------------------------------------------
The line with the question marks is the line I lack.

Member Avatar for harsh2327

First of all, line no. 6 => Button button1; is never used

To answer your query, modify the code as follows

public void actionPerformed(ActionEvent event) {
	JButton cmd = (JButton)event.getActionCommand();  // Typecast the event to convert it to Button object

	if (okButton.equals(cmd)) {
		// Your code here

	} else {
		.
		.
		.
		.
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.