Reputation: 9627
When I create JFrame everytime I see initComponents(); in my constructor. What is it? If I remove it what is going to happen?
Upvotes: 2
Views: 45390
Reputation:
To add to Artem's answer above:
Invariably, the initcomponents() initializes all of the Java swing components objects that your front-end GUI uses using the NetBeans GUI Builder.
These swing components are automatically generated in your Java class whenever you make changes to the design of your GUI using the GUI builder.
As a general rule of thumb, you should never change any aspect of the code within this method as this method is inextricably linked to the front-end NetBeans GUI builder. The image below outlines the NetBeans GUI design builder. A user can quickly jump between the design front-end by clicking on the DESIGN button and also the source by clicking on the SOURCE button respectively
By clicking on the source button (outlined below), you will see the auto generated initcomponents() containing all of the swing components
the following link provides very good quick start guide to the essentials of NetBeans GUI builder: Designing a Swing GUI in NetBeans
Upvotes: 4
Reputation: 692
initcomponents()
is a method that NetBeans (I guess you are using it) swing Designer creates to initialise components (set default values etc.). It doesn't really have anything to do with the JFrame
class.
You can call the method whenever you like (constructor, other method). For Java, it is just like any other method. The NetBeans IDE, however, calls it inside the constructor to control the parameters you have passed via your GUI editor of Netbeans. It is by default private
.
You can think of it as the connection between the GUI Editor and Java. So if you remove it, probably you will not be able to use the functionality that NetBeans provides to work with components (which can still be ok).
Upvotes: 7