5

I am coding up a relatively simple GUI application in Java using widgets from the Swing library. What is the common practice for 'conditionally' displaying certain items? Is it to .setVisible(false) on things that we want to temporarily hide; or is it to .add items as they are needed and then removing them when they are not to be shown any more?

ttttcrngyblflpp
  • 143
  • 1
  • 9
  • 3
    I'd tend to use a `CardLayout` (or in other words, neither of the above).. – Andrew Thompson May 24 '14 at 11:49
  • 1
    for me, I would change visibility – Stultuske May 24 '14 at 11:50
  • *"..for 'conditionally' displaying certain items"* And don't forget you can conditionally *enable* certain items. What is the actual use-case here? – Andrew Thompson May 24 '14 at 11:53
  • @AndrewThompson time to investigate `CardLayout` it looks like. Right now I'm using `BoxLayout` as all I really need is for everything to stack on top of each other. – ttttcrngyblflpp May 24 '14 at 11:53
  • 1
    It's primary the designer opinion. And it particularly depends on the user requirements. – Roman C May 24 '14 at 11:53
  • @AndrewThompson The application is a journey-finder for a simplified bus system. So there are text boxes to input a startStation, an endStation and a time, and I'm outputting the fastest journey (as just a string). I need to display (multiple) error messages that must go away when things are correct obviously; and there is a `JPanel` of stuff for editting a journey only after a journey has been computed. – ttttcrngyblflpp May 24 '14 at 11:58
  • Once I bookmarked this wonderful answer by @DavidKroukamp , of whose link is [drawing warning symbols during form validation in Swing](http://stackoverflow.com/a/14170292/1057230). No doubt, I did liked the contents in the answer, and hopefully it might add somethingy to your knowledge as well :-) – nIcE cOw Sep 02 '14 at 17:15

2 Answers2

5

" I need to display (multiple) error messages that must go away when things are correct obviously"

Not sure how you're displaying your error messages, but seems like a simple JLabel with the simple use of setText() would be appropriate. Trying to add an remove or set visible will mess with the layout, causing a constantly changing layout, which may be undesirable or not very user friendly. Something as simple as;

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

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

I use the space for noErrorMessage as having no space will affect the preferred size and still effect the layout

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3

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

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

If you want to display the widget only once(and not hide it ever again) then just .add it when you need to so that it's displayed(condition/method).

All boils down to preference

Juxhin
  • 5,068
  • 8
  • 29
  • 55