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 8094807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:55:03+00:00 2026-06-05T20:55:03+00:00

Some context for this code, what I am trying to do is create a

  • 0

Some context for this code,
what I am trying to do is create a validation warning if the input containts “” as a value,
so if an error is displayed it displays the message.
If it is valid it does not display a message,
so how do I remove the message when the textField is valid?

The message is a JPanel that contains a JLabel with the text in it,
I add this this JPanel to the frame when it is not valid,
and I am trying to remove it when it is valid.

So what am I doing wrong here?
I am at a basic level with Swing.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Test {

        private JFrame frame;
        private JTextField textField;
        private JTextField textField_1;

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

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

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
                frame = new JFrame();
                frame.setBounds(100, 100, 401, 232);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(null);

                JPanel panel = new JPanel();
                panel.setBounds(10, 11, 330, 94);
                frame.getContentPane().add(panel);
                panel.setLayout(null);

                JLabel lblNewLabel = new JLabel("Firstname :");
                lblNewLabel.setBounds(10, 11, 104, 14);
                panel.add(lblNewLabel);

                textField = new JTextField();
                textField.setBounds(76, 8, 244, 20);
                panel.add(textField);
                textField.setColumns(10);

                JLabel lblLastname = new JLabel("Lastname :");
                lblLastname.setBounds(10, 42, 78, 14);
                panel.add(lblLastname);

                textField_1 = new JTextField();
                textField_1.setBounds(76, 39, 244, 20);
                panel.add(textField_1);
                textField_1.setColumns(10);

                JButton btnValidate = new JButton("Validate");
                btnValidate.addMouseListener(new MouseAdapter() {
                        @SuppressWarnings("deprecation")
                        @Override
                        public void mousePressed(MouseEvent arg0) {
                                JPanel panel_1 = new JPanel();
                                JPanel panel_2 = new JPanel();

                                if(textField.getText().equals("")) {

                                        panel_1.setBackground(new Color(30, 144, 255));
                                        panel_1.setBounds(100, 116, 330, 26);

                                        JLabel lblMessage = new JLabel("0 :");
                                        lblMessage.setForeground(new Color(255, 255, 255));
                                        lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));

                                        panel_1.add(lblMessage);

                                        frame.getContentPane().add(panel_1);

                                        frame.revalidate();
                                        frame.repaint(10);
                                        frame.revalidate();
                                }
                                else if(textField_1.getText().equals("")) {

                                        panel_2.setBackground(new Color(50, 200, 255));
                                        panel_2.setBounds(10, 134, 330, 26);

                                        JLabel lblMessage = new JLabel("1 :");
                                        lblMessage.setBounds(50, 50, 50, 50);
                                        lblMessage.setAlignmentX(50);
                                        lblMessage.setForeground(new Color(255, 255, 255));
                                        lblMessage.setFont(new Font("Tahoma", Font.BOLD, 13));

                                        panel_2.add(lblMessage);

                                        frame.getContentPane().add(panel_2);

                                        frame.remove(panel_1);

                                        frame.revalidate();
                                        frame.repaint(10);
                                        frame.revalidate();
                                }
                        }
                });

                btnValidate.setBounds(231, 71, 89, 23);
                panel.add(btnValidate);
        }
}
  • 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-05T20:55:05+00:00Added an answer on June 5, 2026 at 8:55 pm

    The easiest way is to simply adjust the visibility (JComponent#setVisible( false ) ).

    If you really want to remove the component completely, you have to remove and revalidate, as documented in the Container#remove method

    This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

    which results in code like

    panel.remove( componentToRemove );
    panel.revalidate();
    panel.repaint();
    

    As a side note: please replace the null layout and those setBounds call by a proper LayoutManager. You might want to take a look excellent ‘Nested layout example’ available on SO to see what is possible with layout managers. The Swing tag info on SO contains some extra useful links when starting to work with layout managers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context I came across some code, like this: if( Some_Condition ) throw 0; I
Context I'm trying to have some "plugins" (I'm not sure this is the correct
I'm trying to create a Rails app template I have this block of code
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have a flex app which allows user to create some content. this content
I'm trying to encrypt some content with an RSA private key. I'm following this
I am trying to create some dynamic DDL to build a function and when
First of all, this is Java 1.4 (project restrictions). I'm trying to create a
I am trying to create a custom pin code widget for android as an
I have a strange error with my code. I am trying to show to

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.