Subscribe: NETBEANS 6.1
http://cogitatedminds.blogspot.com/feeds/posts/default
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
application  click  gui  java  main  netbeans  println  program  project  run  shown  system println  system  thread  window 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: NETBEANS 6.1

My Geek Records



ABOUT THIS BLOG: My older posts deals with my interaction with netbeans. However, I am now going to branch off and delve into random technical problems I face in day-to-day work.



Updated: 2014-10-06T22:08:55.709-07:00

 



How to update text box from MFC Worker Thread

2012-07-26T18:02:07.324-07:00



I had to write an MFC application in which I was using a second thread to perform some time consuming process. The main UI thread was waiting for the completion of that event by calling ::WaitForSingleObject(). During execution, I always noticed that the cursor keeps circling and the UI does not respond. How can this be avoided?

1. Create an MFC Worker Thread to handle a synchronous/blocking call.

CWinThread *thread = AfxBeginThread(DelegateThread, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);

2. In DelegateThread(), invoke the blocking call. After this, lets say you want to update a text box from the result of the blocking call. This can be done in the following way. Define the following function in main Cwnd (i.e., main UI thread)

MainCwnd::UpdateTextBox(BSTR bstr)
{
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT);
pEdit->SetWindowTextW(bstr);
}

3. Make the following call from DelegateThread()

BSTR test = SysAllocString(L"Test String");
MainCwnd->UpdateTextBox(test);
SysFreeString(test);

If all is right, this will update your text box! Also, this will avoid calling ::WaitForSingleObject() which gives a better UI feel. However, this is useful only when you need to display the result got from calling the blocking call in DelegateThread(). If there is some extra processing to be done after invoking the blocking call (eg:creating another thread that needs this result), then I cannot guarantee that this method will work.

Note: It is VERY important to note that you cannot make the call to edit the text box from the worker thread. However, you can INVOKE the function to edit the text box from the worker thread. This function should belong to the main dialog box class.



Have Fun

2008-12-09T12:39:54.032-08:00

If you are a beginner and just want to admire netbeans, I suggest you to have an overview of all the sample projects present in netbeans. Trust me,you are going to completely enjoy it. Moreover,this is the perfect way to get a trip of the platform. The one which I enjoyed most was the paint application. To view this, click on files->new project. In categories click on samples->Netbeans modules. Here, choose the paint app and click on finish.This is set as the main project. Build this project by clicking F11 and to run the program,click on F6. Then during run time,the first picture that pops up is as shown below.



(image)




After this,you can paint on the screen as shown below. To view the white sheet,click on file-> new canvas. Finally,you can play around with colours and have fun!





(image)


Similarly as shown,you can download many more loaded sample programs and have fun with them. In C applications,you have two loaded programs.
i) sample C application which prints the arguments.
ii) application which reads an input string through standard inout and prints it through standard output.
C++ applications also are two in number which includes
i) program which prints the welcome message.
ii) a multi processing application.

Like this,you have many applications. The others include the mobile application in mobility using game builder and many more applications in ruby,UML etc.



Keyboard Shortcuts

2008-12-09T12:39:54.249-08:00

I have been working on netbeans for quite some time now and I realised that there were still so many avenues that I am yet to explore,so many barriers that I need to cross. Thats one of the main advantages of blogging. Its you and your journey in that platform(which is netbeans in my case). No horrid lectures,no boring classrooms,nothing! Its all about feeling like a discoverer and actually being one!


I have been harping on netbeans making life simple in a few of my previous posts. And I have been giving a lot of keyboard shortcuts in my previous posts which were actually pretty obvious. You can realise this if you had just opened netbeans once. But today,I came across a feature that ACTUALLY gave me a wide grin. Forget API,web services, GUI and all that! One can set their own keyboard shortcuts! I don't know about you but for someone like me who is pathetic in remembering shortcuts,this was such a pleasant idea. It was like finding an old friend in an isolated island! :)


To do this,click on tools->options and then select keymap. Then,you will get a list of actions for which you can add shortcuts. You can choose one action. For example,I choose to add a shortcut for obtaining the contents of the help menu. Observe the screenshot below.



(image)



To add, choose the action and click on add. Then you get another window as shown.


(image)


After doing this,click on OK and your new keyboard shortcut is set. Apart from setting new shortcuts,you can even change the existing ones. This and much much more!!



