There is a form on which Textfield is created, two buttons and two Labels, i.e. Login form. Specifying login and password. How to make it so that if the login field is empty, it displays a message like "You did not enter a login"?

Created form

this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setBounds(400,200,500,400); this.setTitle("Вход"); Container cont1 = this.getContentPane(); cont1.setLayout(null); cont1.add(btn1); btn1.setSize(150,50); btn1.setLocation(350,310); btn1.addActionListener(new Listik()); cont1.add(btn2); btn2.setSize(150,50); btn2.setLocation(0,310); btn2.addActionListener(new Better()); cont1.add(tf1); tf1.setSize(190,25); tf1.setLocation(250,100); cont1.add(tf2); tf2.setSize(190,25); tf2.setLocation(250,159); cont1.add(lbl1); lbl1.setSize(100,30); lbl1.setLocation(100,90); cont1.add(lbl2); lbl2.setSize(100,30); lbl2.setLocation(100,150); cont1.add(rbtn1); rbtn1.setSize(200,30); rbtn1.setLocation(222,200); 

For btn1 I have already made an event to call an empty form, but if there is an empty field or incorrectly entered data so that this form does not open and an error message is displayed

  • your code in the studio - ArchDemon
  • Already fixed))) - Slava
  • Yes, this code is not needed at all. A code where you can't do something - ArchDemon

1 answer 1

Hang something on a button press event.

 if (null == textLogin.getText() || textLogin.getText().trim().isEmpty()) { JOptionPane.showMessageDialog(this, "текст ошибки", "Error", JOptionPane.ERROR_MESSAGE); } else { //Вызов вышей формы } 

where textLogin is your JTextEdit component containing a login

  • Thank you very much) Works)) - Slava