Updated March 17, 2023
Introduction to JFrame in Java
JFrame is a java class that is extended by Frame class of Java. JFrame is treated as the main window. In JFrame different elements such as labels, text fields, buttons can be added. These elements on JFrame create Graphical User Interface.
JFrame is also known as Swing top-level container. Swing refers to the GUI widget Toolkit for creating applications in JAVA. Swing is lightweight & plate form independent. JFrame class in the application can be created in the following way
public class JFrame extends Frame implements WindowConstants, Accessible, RootPaneContainer
JFrame Constructor
JFrame Constructor & its descriptions are given below.
- JFrame() – JFrame() is a JFrame class constructor which creates a new Frame. By default, it remains invisible.
- JFrame(String title, GraphicsConfiguration gc) – This constructor creates a JFrame in the specified graphical configuration & with the specified title as in parameter.
- JFrame(GraphicsConfiguration gc) – This constructor creates a JFrame in the specified graphical configuration as it is in the parameter.
- JFrame(String title) – This constructor creates a JFrame with the specified title as in parameter.
JFrame Methods
JFrame class provides some methods which play an important role in working with JFrame.
1. AccessibleContext getAccessibleContext() – This method gets the accessible context that remains associated with the JFrame.
2. Container getContentPane() – This method creates the JFrame’s contentPane object.
3. Component getGlassPane() – This method creates the glassPane object for JFrame.
4. int getDefaultCloseOperation() – When the user clicks on the close button on this Frame then this method returns the operation.
5. JMenuBar getJMenuBar() – Menubar set created at the Frame by using this method.
6. JLayeredPane getLayeredPane() – LayeredPane object is returned by this method.
7. JRootPane getRootPane() – The rootPane object is returned by this method.
Below given methods [8 – 15] access modifier “protected” need to be added at the start of the method –
8. addImpl(Component comp, Object constraints, int index) – This method adds the specified child element for this frame.
9. JRootPane createRootPane() – Create the default rootPane & called by the constructor.
10. void frameInit() – It is called by the constructor to initialize the JFrame.
11. boolean isRootPaneCheckingEnabled() – Calls to add & setLayout are forwarded to contentPane or not is validated by this method
12. String paramString() – This method returns JFrame in the form of String.
13. void processWindowEvent(WindowEvent e) – This method process the window event occurred on the window component.
14. void setRootPane(JRootPane root) – This method sets the rootPane Property.
15. void setRootPaneCheckingEnabled(boolean enabled) – This one sets calls to add & setLayout are forwarded to the contentPane or not.
16. static boolean isDefaultLookAndFeelDecorated() – If the newly created JFrames have their Window decorations as provided by the current look and feel then this method returns true.
17. TransferHandler getTransferHandler() – This method gets the transferHandler property.
18. void remove(Component comp) – For removing the specific component from JFrame container we can use this method.
19. void repaint(long time, int x, int y, int width, int height) – This method is used to repaint the specific rectangle inside of the component in few milliseconds.
20. void setContentPane(Container contentPane) – Property of the contentPane is set by this method.
21. void setDefaultCloseOperation(int operation) – Default operation set by this method which will happen when the user initiates the close operation for a Frame.
22. void setGlassPane(Component glassPane) – glassPane property can be set by using this method.
23. void setIconImage(Image img) – Icon for the window frame can be set by using this method.
24. void setJMenuBar(JMenuBar menubar) – Menubar for JFrame can be set by using this method.
25. void setLayeredPane(JLayeredPane jlPane) – LayeredPane Property is set by this method.
26. void setLayout(LayoutManager manager) – LayoutManager is created by using this method.
27. void setTransferHandler(TransferHandler newHandler) – This one sets the property related to the transferHandler, This one creates a mechanism to support the data transfer for the component.
28. void update(Graphics g) – This one calls the paint to color the frames & elements
Examples of JFrame in Java
Here are the following examples mentioned below.
Example #1
Please see the below-given example
import javax.swing.*;
public class demoJFrame extends JFrame{
JFrame frame;
demoJFrame(){
setTitle("A Sample JFrame Window");
JButton button = new JButton("click");
button.setBounds(100, 100, 115, 55);
add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args){
new demoJFrame();
}
}
In the above-given example, the swing library is imported to include different classes to work with the Frame & events. In the below screenshot a frame is created using JFrame classes of swing library.
Example #2
In this example, using multiple methods of JFrame, Layout & Events classes to create forms inside of the frame.
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
public class JFrameDemo{
public static void main(String[] args){
// Create frame with title Registration Demo
JFrame frame= new JFrame();
frame.setTitle("JFrame Based Contact Form");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel headingPanel = new JPanel();
JLabel headingLabel = new JLabel("Contact Us Panel");
headingPanel.add(headingLabel);
JPanel panel = new JPanel(new GridBagLayout());
// Constraints for the layout
GridBagConstraints constr = new GridBagConstraints();
constr.insets = new Insets(5, 5, 5, 5);
constr.anchor = GridBagConstraints.WEST;
// Setting initial grid values to 0,0
constr.gridx=0;
constr.gridy=0;
JLabel nameLabel = new JLabel("Enter your name :");
JLabel emailLabel = new JLabel("Enter your email :");
JLabel phoneLabel = new JLabel("Enter your phone :");
JLabel msgLabel = new JLabel("Message :");
JTextField nameInput = new JTextField(20);
JTextField emailInput = new JTextField(20);
JTextField phoneInput = new JTextField(20);
JTextArea textArea = new JTextArea(5, 20);
panel.add(nameLabel, constr);
constr.gridx=1;
panel.add(nameInput, constr);
constr.gridx=0; constr.gridy=1;
panel.add(emailLabel, constr);
constr.gridx=1;
panel.add(emailInput, constr);
constr.gridx=0; constr.gridy=2;
panel.add(phoneLabel, constr);
constr.gridx=1;
panel.add(phoneInput, constr);
constr.gridx=0; constr.gridy=3;
panel.add(msgLabel, constr);
constr.gridx=1;
panel.add(textArea, constr);
constr.gridx=0; constr.gridy=4;
constr.gridwidth = 2;
constr.anchor = GridBagConstraints.CENTER;
// Button with text "Register"
JButton button = new JButton("Submit");
// add a listener to button
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
headingLabel.setText("Thanks for Contacting us. We'll get back to you shortly.");
nameInput.setText("");
emailInput.setText("");
phoneInput.setText("");
textArea.setText("");
}
});
panel.add(button, constr);
mainPanel.add(headingPanel);
mainPanel.add(panel);
frame.add(mainPanel);
frame.pack();
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In the above-given example, A window frame is created using JFrame. & creating elements using the JPanel() class methods. other libraries are being used for layout & events. The following are the screenshot attached after compiling & running the Java code.
Conclusion
JFrame is the class of the swing package which mainly used to create the JFrame. We can say that the JFrame window can be created by instantiating the JFrame class. The majority of new GUI Java codebases are using JavaFX which is the swing replacement in the latest version of Java. In the coming days, programmers are moving to the JavaFX due to the latest version of Java libraries & extended features.
Recommended Articles
This is a guide to JFrame in Java. Here we discuss the constructors and methods of JFrame along with respective examples in detail. You may also have a look at the following articles to learn more –