Need for Netbeans

2008-04-09T05:23:15.033-07:00



Why is netbeans necessary??


"Necessity is the mother of all inventions", goes the old saying. Well my dear friend,its the same case here also. Although Java has become inseparably linked with the Internet, it is important to realise that java is first and foremost a programming language. Let us just take a ride down the history lane and look at the development of programming languages. We all know that it was C which first got the status of a "programmer's language". C was developed from 'B' and this fact is well known. However, C++ was also in the coming and it came! Why did this happen? Did the arrival of C++ prove C to be an incompetent language?

NO! As the complexity and demand grew, so did all the computer languages. So after C and C++, the stage was set for Java!! After installing jdk and jre, one can execute java applications at the command prompt. In this feature, it is important for the programmer to remember all commands and recollect them at the appropriate time. Isn't this all a little tedious? Don't we like everything at the click of a button? We always aim for more compact and steady platforms! Many paradigms of view.. god o god!

And all this brings us to confluence of IDE and java applications onto a single platform.. NETBEANS IDE!!! :D I know I seemed like it was the most obvious thing.. ha ha.. Well well,I just saved you a lot of work which would have taken you a lot of time to figure out.. All my blog readers.. consider yourselves blessed!! :D




Experiment Time

2008-04-14T09:50:45.822-07:00



Netbeans is NOT the place where one can gain expertise in java! So if you are a novice, I say-"stick to routines". Just to make my point felt, I want to cite an example. Consider this program.



class Fun
{
public static void main(String[] args)
{
int count,i=0;
String words;
count=args.length;
System.out.println("Number of arguments= " +count);
while (i < count)
{
words=args[i];
i++;
System.out.println(i+ ":" + "Java is " +words+ "!");
}
}
}



Execute this program in jdk and at the command for compilation,give the following statement(let your filename be Fun!)

java Fun Robust Simple Secure Portable Multithread

The output of your program would be as follows:

Number of arguments=5
1: Java is Robust!
2: Java is Simple!
3: Java is Secure!
4: Java is Portable!
5: Java is Multithread!

Isn't this such a cool output!!! Now, you know the use of String[] args!!! If you were a starter, I sure can swear that you wouldn't have figured this out.. As far as my knowledge with netbeans goes, I don't think there is an option where I can provide arguments at run-time.. (If there is that option, I am yet to discover it then!!)







Observe

2008-12-09T12:39:54.506-08:00


One of the main advantages of using IDE is that it makes life easy for a programmer.
For example,if I were to type a program which has a command to print a statement,one can observe that a window pops up as soon as "System." is typed. This window is as shown below.



(image)

This happens because the System class contains several class fields and methods and they cannot be instantiated. The facilities provided by the System class include standard input, standard output, and error output streams. By convention, this output stream is used to display information or messages that are intented to be attended by the user. "println" which is normally added at the terminal end belongs to the PrintStream. This class prints Java primitive values and object to a stream as text. Errors if present can be detected by calling the checkError() method. Additionally, this stream can be designated as "autoflush" when created so that any writes are automatically flushed to the underlying output sink when the current line is terminated. "println" just terminates the current line by inserting a line separator string(which does not mean \n).




Taking the next step

2008-12-09T12:39:54.685-08:00

