I’m new to Java’s Swing and I’m exploring it’s capabilities through NetBeans.  I’m experimenting with both hard coding and using the ‘Swing Gui Forms’ >> JFrame Form. In this post I’ll outline two ways of making your JFrame full screen, one for NetBeans JFrame Form, and the other for those hand coding their JFrame from scratch. Hope Java newbies find this useful, if you do post a comment.

#1: NetBeans JFrame Set It To Full Screen…

Okay, this sounds fairly simple. To be honest, I expected to find a check box in the JFrame properties options that would enable full screen with a single mouse click. Guess what, that check box does not exit. The gauntlet has been thrown!…

Having spent some time fiddling around with the properties options I decided to Google it. The Java online community is active and helpful, you’ll find great tutorials and shared information on many related sites (I’ve provided links to a few at the end of this article).

On this ocassion I found the answer to this issue on a humble wordress site, www.mrtextminers.wordpress.com. The actual post on the page outlining the authors solution did not work for me, but in the comments you’ll find this little gem:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// construtor code
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
setExtendedState(MAXIMIZED_BOTH);
}
});

These imports call related packages into your form with all sorts of mystical powers to help make your program sing. You can copy and paste the above imports into your file, place it under your package name, and located above your class name.

The second piece of code goes in the constructor. Copy and paste that lovely code before your closing curly bracket in the constructor.

So What Does It All Mean?

To hazard a guess…what that code does is it adds a new window listener. This listens for when the window event ‘windowOpened’ happens (when your JFrame program runs), and then sets the JFrame window to maximum width and height, equal to the dimensions of your screen. Magic!

#2: If You Are Hard Coding Your Gui…

…instead of using the wysiwyg Gui tools in NetBeans, the following is another solution that will work perfectly (see this coderanch thread).

In your class, declare your JFrame:

JFrame frame;

In your constructor add the following code:

frame = new JFrame("Test");  
Toolkit tk = Toolkit.getDefaultToolkit();  
int xSize = ((int) tk.getScreenSize().getWidth());  
int ySize = ((int) tk.getScreenSize().getHeight());  
frame.setSize(xSize,ySize);  
frame.show();//see point number 5 below for update on this line of code... 

Finally, in your method add the following:

Test app = new Test();

Briefly, what’s happening above is you:

  1. declare and create a new JFrame.
  2. You then create a new Toolkit reference…
  3. …and two integer variables. The screen width and height are assigned to the int variables. (These have to be cast as integers (ints) as the getWidth, getHeight methods use doubles).
  4. Now the new JFrame (called frame) is sized to match the two int values for width and height.
  5. Then the frame is told to show itself. (.show() is depreciated, so instead use frame.setVisible(true);)
  6. Call the JFrame into action in your main method. Note that Test is the name of the class used in this example, change Test and new Test with your own class name.
Now you have to examples of how to make your JFrame full screen, enjoy. Also, don’t forget to check out the sites I linked to in the post body and the the links below.
Useful programming/java websites: