In the next step, we will create a JPanel.
A JPanel can incorporate many different combinations of components and behaviors. This way, it is possible to interchange the Panels on top of the Frame and let other behavior take place at the window.
Each JPanel is configured with a layout; the layout determines how the components will be arranged on the top of the panel. NetBeans makes it easy to change the layouts with a click of a button.
It is necessary to have the source of a Java Desktop project open in NetBeans. Many of the files created in this recipe build upon previous recipes in this chapter, because we wish to give continuity to the recipes.
The project and file name references come from the other recipes in this chapter, so feel free to use the same naming if not confident enough.
It is necessary to have an empty JFrame form so the JPanel can be used.
There are three ways to add a panel:
Below is the first method to create a new JPanel, we will call it Methood 1: Right-click on the package where the JPanel should be located and:
The MyJPanel.java
class will be opened in the GUI Editor.
The Method 2 employes the drag-and-drop technique. In this method, we will use a JFrame created in the previous recipe.
Open MyJFrame.java
, or some other JFrame implementation, so that the class will be shown in the GUI Editor:
MyJFrame.java
.A component placing animation can be seen when the JPanel first enters MyJFrame.java
. By clicking on a corner of the panel it is possible to resize the component.
Adding a Panel by right-clicking on the Frame (Method 3).
For method three, we will use a JFrame created in a previous recipe.
MyJFrame.java
, or some other JFrame implementation. The class will be shown in the GUI Editor. MyJFrame.java
.A centered panel in the middle of MyJFrame.java
can be seen the the previous screenshot.
All of the methods behave mostly in the same way, that is, they create a new Panel. The difference between method one and the others is that a new, separate class is created just for the Panel; while in the other ones, a Panel is inserted inside the JFrame implementation, applied to a Layout, and used from that. The choice between methods two and three is a matter of personal preference, since the IDE just adds and configures a JPanel.
Interested in reusing JPanels as base implementations for bigger and more complex applications?
Method one is especially useful if an application with different Panels is being built and these Panels are going to interact on top of some Frame.
How to do it: Right-click on the package where the JPanel should be located and:
Let's design MyBaseJPanel with the components we think are proper for this base implementation.
Open MyBaseJPanel.java
in the GUI editor and drag-and-drop the following components from the Palettes Swing Controls submenu:
Arrange the components as you wish and then save the file.
Open MyJFrame.java
in the GUI editor and click on the corner of the Frame to resize it, making it bigger so the Panel can be accommodated inside the Frame, and finally drag-and-drop the MyBaseJPanel from the Projects window into MyJFrame.
While placing the Panel inside the Frame, you may notice that the Panel does not occupy the entire area of the Frame. It is possible to select the Panel that was included by just clicking on the area where the Panel was dropped. You can use this to quickly select Panels for deletion.
There are two ways of changing the caption of the components:
After making a few changes in the captions, it is possible to create an interface like this:
3.128.30.193