Before we proceed further,one must understand the essence of Beans. To better understand its significance,consider the following. Resistors,capacitors and inductors form the building blocks of a simple electronic circuit. All these components can be reused in complex/integrated circuits which probably yield more efficiency. This is because the functionality of every component is understood and it does not change with the system. So,every aspect of the component's behaviour can be documented. Unfortunately,until recently "write-once,run-anywhere" paradigm has not been highly successful in achieving the benefits of reusability and portability. Large applications grow high in complexity and it becomes very hard to enhance. Part of the problem is that ,until recently,there has not been a standard,portable way to write software component. This is where the role of a bean comes into play. As per standard definition, a bean is a "software component that has been designed to be reusable in a variety of environments". It may perform a simple operation such as spell check in a document or a complex one like forecasting the performance of a stock.Netbeans is one such platform which also aids software development. In netbeans,it is important to realise that the platform recognizes and evaluates strictly in terms of a 'project'.Time to learn:Of the Java SE applications of netbeans,there is an application called 'Java project with existing sources wizard'. Click on File->New project. Click on java and choose the 'Java project with existing sources wizard'. Then,choose a project name and a project folder and click next. When adding the source package folder,add the folder which contains your java programs. [In a windows system,by default,all these java programs are present in C:\Program files\Java\jdk(version)\bin]Finally,click finish and your project is created.NOTE: If you include this folder,then the whole folder becomes a project and your individual programs gain the status of a file.If you have followed all the steps I have mentioned above,then its first part accomplishedNow,you can run any file present in your project. To open your file,click on your project folder which will open all the files tored in it. In my example,I am going to run a program which is stored as "Accurate.java" in my systen. To illustrate this,I am now going to run a file in my project. The screenshot is as shown below.The program I have shown is a basic multithread program. Its code is class NewThread implements Runnable{ String name; Thread t; NewThread(String threadname) { name=threadname; t=new Thread(this,name); System.out.println("New thread: " + t); t.start(); } public void run() { try { for(int i=15;i>0;i--) { System.out.println(name + ":" +i); Thread.sleep(200); } } catch (InterruptedException e) { System.out.println(name + "interrupted"); } System.out.println(name + "exiting"); }}class Accurate{ public static void main(String args[]) { NewThread ob1= new NewThread("One"); NewThread ob2= new NewThread("Two"); try{ Thread.sleep(1000); ob1.t.suspend(); System.out.println("Suspending thread one"); Thread.sleep(1000); ob1.t.resume(); System.out.println("Resuming thread one"); Thread.sleep(1000); ob2.t.suspend(); System.out.println("Suspending thread two"); Thread.sleep(1000); ob2.t.resume(); System.out.println("Resuming thread one"); } catch (InterruptedException e) { System.out.println("Main thread interrupted"); } System.out.println("Main thread exiting"); }}To run this file,we click on Build->"Complile Accurate.java" (F9). A[...]



Formatting

2008-12-09T12:39:55.016-08:00

All programmers know that formatting of a code is very important in any code. This makes the program more clear,especially when the program is too lengthy. On another platforms,the programmer needs to take the extra effort to do this. However,netbeans simplifies everything for you. To make my point clear,consider the screenshot below.



(image)


I suggest you to follow the steps carefully to format your code.

1) Select your entire program with the right click of a mouse ( the picture will be as shown above).

2) Right-click and click on the option which reads "format". The shortcut is Alt+Shift+F.

Simple,isn't it?? Just by following these two simple steps,one can arrive at a correct formatted code!




Web Services

2008-12-09T12:39:55.219-08:00

Web services?? Don't get confused..You heard me right! Double whammy for all you complete internet addicts! Netbeans allows access to API documentation of various web applications! Now,thats something new!! Firstly,what is API?? Firstly, API stands for Application Programming Interface and it implies to the source code interface your operating system provides when requests are made by computer programs. To notice this,switch on your internet and open netbeans 6.1. After this,you get an option called "services" at the left side of your screen on which you will have to click. After doing so,you will get a screen as shown below.


(image)

Right-click on the tab at facebook and click on "view API documentation". Why am I choosing facebook to explain my point? Well,today is the age of social networking and the chances that a person knows facebook is higher than the person knowing anything else!! So, your facebook application also makes method calls over the internet by sending HTTP requests to the facebook API server. This part of the option of netbeans is best learnt when the YOU choose to explore it! Like the tag line of netbeans 6.1 says..."the only IDE you need!!" Its open source! So guys,get started if you still haven't!!




GUI!

2008-12-09T12:39:56.382-08:00

