简体   繁体   中英

How to add ActionEvent to Two JTextFields

Question is: How do i add code to ensure that a table number (tableNumberJTextField) and waiter name (waiterNameJTextField) have been entered? If one or all fields are empty Use a JOptionPane to inform user that both fields must contain Information

At the moment i have:

private void calculateBillJButtonActionPerformed( 
      ActionEvent event )

{

    if(event.getSource() == tableNumberJTextField)   
     {

     }
     else
     {
         JOptionPane.showInputDialog("Information missing");
     }

   } // end method calculateBillJButtonActionPerformed

Would i need another IF...Else and a JOptionPane in there for WaiterName as well as tableNumberJTextField seen in the code above OR am i completely doing this wrong????

FULL QUESTION THAT I HAVE TO DO IS: calculateBillJButtonActionPerformed method (which immediately follows dessertJComboBoxItemStateChanged) and add code to ensure that a table number (tableNumberJTextField) and waiter name (waiterNameJTextField) have been entered. If one of these fields is empty, display a JOptionPane informing the user that both fields must contain information. Otherwise, call the calculateSubtotal method, which you implement in the next step, to calculate the subtotal of the bill. The calculateSubtotal method takes no arguments and returns a double containing the subtotal, which you should display in subtotalJTextField. Calculate and display the tax and the total of the bill in JTextFields taxJTextField and totalJTextField, respectively. The tax rate is specified in a constant TAX_RATE.

Presumably, the actionPerformed you're showing is for a button. If so, just check for text in the fields:

   String tableNumber = tableNumberJTextField.getText().trim();
   String waiterName = waiterNameJTextField.getText().trim();

   if (tableNumber.length() == 0 || waiterName.length() == 0)
    {
       // show an error
    }
    else
    {
       // do a calculation
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM