Java Tutorial

by Dinesh Thakur Category: Swing

Every program with a Swing GUI must contain atleast one top level swing container and JFrame is the most commonly used top level container for creating GUI applications.

 
by Dinesh Thakur Category: Swing

In this Example, JFrame class is a predefined class present in swing package. This class is used to generate a frame and takes string argument. getTooZkit () method is a predefined method of Toolkit class. It returns the system's default Toolkit object. The default Toolkit is identified by the System property awt.toolkit.

 
by Dinesh Thakur Category: Swing

In this Example, Calendar class is an abstract class present in java.util package. getInstance method is a static method of Calendar class. This method is used to create the object of Calendar class. Return type of this method is Calendar class object. Syntax-public static Calendar getInstance (). get method is a predefined method of Calendar class. It is used to return the value of a given calendar field. Return type of this method is int type. Syntax-public int get (int)



 
by Dinesh Thakur Category: Swing

JOptionPane Class is present in javax.swing package. This class is used to display simple dialog boxes to get the text input From the user. showInputDialog method is a static method of JOptionPane class. This method uses single parameter that specifies the prompting message that is displayed in the dialog box. In this program message is- "Enter the Number for Right Angle Trangle Height?" This method returns string value. Syntax- public static String showInputDialog (String s).

 
by Dinesh Thakur Category: Swing

Java class named JOptionPane that produce dialog boxes. A dialog box is a GUI object in which you can place messages that you want to display on the screen. The showMessageDialog() method that is part of the JOptionPane class. The showMessageDialog() method is followed by a set of parentheses. The showMessageDialog() are two arguments. The first argument is null. its means the message box should be shown in the center of the screen. After the comma, second argument is the string that should be output.

 
by Dinesh Thakur Category: Swing

The basic component that require whenever we implement a visual interface to rid Swing is the JFrame (top-level container) class. This class encapsulates a classic window any operating system with graphical environment (Windows, OS X, Linux etc.)



 
by Dinesh Thakur Category: Swing

Progress indicators or progress bars are the new controls that give the users some indications of the progress of an operation. When we call JProgressBar, we can set the maximum and minimum value for the progress bar.

 
by Dinesh Thakur Category: Swing

Lists are supported in Swings by the JList class. It is a very popular control as it allows the user to present a list of items in an easier and efficient manner. It also allows hiding the long list of items by making the lists scrollable. JList has been inherited from a long list of classes.

 
by Dinesh Thakur Category: Java Methods

To create a class variable or method, include the word static in front of the method's name. The modifier static typically comes after any protection modifiers. Given below is an example that illustrates how a class variable may be created in a program.

 
by Dinesh Thakur Category: Java Methods

Accessor methods are used for initializing and accessing the value of instance variables. The value of these instance variables can be used further in the program. For creating accessor methods, it is required to create two methods among which one method is used to initialize the value and other is used to retrieve the value. An accessor method makes the program more readable and understandable. Moreover, accessor methods are similar to any other method, as can be seen from Program.

 
by Dinesh Thakur Category: Java Methods

A Java method is a collection of statements, all performing a particular operation.  Let’s say that you were calling the method called System.out.println(); Java executes some different statements that print a message to your console.  We’re going to look at the creation of methods or, as they often called, functions.

 
 
by Dinesh Thakur Category: Java Methods

Since the string variable str is static, it is initialized to "This is second line displayed" (static variables are initialized before an object is initiated). Then, the static block is executed which will invoke the disp() method displaying the message: "This is first line displayed". Static() method can be invoked without the need of any object. In main(), again disp() method is called with the string str (This is second line displayed). After that an object SMV is made, and it will get a member str2 initialized to "This is last line displayed". Then disp() is called with SMV.str2 which displays: This is last line displayed on the screen

 
by Dinesh Thakur Category: Java Methods

Tail recursion occurring when the recursive call is at the end of the recursive instruction. This eliminates the potential problem of stack overflow. A call is tail recursive if nothing has to be done after the call returns. i.e. when the call returns, the returned value is immediately returned from the calling method.

 
by Dinesh Thakur Category: Java Methods

In this program, user is asked a number to enter which is stored in integer form in variable n. The value entered is say 1023.Now, this value of variable n is passed to parameter x by invoking revers() method. In revers() method, x is divided by 10 and the remainder is displayed (% returns remainder). When 1023 is divided by 10, the remainder will be 3 which is displayed on the screen. Then, again revers() is invoked with value 102/10 i.e. with 102 (because the output of dividing two integers is also an integer). 102 is assigned to variable x. Again, 102 is divided by 10 and the remainder is displayed. The remainder this time will be 2 which is displayed. Again revers() method is invoked with 10/10 i.e. which 1. And the process continues until value of x becomes 0.

 
by Dinesh Thakur Category: Java Methods

Recursion occurs when a function calls itself in its own body. That is, in the body of the function definition there is a call to itself. When the function calls itself in its body, it results in an infinite loop. So, there has to be an exit condition in every recursive program.

 
by Dinesh Thakur Category: Java Methods

There are two ways of passing arguments to a method (function/subroutine):

1. Call by Value

This method copies the value of an argument into the formal parameter of the method. Therefore, changes made to the parameter of the method are limited to that method only and there is no impact of changes on the argument. That is, when control returns back to the caller method, earlier values of the arguments will be seen.

 
by Dinesh Thakur Category: Java 2D Example

The Java2D API provides advanced two-dimensional graphics capabilities for programmers who require detailed and complex graphical manipulations. The API includes features for processing line art, text and images in packages java.awt, java.awt.image, java.awt.color, java.awt.font. java.awt.geom, java.awt.print and java.awt.image.renderable.



 


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.