Netbeans/C2/Handling-Images-in-a-Java-GUI-Application/English
Title of script: Handling Images in a Java GUI Application
Author: Sindhu Ghanti
Keywords: Netbeans, Images, Java, GUI
Resources for "Handling Images in a Java GUI Application"
|
|
Slide 1
(Welcome Slide) |
Hi everyone.
|
Slide 2
Prerequisites |
We assume that you have the basic working knowledge of Netbeans.
We also assume that you know to place text fields, buttons, menus, etc. on a JFrame form.
|
Slide 3
Introduction |
In this tutorial, we will learn in detail about handling images
|
Slide 4
System Setup |
For this demonstration, I am using the Linux Operating System Ubuntu v11.04
|
Slide 5
Handling Images |
The standard way to handle and access images in a Java Application is by using the getResource() method.
|
Slide 6
Lesson Outline |
In this tutorial, we will -
Now let us switch to the IDE to create our sample application. |
Switch to Netbeans Window
From the Menu bar >> choose File >> New Project |
Creating the Application
From the File menu, choose New Project. |
In the New Project Wizard, select Java >> Java Application >> click Next. | Under Projects, select Java, under Categories select Java, Java Application and click Next. |
In the Project Name field, type ImageDisplayApp. | In the Project Name field, type ImageDisplayApp. |
Uncheck the Create Main Class checkbox. | Clear the Create Main Class checkbox. |
Point to / Check the Set as Main Project checkbox | Make sure that the Set as Main Project checkbox is selected. |
Click Finish. | Click Finish. The project is created in your IDE. |
Narration Only | In this section, we will create the Jframe form and add a Jlabel to the form.
|
In the Projects window, clicked on the plus sign next to ImageDisplayApp node. | In the Projects window, expand the ImageDisplayApp node. |
Right-click the Source Packages node >> click New >> Jframe form. | Right-click on the Source Packages node and choose New and then Jframe form. |
In Class Name >> type Image Display. | In the Class Name field, type ImageDisplay. |
In Package Name >> type org.me.myimageapp. | In the Package field, type org.me.myimageapp. |
Click Finish. | Click Finish. |
In the Palette, select Label and drag it to the Jframe. | Now let us add the Jlabel.
|
Show Design part in the IDE. | For now, your form should look something like this. |
In the Projects window, point to Source Packages | Adding a Package for the Image
When you use images or other resources in an application, typically you create a separate Java package for the resource.
|
In the Projects window >> right-click org.me.myimageapp >> choose New >> Java Package. | In the Projects window, right-click the org.me.myimageapp node and choose New > Java Package.
|
In the New Package Wizard >> add .resources to org.me.myimageapp. | In the New Package Wizard, add .resources to org.me.myimageapp.
|
Click Finish. | Click Finish. |
Point to the Projects window | In the Projects window, you should see the image appear within the org.me.myimageapp.resources package, when you add the image. |
In the GUI designer, select the label that you have added to your form. | In this application, the image will be embedded within a Jlabel component.
|
In the Properties window >> click Properties >> scroll to Icon property.
|
In the Properties window, below the palette, on the right hand side, click the Properties category and scroll to the Icon property.
|
Click the ellipsis (...) button. | Click the ellipsis (...) or the three dots on the right side.
|
In the Icon Property dialog box >> click Import to Project. | In the Icon Property dialog box, click Import to Project. |
Choose any image. | In the file chooser, navigate to the folder that contains your image that you want to use. |
Click Next. | Click Next. |
In Select Target Folder >> select the Resources folder. | In the Select Target Folder page of the wizard, select the Resources folder. |
Click Finish. | And click Finish. |
Narration Only | After you click Finish, the IDE copies the image to your project.
|
Click OK | Click OK here. |
Right-click on ImageDisplayApp >> select Clean & Build | Right click on your project node and select Clean and Build option.
|
In the Files window >> ImageDisplay project >> dist>> point to the jar file | You can now go to the Files window, and under the build folder, and under dist folder, you can see the jar file.
|
In the Design view, point to the image | It also displays your image on the label in the Design view of your form. |
In the Properties window >> select Text property >> delete jLabel1. | At this point, you can do some simple things to improve the appearance of the form.
|
Narration Only | That value was generated by the GUI Builder as display text for the label.
|
Drag the label/image to center it | Drag the label to center it on the form. |
In the GUI Designer >> click Source tab. | To view the generated code
In the GUI Designer, click the Source tab. |
Scroll down to the line that says Generated Code. | Scroll down to the line that says Generated Code. |
Click the plus sign (+) to the left of Generated Code | Click the plus sign (+) to the left of the Generated Code line to display the code that the GUI Designer has generated. |
Show jLabel1.setIcon(new.......) | Here, the keyline is this. |
In the Source view >> select and show the setIcon() method | Since you have used the Property editor for jLabel1's icon property, the IDE has generated the setIcon method. |
In the Source view >> select and show the getResource() method | The parameter of that method contains a call to the getResource() method on an anonymous inner class of ImageIcon. |
Right-click on the image in the Design view. | Once your image has been added, right-click on it in the Design view itself. |
Click on Events >> Mouse >> mouseClicked | Click on Events > Mouse > mouseClicked. |
View switches to Source mode | The view is switched to the Source mode. |
In the Source view >> show the code being typedThe code if given over here will be better. So that i can type it. | Here you can add the code to customize your action on a mouse click.
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); String randomText; randomText=”A structure carrying a road, path, railroad, or canal across a river, ravine, or other obstacle”; JLabel label=new Jlabel(“Bridge: “+randomText); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true);
|
Highlight the line in the code snippet which creates a JFrame | First I have created a new Jframe for the pop-up. |
Highlight the line in the code snippet which sets the close operation | And I have set the default close operation. |
Highlight the line in the code snippet where the text is added | And finally provided the text for the pop-up. |
At the beginning of the file >> type >> import javax.swing.*; and java.awt.*; | After adding these lines of code, let us import the necessary packages by adding two statements at the beginning of the file.
Include: import javax.swing.*; import java.awt.*;
|
Show the entire code in the Source view | Let us now build and run the application.
We have generated the code for accessing and displaying the image. |
Narration Only | Let us build and run the application to ensure that the image is accessed.
|
Narration Only | When you set the Main class, the IDE knows which class to run when you run the project. |
In the Files window >> show jar file | In addition, this ensures that the Main class element in the application's JAR file is generated when you build the application. |
Right-click the ImageDisplayApp project's Node >> choose Properties. | Right-click the ImageDisplayApp project's Node in the Projects window, and choose Properties. |
In the Project Properties dialog box >> select Run | In the Project Properties dialog box, select the Run category on the left side. |
Click the Browse button next to Main Class field. | Click the Browse button, that is next to the Main Class field. |
Select org.me.myimageapp.ImageDisplay class >> click Select Main Class button >> click on OK | Then select the org.me.myimageapp.ImageDisplay and click the Select Main Class button.
|
Right-click on the Project node >> select Clean & Build. | Right-click on the Project node, and select Clean & Build. |
Point to Build Products of the application in the Files window. | You can view the Build Properties of the application in the Files window. |
Point to the Build folder | The Build folder contains the compiled class. |
Point to the dist folder | The dist folder contains an executable JAR file that contains the compiled class and the image. |
Click on Run. | Now choose Run from the tool bar. |
Switch to Output Window
|
Our output window opens with the image.
|
Slide 7
Assignment |
Now, time for the assignment!
|
Switch to Netbeans Window
Show solved assignment |
I have already created the assignment. Let us run the assignment project. Your assignment should look similar to this.
|
Slide
Summary |
So, to summarize this tutorial, we have,
|
Slide 8
About the Spoken Tutorial Project |
Watch the video available at the link shown on the screen.
|
Slide 9
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
|
Slide 10
Acknowledgements |
Spoken Tutorial Project is a part of the Talk to a Teacher Project.
|
Slide 11
About the Contributor |
This tutorial has been contributed by IT for Change. Thank you |