Showing posts with label Netbeans. Show all posts
Showing posts with label Netbeans. Show all posts

January 09, 2012

Creating basic Graphical User Interface (GUI) application in java with Screenshots

Hi all,

One of my friend asked me, how to create a basic java GUI application which can run on desktop?

A small java gui desktop application, which is having a button and when user click on it, it will show the status of the application.

Source code:

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

public class PSRSampleGUI
public static void main(String[] args) {
       
        // Creating a frame
        JFrame frame_name = new JFrame("PSR");
      
        //Setting the layout to view the content
        frame_name.setLayout(new FlowLayout());
      
        // Sample lable option
        JLabel label_name = new JLabel("Click Start button");
      
        // Creating a button
        JButton button_name = new JButton("Start");
      
        // Adding button to frame
        frame_name.add(label_name);
        frame_name.add(button_name);
      
        // Adding button functionality
        button_name.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JOptionPane.showMessageDialog(null, "Started", "App Status", JOptionPane.INFORMATION_MESSAGE);
                // Call the method here ( the implemented method)
            }
        });
      
        // Displaying the frame in the center of the screen window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = frame_name.getSize();
        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);
        frame_name.setLocation(windowX,windowY);
      
        // Displaying the Frame with packing all the contents properly
        frame_name.setVisible(true);
        frame_name.pack();
    } // main method ends here
} // class ends here

Screenshots:
  • By Running the application, you will get the following window in the center of your screen.
  • After clicking on "Start" button, you will get the following window

If you have any queries, please mail to psrdotcom@gmail.com

December 27, 2011

Java Native Interface (JNI) in Netbeans with C/C++ and Java Hello World Sample Source Code

Hi all,

Today I configured the Netbeans IDE to work with Java Native Interface (JNI).

Pre-Requisites:
  1. Java Development Kit (JDK)
  2. Cygwin (or) MinGW & MSYS
  3. NetBeans IDE with Java and C/C++ Bundle

Installation:
  • Install JDK
  • Install Cygwin (or) MinGW&MSYS
  • Note: While installing please choose the destination directory name without spaces
  • I have installed NetBeans IDE All Version, because I thought, I will need all the technologies. That is different approach.

Configuration:
  • Set the environment variables like JAVA_HOME and PATH with Java Installation Directory and JDK bin, Cygwin/MinGW&MSYS bin directories respectively
  • Cygwin/MinGW integration with NetBeans
  • Configure the C/C++ with Tools MinGW bin directory, which automatically fills the corresponding compilers

Source Code:

  • Create a sample java application with the following detail

package javaapp1.JavaApp1;

public class JavaApp1
{
public static void main(String [] args)
{
  new JavaApp1.nativePrint();
}

private native void nativePrint();
}

  • Clean and Build the java project
  • In Command Prompt, Navigate to Netbeans Project Directory
    • cmd> cd Netbeans/ProjectDir
  • Create the Java header file
    • cmd> javah -o JavaApp1.h -classpath JavaApp1\build\classes javaapp1.JavaApp1

  • Create a C/C++ Dynamic Library Project in Netbeans (In this example, it is CppDll1)
  • Change some of the project properties
  • Make sure that, you have downloaded and configured the proper Cygwin/MinGW with exact Architecture 32bit/64bit
  •  Make sure that you are using proper gcc for 32bit/64bit dll creation
  • Linker must be of same architecture, 32bit/64bit


  • Include the Java header file, which we have created in Java Project.
    • Right click on the "Source Files"-> Select "Add an Existing Item"
    • Select the created header file from Project Home Directory
    • (OR)
    • Manually copy the created header file from Project home directory to CppDll directory
  • Create a C Source file in "Source Files" in CppDll1 project
 #include "JavaApp1.h"

JNIEXPORT void JNICALL Java_javaapp1_JavaApp1_nativePrint(JNIEnv *env, jobject obj)
{
    printf("\nHello World from C\n");  
}
  • Clean and Build the C code. I have faced the following issue.
    • Issues Faced:
      • I am using Windows 7 64-bit and installed Jdk 64-bit. But initially I have installed MinGW 32 bit and it is generating only 32 bit DLL which is not suitable to run the Java application in NetBeans.
    • Resolution:
      • Downloaded MinGW 64 bit and configured in the NetBeans C/C++ Options
  • Copy the .dll file path
  • In Java project, open the main class and add the following content
package javaapp1.JavaApp1;

public class JavaApp1
{
static
{
  System.load("dll file path");
}
public static void main(String [] args)
{
  new JavaApp1.nativePrint();
}

private native void nativePrint();
}
  • Clean and Build the project
  • Run the Java project to see the final JNI output.

References:
http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
http://www.cygwin.com/
http://sourceforge.net/projects/mingw-w64/
http://netbeans.org/community/releases/71/install.html

Please send your feedback to psrdotcom@gmail.com

December 22, 2011

Netbeans Plugin Install, Update, Problem, Troubleshoot

Dear all,

I was facing an issue while updating my netbeans. It was showing the following screen


I tried to  resolve this problem by checking the proxy configuration, firewall configuration, refreshing the content. None of them solved my problem.
Note: You please try all those settings, it may work for you. If none the above configuration solves your problem then follow the below solutions.


I have found 2 solutions to resolve/troubleshoot this problem.

a) Manual Update
  1. Copy the path of the networking problem link
  2. Download the .nbm file
  3. In Netbeans, Click on Tools (Menu) -> Select "Plugins" option
  4. Select the tab "Downloaded" -> Click "Add Plugins"
  5. Choose the appropriate .nbm file from the downloaded directory
  6. Click "Open" to view in the "Downloaded" tab
  7. Check the plugin and Click on "Install" button
  8. Now the Netbeans plugin will be updated and it will auto download the dependencies
Note: Above Screens are of Netbeans IDE 7.0.1. For your netbeans IDE the options may be different menus but the procedure is same.
  • Pros: Easy to install the specified/chosen plugin and update
  • Cons:
    • Issue: While updating, if it was not able to get the dependency, then it will show the same "Networking Problem"
    • Resolution: Follow the same procedure from step 1

b) Automatic Update:

  1. In Netbeans, Click on Tools (Menu) -> Select "Plugins" option
  2. Select the tab "Settings"
  3.  
  4. Click "Add" button to add a plugin update center
  5.  
  6. Enter the name and path of the plugin update center and Click on "OK"
  7. It will auto-refresh the plugins available
Install Plugin:
You can download many netbeans plugins from "Available Plugins" tab of "Plugins" window.
Install your own choice of plugins and enjoy application development with Netbeans.

If you have any queries, please contact me on psrdotcom@gmail.com

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com