0

How do I check to see if the JTextfield is empty to return true or false and set the focus? I have tried checking to see if the field is equal to null but that does not work. What I have done that pertains to this question can be found under the comment // THIS SECTION OF CODE and the boolean below it

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

public class DollarStore extends Applet  implements ActionListener 

{

 public JLabel      n1  = new JLabel("Enter sales person number (1-4): ");
 public JTextField  tf1 = new JTextField(5);
 public JLabel      n2  = new JLabel("Enter product number (1-5): ");
 public JTextField  tf2 = new JTextField(5);
 public JLabel      n3  = new JLabel("Enter sales amount (0.01-0.99): ");
 public JTextField  tf3 = new JTextField(5);
 public JTextArea   ta  = new JTextArea (8,58);
 public JButton     s   = new JButton ("Submit");
 public JButton     c   = new JButton ("Clear");

  public void init()
  {
  setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));

add(n1);
add(tf1);
add(n2);
add(tf2);
add(n3);
add(tf3);
ta.setFont(new Font("Courier", Font.BOLD, 10));
ta.setEditable(false);
add(ta);
add(s);
add(c);

s.addActionListener(this);
c.addActionListener(this);
}

// THIS SECTION OF CODE
private boolean emptyFields()
{
if (tf1.getText() == null)
{
 tf1.requestFocus();
 return true;
}
if (tf2.getText() == null)
{
 tf2.requestFocus();
 return true;
}
if (tf3.getText() == null)
{
 tf3.requestFocus();
 return true;
}

 return false;


 } // End emptyFields

public void actionPerformed(ActionEvent e)
{
   // Variables for TextField 
 int    spnumber = Integer.parseInt(tf1.getText());
 int    pnumber  = Integer.parseInt(tf2.getText());
 double sanumber = Double.parseDouble(tf3.getText());

if (e.getSource() == c)
{
  tf1.setText("");
  tf2.setText("");
  tf3.setText("");
  tf1.requestFocus();
}

if (e.getSource() == s)
{
emptyFields();

 {
  if (spnumber < 1 || spnumber > 4)
   showStatus("Sales person number must be in the range 1 to 4");
   if (pnumber < 1 || pnumber > 5)
   showStatus("Product number must be in the range 1 to 5");
   if (sanumber < 0.01 || sanumber > 0.99)
   showStatus("Product number must be in the range 0.01 to 0.99");
 }
 }
}  // End actionPerformed
}
Beeeee
  • 173
  • 1
  • 4
  • 12
  • 2
    `JTextField#getText#isEmpty`...You might also like to have a look at [Validating Input](http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification) which would probably be significantly easier – MadProgrammer Mar 28 '16 at 23:39
  • You could put your JTextFields in an array and iterate over it. – Drew Kennedy Mar 28 '16 at 23:42
  • Try http://stackoverflow.com/questions/17132452/java-check-if-jtextfield-is-empty-or-not? It seems like your question. – Cache Staheli Mar 28 '16 at 23:42
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Mar 29 '16 at 00:09
  • 1) `public class DollarStoreHarwoodBrent extends Applet implements ActionListener { public JLabel ..` Don't mix Swing and AWT components! The Swing equivalent is a `javax.swing.JApplet` 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Mar 29 '16 at 00:12
  • @AndrewThompson yes it is due to the teachers specs, he is too set in his ways, sorry for formatting issues, I'm just a beginner...he also wants it done by using the least amount of whitespace – Beeeee Mar 29 '16 at 00:23
  • *"yes it is due to the teachers specs,"* So, like I said, please refer them to that blog article. If they resist dropping applets from the curriculum, ask (OK challenge) them to make an applet that the students can see on the web. It will be a lot harder than they expect (especially re digitally signing the code). – Andrew Thompson Mar 29 '16 at 00:28

2 Answers2

7

JTextField#getText#isEmpty...

if (tf1.getText().trim().isEmpty()) {
    ...
}

It's been a very long time since JTextFiels#getText could return null

You might also like to have a look at Validating Input which would probably be significantly easier

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

Try this:

if (tf1.getText().equals("")) {
    //set true or false
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amber M
  • 177
  • 1
  • 6