Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:40:59+00:00 2026-06-05T16:40:59+00:00

What I am trying to do is this, when I enter the details it

  • 0

What I am trying to do is this,
when I enter the details it will validate if the textFiled is empty when a button is pressed, if it is empty it will display a message saying that.
Then it will move to the next textFile similar to many web based registration forms,
what I am trying to find out is why wont the message change?

Pasting this code into an ecilpse file and running it should display the simple frame and what I am trying to do.

The message displays on the bottom of the frame when the firstname field is empty,
can anyone explain why it doesn’t show the next message when the firstname field containes text and the middlename contains no text?

Most of the logic is at the bottom of the code.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;

public class start {

private JFrame frame;
private JTextField tfFirstname;
private JTextField tfMiddlenames;
private JTextField tfSurname;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                start window = new start();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public start() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();

    frame.setBounds(100, 100, 505, 429);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    frame.getContentPane().setLayout(null);

    final JPanel panelClientNew = new JPanel();
    panelClientNew.setBackground(new Color(0, 102, 255));
    panelClientNew.setBounds(10, 11, 469, 299);
    frame.getContentPane().add(panelClientNew);
    panelClientNew.setLayout(null);

    JLabel lblFirstname = new JLabel("Firstname :");
    lblFirstname.setHorizontalAlignment(SwingConstants.RIGHT);
    lblFirstname.setVerticalAlignment(SwingConstants.BOTTOM);
    lblFirstname.setForeground(new Color(255, 255, 255));
    lblFirstname.setFont(new Font("Tahoma", Font.BOLD, 13));
    lblFirstname.setBounds(10, 16, 163, 14);
    panelClientNew.add(lblFirstname);

    tfFirstname = new JTextField();
    tfFirstname.setFont(new Font("Tahoma", Font.PLAIN, 13));
    tfFirstname.setBounds(177, 10, 282, 27);
    panelClientNew.add(tfFirstname);
    tfFirstname.setColumns(10);

    JLabel lblMiddlenames = new JLabel("Middlenames :");
    lblMiddlenames.setHorizontalAlignment(SwingConstants.RIGHT);
    lblMiddlenames.setForeground(new Color(255, 255, 255));
    lblMiddlenames.setFont(new Font("Tahoma", Font.BOLD, 13));
    lblMiddlenames.setBounds(10, 47, 163, 14);
    panelClientNew.add(lblMiddlenames);

    tfMiddlenames = new JTextField();
    tfMiddlenames.setFont(new Font("Tahoma", Font.PLAIN, 13));
    tfMiddlenames.setBounds(177, 41, 282, 27);
    panelClientNew.add(tfMiddlenames);
    tfMiddlenames.setColumns(10);

    JLabel lblSurname = new JLabel("Surname :");
    lblSurname.setHorizontalAlignment(SwingConstants.RIGHT);
    lblSurname.setForeground(new Color(255, 255, 255));
    lblSurname.setFont(new Font("Tahoma", Font.BOLD, 13));
    lblSurname.setBounds(10, 78, 163, 14);
    panelClientNew.add(lblSurname);

    tfSurname = new JTextField();
    tfSurname.setFont(new Font("Tahoma", Font.PLAIN, 13));
    tfSurname.setBounds(177, 72, 282, 27);
    panelClientNew.add(tfSurname);
    tfSurname.setColumns(10);