A GUI is a graphical (rather than purely textual) user interface to a computer. Why is GUI necessary? Well,the term was coined because the early interactive user interfaces to the computer were not graphical but merely text oriented. This was highly cumbersome as you had to remember all the commands and the response of the computer was infamous as well. Majority of today's operating systems come with a graphical user interface. Applications typically use elements of the GUI that come with the operating system and add their own graphical user interface elements.In netbeans,GUI design takes place in the IDE workspace. The tools present in IDE's java GUI tools include 1) GUI Builder2) Inspector Window3) Palette Window4) Properties Window5) Connection Wizard6) Palette ManagerInstead of getting down to theoritical aspects of the GUI, I prefer to explain the method of implemeting this concept in netbeans by creating a project. Our project is to create a calculator.i) Click on File-> New Project-> Java(in categories)-> Java Application(in projects). After doing this, click next.ii) Give project name as Calculate and while doing so,ensure the following important points.(a) Select "set as main project" .(b) Deselect "create main class" if it is selected.iii) Click Finish.iv) In projects window, right click Calculate and select new->Jframe form.Give (a)class name: CalculateUI(b) package : my.calculateThen click finish.v) Next,we use the Palette to create to create the layout of our calculator. To use the options present in the palette,select a JPanel from the Palette and drop it onto the JFrame. In our design of the calculator, we use four jLabels, four jTextFields and eight jButtons. After dragging and dropping all of these on to the jFrame,our screen should look similar to the one shown below. If you do not clearly understand how to use GUI builder,the following webpage might come in handy. Do check this out. http://www.netbeans.org/kb/60/java/quickstart-gui.htmlHere,I have just enlarged the photo to aid better understanding.vi) Now,we rename every display text component added on to the jFrame window. To do this, double click on every component and rename. The screenshot after doing so will be as shown below.Just to make the photo more clearer,I have added an enlarged version of the photo. Hope it helps..!Here it is important to observe that basic arithmetic operations can be performed on two operands wgich is indicated by 'First Number' and 'Second Number'. 'Unary field' is used when an operation is to be performed on a single number. vii) After renaming is done,we need to add functionality to every component of the calculator. Let us start with the EXIT button. To add functionality,right click on the exit button and choose action and select action performed. The IDE will then open up the Source Code window. Now, scroll to where you implement the action you want the button to do when the button is pressed. Your source code window contains the following statement.private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){ //TODO: Add your handling code here :}We add one line to exit and our final code looks as shown below. Similarly,one should follow the same procedure for adding functionality to different buttons and the codes for all buttons are as shown below. I) EXIT Buttonprivate void jButton3ActionPerformed(java.awt.event.ActionEvent evt){ System. exit(0);}II) SQUARE button:private void jButton5ActionPerformed(java.awt.event.ActionEvent evt){ float num,result; num=Float.parseFloat(jTextField3.getText()); result=num*num; jTextField4.setText(String.valueOf(result));}III) MULTIPLY Button: private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){ float x1,x2,result; x1=Float.parseFloat(jTextField1.getText()); x2=Float.parseFloat(jTex[...]



Installation

2008-09-13T11:04:22.413-07:00



I consider that by now, all my blog readers would have taken the first step atleast. The installation procedure is quite simple and it moves smoothly. And hey,you must be bored waiting for the 183MB file to download!! :) Ah, are you telling me the links for online jokes to keep you entertained now?? :D Help yourself with the www guys! (waiting waitng waiting....)

Phew! At last,eh?? I know! So, your installation is complete. If you are facing problems, I suggest you have a reading of this page:

http://www.netbeans.org/community/releases/61/install.html

I hope its mission accomplished now!





Follow Traditions

2008-12-09T12:39:57.250-08:00



People people people! No matter how adept (image)
you are at IDE or java,traditions come first!
Meaning we always start off with our initial
little simple java program before taking it
further.. So.. here goes my mundane routine of explaining to you as to how one can carry off their first program on netbeans.

SMILE PLEASE!

In netbeans click on file->new project. After doing this,you get a window as shown below.





(image)
Click on Java application tab and click next. You then see the java application box where you enter your project name as HelloWorld(as shown ). Its very important to observe here that the main class is named automatically..!!

If you have done this,then you are on the right track. After this,writing your one line is a piece of cake because everything else in the window is ready for you! IDE's.. bliss!!!





(image) You get your window as shown in the right. Give your basic print statement as

System.out.println("Hello World");

After this is done,you have to build your program by pressing F11 or go to build->build main project. If you are not getting your output screen,go to window->output->output(OR ctrl+F4) Then,run your program by pressing F6 or run->run main project... :)

Congratulations! You wrote the simplest program on netbeans!!





Welcome!

2008-12-09T12:39:57.417-08:00

(image)
Hello everyone,
This is it! My first real attempt at blogging! And I chose NETBEANS to start with. Its really exciting for me to write this blog from a perspective of exploring netbeans i.e, as someone who is trying to get a strong grip of it. So, this is where you start your journey.. your all important initial view!!