Showing posts with label Netbeans at glance. Show all posts

How to put image in jFrame or Swing application?

            I've got so many requests on my website that how to put image in swing application or Jframe. Its very easy for one to understand. I hope you are using some IDE for creating swing application. I'm using Netbeans here to create one.

To place image on swing application you can use jLabel for that. If you are not using any IDE here is code for you.

private javax.swing.JLabel jLabel1;
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon_128.png")));
//Here image placed in package/folder called images. For better explanation read the whole article.

Now lets roll to IDE Netbeans. Netbeans implement same as above i wrote.

Step 1 : Create package/folder in project that contains images for your project.
  • Why is required so? : Say if you are building application for your client that requires images in that. Pointing image out of project folder will not be available in client's computer. So by placing/binding images within project will always available in *.jar file.

Step 2 : Create or place jLable in jFrame. 


Step 3 : Open up jLabel properties and browse icon.

properties jLabel

Step 4 : Select desired image either way from package (Recommended) or external image.

swing, netbeans

Step 5 : Press "Ok" and  now remove text of jLabel.

jLabel

Output : Finally we've done it. You can move your image at your desired place.



Why 'Test Packages' folder in netbeans?

netbeans
Some of you thought why there is a folder "Test Packages" in netbeans. There is a special thought for providing Test Packages folder in project. This will help you only if you are developing some API or say class for general use in team or company or say you are free lancer. Check the my javaQuery API's project structure in image.

You'll see there is so many classes in "Source Packages" folder. and APITest.java in "Test Packages" folder. This is how my javaQuery API. I test each code before publishing the API. "Test Packages" helps me to run my "Source Packages" code. When you create the .jar file using netbeans. It won't let include "Test Packages" class in final .jar file. 

javaQuery

How to create Test Packages Folder in your project if its missing.

You may deleted the "Test Packages" folder from your project and now you want it back to your project. Let me show you how you can achieve that. Right click on project and go to Properties. You'll find Test Packages option in right side pane in source option and click Add Folder. Check the below image and use folder name same as "Test Packages"

netbeans


How to pass argument to main class in netbeans?

netbeans
Lot of people asking me "How to pass argument to main class in netbeans?". Its very easy but you didn't explored well enough. Let me show you how you can do it.





Step 1: Right click on project and click on "Set as Main Project". Now Right click on project and go to "Properties" > "Run" option as shown in below image.

netbeans properties

Step 2 : You can see that there is browse... option for "Main Class" click on it. And select the class you wanna pass the argument. The class you select as a Main Class will always run as first class on starting of the project.


netbeans properties

Step 3: Pass the argument as show in below image. If you wanna pass multiple argument separate it by space. 

Note : If you run the class by right clicking on file. It won't accept argument and give you ArrayoutofBound Exception. To use that argument you must run whole project. Use PLAY button on top tool bar or right click on project and click RUN.
  •     - int  : 10
  •     - String single word : Hello
  •     - String more then one word : 'Hello World' (use single quote)
netbeans properties

What is Global libraries in Netbeans?


Netbeans is one of the famous editor around the Java developers. People say eclipse is better but i don't think so because in eclipse you have to configure all the thing at each step. I built so many applications in netbeans and i prefer it rather then using the eclipse.

Many of you using the netbeans for a long time but for the time shortage you didn't checked all features of the netbeans.I started new topic on my blog "Netbeans at glance". I'll give some fantastic reviews, idea, tools about netbeans. 

So back to the topic now we all use some libraries in our projects for that what we do each time we import the library from the hard drive and we have to navigate through different drives and all. It waste so much time. If you are professional programmer then you much take care of your time. I'll show you how you can save time on such a things.
  • Always create separate one folder/ drive (if you have space) for programming stuff.
  • Keep all API/ libraries in one folder. Helps to navigate if you want to copy some API/libraries to transfer in other media.
Global libraries in netbeans helps you to save your time by importing libraries each time. When ever you create new project it'll be available in list so click on it and u'll have all libraries. Let me show you how you can do this. The below thing you can also do by Tools > Libraries

Step 1: Open up any project move to the Libraries folder. Right click on it and click Add Library

Step 2: It'll show you the list of global libraries. Now Click on create


Step 3: Write down you library name and select class libraries


Step 4 : You'll have window click on Classpath and select your Jar/Folder.



Step 5: Click ok. Now you can brows the library in all project you create in netbeans so you don't need to navigate drives and folder each time.


Don't forget to follow javaQuery on Google + : https://plus.google.com/106033350352161272308/