Creating a Panel

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.

Getting ready

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.

Tip

If you are unsure how to do these steps, please refer to the previous recipes of this chapter.

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.

How to do it...

There are three ways to add a panel:

  • Creating a new JPanel
  • Drag-and-drop
  • By Manipulating the JFrame

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:

  1. Select New and JPanel Form....A new window called New JPanel Form will be shown. In the Class Name field, type MyJPanel.
  2. Leave all the other options as default and click on Finish.
How to do it...

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:

  1. With the Palette window open, expand Swing Containers.
  2. Drag-and-drop the Panel icon into MyJFrame.java.
How to do it...

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.

  1. Open MyJFrame.java, or some other JFrame implementation. The class will be shown in the GUI Editor.
  2. Right-click inside the MyJFrame.java.
  3. Select Add From Palette, then select Swing Containers, and finally select Panel.
How to do it...

A centered panel in the middle of MyJFrame.java can be seen the the previous screenshot.

How it works...

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.

There's more...

Interested in reusing JPanels as base implementations for bigger and more complex applications?

Reusing JPanels

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:

  1. Select New and JPanel Form.... A new window called New JPanel Form will be shown.
  2. Inside Class Name, type MyBaseJPanel.
  3. Leave all the other options as default and click on Finish.

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:

  • 5 Labels
  • 1 Tree
  • 3 TextFields
  • 1 Button
  • 2 Radio buttons

Arrange the components as you wish and then save the file.

Reusing JPanels

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.

Reusing JPanels

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.

Editing text label of components

There are two ways of changing the caption of the components:

  1. By selecting the desired component and pressing F2. The component will then change its looks and let the user type the content.
  2. By right-clicking on the component. After right-clicking on the component, select the first option, Edit Text, as shown in the following screenshot:
Editing text label of components

After making a few changes in the captions, it is possible to create an interface like this:

Editing text label of components
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.30.193