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);
        }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Aaron
  • 11,239
  • 18
  • 58
  • 73

2 Answers2

3

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

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • So if I was to frame.remove(panel_1) then frame.revalidate() then frame.repaint() it would remove the panel and all of the component contained inside it? – Aaron Jun 19 '12 at 21:23
  • I've updated the code in the question please can you tell me what I am doing wrong with the last else statement as when that code block executes the panel_1 is still visible? I've changed it to how I think it should work? as a side note newer panels added have a lower z-index then the panel added before it? – Aaron Jun 19 '12 at 21:30
  • @Aaron What you do wrong is that you do not remove the panel you previously added, but you try to remove a panel which was never added. Note how you always create a new `panel_1` and `panel_2` in your `MouseListener` (`MouseListener` which should probably be replaced by an `ActionListener` for a `JButton`) – Robin Jun 19 '12 at 21:35
  • in the first if statement I add panel_1 to the frame and try to remove it in the else statement but it still displays? So it was added in the if statement which is confusing me? – Aaron Jun 19 '12 at 21:43
  • @Aaron No, each time you enter that `MouseListener` you create new panels (first two lines in your listener). So the `panel_1` you add the first time you enter the listener is not the same as the `panel_1` when you enter the listener a second time and try to remove it – Robin Jun 19 '12 at 21:58
  • I understand now what I am doing is adding the panel above the start of the mouse listener and it creates that one instance of that panel and now I can remove it when the validation is complete! – Aaron Jun 19 '12 at 22:14
1

yourpanel.setVisible(false); should hide your panel, where "yourPanel" is your JPanel instance

Morfic
  • 15,178
  • 3
  • 51
  • 61