Java Reference
In-Depth Information
The setVisible Method
Many classes of Swing objects have a setVisible method. The setVisible method
takes one argument of type boolean . If w is an object, such as a JFrame window, that can
be displayed on the screen, then the call
w.setVisible( true );
will make w visible. The call
w.setVisible( false );
will hide w .
SYNTAX
Object_For_Screen .setVisible ( Boolean_Expression );
EXAMPLE (FROM DISPLAY 17.2)
public static void main(String[] args)
{
JFrame firstWindow = new JFrame();
.
.
.
firstWindow.setVisible( true );
}
PITFALL: Forgetting to Program the Close-Window Button
The following lines from Display 17.2 ensure that when the user clicks the close-
window button, nothing happens:
firstWindow.setDefaultCloseOperation(
JFrame.DO_NOTHING_ON_CLOSE);
If you forget to program the close-window button, then the default action is as if you
had set it the following way:
firstWindow.setDefaultCloseOperation(
JFrame.HIDE_ON_CLOSE);
In the program in Display 17.2, this would mean that if the user clicks the close-
window button, the window will hide (become invisible and inaccessible), but the
program will not end, which is a pretty bad situation. Because the window would be
hidden, there would be no way to click the "Click to end program." button. You
would need to use some operating system command that forces the program to end.
That is an operating system topic, not a Java topic, and the exact command depends
on which operating system you are using.
 
Search WWH ::




Custom Search