Is Java GUI better to remove or set Visible (false)?

I am coding a relatively simple graphical Java application using widgets from the Swing library. What is the common practice of "conditionally" displaying certain elements? This is for the .setVisible(false)things we want to temporarily hide; or is it in .addas needed and then deletes them if they are no longer displayed?

+4
source share
2 answers

Usually such things depend on the opinion of the designer, however, it depends on what exactly you are trying to show / hide.

If you want to display the widget after a certain condition / method returns true, just use setVisible (true), this allows you to easily turn on / off.

If you want to display the widget only once (and no longer hide it), then just .add when you need it to be displayed (condition / method).

It all depends on preference

+3
source

"I need to display (several) error messages that should disappear if everything is correct"

, , , JLabel setText() . , , . - :

String errorMessage = "Error";
String noErrorMessage = "  ";

....
if (error) {
    errorLabel.setText(errorMessage);
} else {
    errorLabel.setText(noErrorMessage);
}

noErrorMessage ,

+5

Source: https://habr.com/ru/post/1541873/


All Articles