    JButton btnAdd = new JButton("Add");
    btnAdd.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent arg0) {
            /*
             * 
             * 
             *
             *I am trying to create a message that validates on certain circumstances
             * 
             * 
             * 
             */

            if(tfFirstname.getText().equals(null) || tfFirstname.getText().equals("") || tfFirstname.getText().equals(false)) {
                JPanel panelMessage = new JPanel();
                panelMessage.setBackground(new Color(30, 144, 255));
                panelMessage.setBounds(10, 321, 469, 29);
                frame.getContentPane().add(panelMessage);

                JLabel lblPersonSaved = new JLabel("Please Enter Firstname :");
                lblPersonSaved.setForeground(new Color(255, 255, 255));
                lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15));
                panelMessage.add(lblPersonSaved);
                frame.revalidate();
                panelMessage.revalidate();
                frame.repaint();

            }
            else if (tfMiddlenames.getText().equals(null) || tfMiddlenames.getText().equals("") || tfMiddlenames.getText().equals(false)) {
                JPanel panelMessage = new JPanel();
                panelMessage.setBackground(new Color(30, 144, 255));
                panelMessage.setBounds(10, 321, 469, 29);
                frame.getContentPane().add(panelMessage);

                JLabel lblPersonSaved = new JLabel("Please Enter Middlenames :");
                lblPersonSaved.setForeground(new Color(255, 255, 255));
                lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15));
                panelMessage.add(lblPersonSaved);

                frame.revalidate();
                panelMessage.revalidate();
                frame.repaint();

            }
            else if (tfSurname.getText().equals(null) || tfSurname.getText().equals("") || tfSurname.getText().equals(false)) {
                JPanel panelMessage = new JPanel();
                panelMessage.setBackground(new Color(30, 144, 255));
                panelMessage.setBounds(10, 321, 469, 29);
                frame.getContentPane().add(panelMessage);

                JLabel lblPersonSaved = new JLabel("Please Enter Surname :");
                lblPersonSaved.setForeground(new Color(255, 255, 255));
                lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15));
                panelMessage.add(lblPersonSaved);
                frame.revalidate();
                panelMessage.revalidate();
                frame.repaint();

            }
            else {


                //Validation has passed

            }
        }
    });
    btnAdd.setBounds(370, 265, 89, 23);
    panelClientNew.add(btnAdd);





}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:41:02+00:00Added an answer on June 5, 2026 at 4:41 pm

    I recommend that you use an InputVerifier as this will verify that the contents of the JTextField are correct (any way that you wish to define this) before allowing you to even leave the JTextField. Now it won’t stop you from pressing other JButtons and whatnot, so you’ll need to take other precautions if you have a submit button. An example of a simple InputVerifier that checks to see if the JTextField is empty is shown below:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class InputVerifierExample extends JPanel {
       public static final Color WARNING_COLOR = Color.red;
       private JTextField firstNameField = new JTextField(10);
       private JTextField middleNameField = new JTextField(10);
       private JTextField lastNameField = new JTextField(10);
       private JTextField[] nameFields = { 
             firstNameField, 
             middleNameField,
             lastNameField };
       private JLabel warningLabel = new JLabel("  ");
    
       public InputVerifierExample() {
          warningLabel.setOpaque(true);
    
          JPanel namePanel = new JPanel();
          namePanel.add(new JLabel("Name:"));
          MyInputVerifier verifier = new MyInputVerifier();
          for (JTextField field : nameFields) {
             field.setInputVerifier(verifier);
             namePanel.add(field);
          }
          namePanel.add(new JButton(new SubmitBtnAction()));
    
          setLayout(new BorderLayout());
          add(namePanel, BorderLayout.CENTER);
          add(warningLabel, BorderLayout.SOUTH);
       }
    
       private class SubmitBtnAction extends AbstractAction {
          public SubmitBtnAction() {
             super("Submit");
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             // first check all fields aren't empty
             for (JTextField field : nameFields) {
                if (field.getText().trim().isEmpty()) {
                   return;  // return if empty
                }
             }
             String name = "";
             for (JTextField field : nameFields) {
                name += field.getText() + " ";
                field.setText("");
             }
             name = name.trim();
             JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
                   JOptionPane.INFORMATION_MESSAGE);
          }
       }
    
       private class MyInputVerifier extends InputVerifier {
    
          @Override
          public boolean verify(JComponent input) {
             JTextField field = (JTextField) input;
             if (field.getText().trim().isEmpty()) {
                warningLabel.setText("Please do not leave this field empty");
                warningLabel.setBackground(WARNING_COLOR);
                return false;
             }
             warningLabel.setText("");
             warningLabel.setBackground(null);
             return true;
          }
    
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("InputVerifier Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new InputVerifierExample());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a user to enter their details and then store
I'm trying to enter edit mode on a specific cell like this: void MainWindow::on_addButton_released()
I'm trying this: {:id => 5, :foos => [1,2,3]}.each {|k,v| v.to_s} But that's returning
I've been trying this for hours, so maybe a fresh set of eyes will
I am currently working on this application(C#,WPF,LINQ2XML,.NET 4.0) that displays details for 'Animals'. An
I'm trying to create a simple login system that will have the forms and
This if statement is not working. I am trying to make it so that
Im trying this, but when i press it it dont changes, it only displays
I'm trying this pattern: (r'^jobs/(?P<job_id>\d+)/$', job_handler) To work with jobs/ and jobs/{job_id}, but the
I'm trying this for almost two hours now, without any luck. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.