JFrame is a java swing container that can be used to create application windows. JFrames are the top most container in the swing applications. All the GUI components like labels, comboboxes are added into the JFrame. In this tutorial, we will learn how to make use of the Java JFrames to create swing applications and see how to manage the JFrame windows.
Simple application with JFrame
First, let’s create a simple application. We will add a button to the JFrame container.
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class JFrameTutorial { public static void main(String[] args) { //Create a JFrame JFrame jFrame = new JFrame("My JFrame Window!"); //Set JFrame size jFrame.setSize(300, 200); //Create a JPanel to contain our button JPanel panel = new JPanel(); //Add button to the panel panel.add(new JButton("Click Me")); //Attach panel to JFrame jFrame.add(panel); //Show the JFrame jFrame.setVisible(true); } }
This will create the following output
Even though the above code works, it is not perfect. If you run the above code, you will have some questions. Let me make things clear for you.
Question: Even after the JFrame is closed, the application is still running in the background. Why does my application still run after closing the main window?
This is because you haven’t specified what to do when the JFrame is closed. Remember that in a real-world application, there will be many windows, so many JFrames. By default, if the JFrame was exiting/killing the application when it is closed, then this would cause countless issues. Because each window closing will cause the application to terminate by default.
To prevent this, when the close button is pressed, the JFrame just gets hidden and will not exit the application fully. To exit the application on window close, add the following line into the code to set setDefaultCloseOperation.
public static void main(String[] args) { JFrame frame = new JFrame("My JFrame Window!"); frame.setSize(300, 200); JPanel panel = new JPanel(); panel.add(new JButton("Click Me")); frame.add(panel); //Close the application when the window is closed frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
Question: The JFrame window is opening at the edge of the screen. How to make JFrame center on screen?
By default, the Window will appear at the edge of the screen (most of the time, in most of the common operating systems). To make the JFrame window appear at the dead center of the screen, you need to set the window’s location-relative-to null. It can be done as follows.
//Set location of the JFrame relative to null to get it open at the center of the screen frame.setLocationRelativeTo(null);
Set Icon for JFrame Window
Very often, we will have to set our custom icon to the application window. With JFrame, this is possible with the method JFrame#setIconImage(Image). This will make the icon appear on the OS taskbar as well.
//Read the image you want to set as icon Image image = ImageIO.read(JFrameTutorial.class.getResourceAsStream("/logo.png")); //Set the image icon to window frame.setIconImage(image);
If you would like to learn how to set up a system tray icon, have a look into this tutorial on setting up of tray-icon with menu in Java.
import java.awt.Image; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class JFrameTutorial { public static void main(String[] args) throws IOException { JFrame frame = new JFrame("My JFrame Window!"); frame.setSize(300, 200); JPanel panel = new JPanel(); panel.add(new JButton("Click Me")); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setIconImage(ImageIO.read(JFrameTutorial.class.getResourceAsStream("/logo.png"))); frame.setVisible(true); } }
Close/Hide JFrame Programmatically
Let’s say we want to close a JFrame programmatically. This can be done with JFrame#setVisible(false) method followed by a JFrame#dispose() method to clear the object. There is a deprecated function JFrame#hide(), but you should never use it as it has been deprecated from Java 1.5.
Simply set the visibility of the frame to false, and it will hide/close the window. Then call dispose() to release the system resources used by it.
//Set visibility to false for closing the JFrame frame.setVisible(false); //Dispose the frame to release resources frame.dispose();
Another method for closing
Another easy method for closing the JFrame is by simulating the close button click event. As we have already seen, pressing the X button will close the window. We can simply simulate the close button click to close the JFrame itself. We can do that as follows
jFrame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
Maximize and minimize JFrame Programmatically
Now, let’s say we came across a requirement where we need to maximize a JFrame window programmatically. There is a function called JFrame#setExtendedState(int state) for programmatically maximizing and minimizing. Let’s see how we can do that with some code snippet.
private void minimizeJFrameWindow(JFrame jFrame) { //Minimize to task bar jFrame.setExtendedState(JFrame.ICONIFIED); } private void maximizeJFrameWindow(JFrame jFrame) { //Maximize the window jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); } private void maximizeJFrameWindowWidth(JFrame jFrame) { //Maximize the window only in the width (x-axis). Height will remain unchanged jFrame.setExtendedState(JFrame.MAXIMIZED_HORIZ); } private void maximizeJFrameWindowHeight(JFrame jFrame) { //Maximize the window only in the height (y-axis). Width will remain unchanged jFrame.setExtendedState(JFrame.MAXIMIZED_VERT); }
Conclusion
In this tutorial, we have learned how to make use of JFrame to create windows in Java. We have seen how to create it, attach icon, center on screen, maximize and minimize etc. If you have liked this article, you might also like the following articles I have written.
… [Trackback]
[…] Find More Information here on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More here to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More on on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More Information here to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] There you can find 24785 more Info to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More Information here to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More on to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More Info here on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Here you can find 9741 additional Information to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] There you can find 74316 additional Information on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More Info here on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Here you can find 35964 additional Info to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More here on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More Information here to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More on on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Find More here to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]
… [Trackback]
[…] There you can find 12451 additional Info to that Topic: genuinecoder.com/java-jframe-tutorial-with-example/ […]