2

In Swing, we can hide JFrame by using two methods:

  1. frame.setVisible(false)
  2. frame.setState(Frame.ICONIFIED)

The difference that I discover are:

  1. frame.setVisible(false) removes icon from taskbar whereas ICONIFIED doesn't.
  2. We can add listener to ICONIFIED whereas we can't add it for frame.setVisible(false).

Is there other major difference I'm missing? Any ideas would be greatly appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Crawler
  • 1,988
  • 3
  • 21
  • 42
  • 1
    What is the default close operation of the frame? That will change the behavior of `setVisible(false)` – Andrew Thompson Sep 17 '14 at 10:27
  • JFrame.EXIT_ON_CLOSE closes the frame and if its the only alive frame of program then program also closes. I know this much. Im confused by your comment "That will change the behavior of setVisible(false)". – Crawler Sep 17 '14 at 11:01
  • Try using the others (`DISPOSE_ON_CLOSE` & `DO_NOTHING_ON_CLOSE`), consult the Java Docs for each while you're at it. For `DISPOSE_ON_CLOSE`, see [this example](http://stackoverflow.com/a/7143398/418556). The icon(s) should appear in the task-bar until the last is closed. – Andrew Thompson Sep 17 '14 at 11:09

3 Answers3

2

With setVisible(false), if the Component is not already marked invisible, setVisible calls invalidate() which invalidates the Container’s layout and the chain of parents since there is now more screen real estate in the Container and the positions of the siblings must be adjusted to flow into the freed space.

API of Component.

But the ICONFIED is doing the minimize process of a window.

codebot
  • 2,540
  • 3
  • 38
  • 89
1

We can add listener to ICONIFIED whereas we can't add it for frame.setVisible(false).

You can use a ComponentListener and handle componentHidden(...).

camickr
  • 321,443
  • 19
  • 166
  • 288
  • ComponentListener is really new to me. can you give me some more difference and list them in points. – Crawler Sep 17 '14 at 15:16
0

The major differences as I know are as follows:
1) frame.setState(Frame.ICONIFIED) just changes the state of the frame whereas frame.setVisible(false)     changes visibility of frame.
2) setState(Frame.ICONIFIED) method in class java.awt.Frame can programmatically minimize a frame     and setState(Frame.NORMAL) to restore it.
3) Invisible frame cannot use any listener but you can add listener to frame that is ICONIFIED.
4) frame.setVisible(false) removes the physical state of frame from screen whereas     setState(Frame.ICONIFIED) just changes state preserving its physical state.

These two methods have their own characteristics so be more confident in choosing one that is more suitable for your situation.

Neeman
  • 126
  • 2
  • 10