Sunday, 15 February 2026

Tutorial on Swing Components

 Swing library and it's categorization of components and controls


Basically, Swing library provides around 50+ components. These all components are categorized containers, windows and controls etc. based upon their behavior and working style. 

Apart of  these default components, As I wrote in earlier posts, Swing components are customizable and new components can be invented, on need basis. 

Default Swing components and their categorizations are as below:
  • Swing Containers
    • Panel
    • Tabbed Pane
    • Spilt Pane
    • Scroll Pane
    • Tool Bar
    • Internal Frame
    • Desktop Pane
    • Layered Pane
  • Swing Controls
    • Label
    • Button
    • Toggle Button
    • Check Box
    • Radio Button
    • Button Group
    • Combo Box
    • Check Box
    • List
    • Text Field
    • Text Area
    • Scroll Bar
    • Slider etc
  • Swing Windows
    • Dialog
    • Frame
    • Color Chooser
    • File Chooser
    • Option Pane
  • Swing Menus
    • Menu Bar
    • Menu
    • Menu Item
    • Popup Menu
    • Separator
  • Swing Fillers
    • Glue
    • Horizontal Glue
    • Rigid Area
    • Vertical Glue etc.
Example Window with some Controls and containers:

 
Fig 1: Sample Controls from Swing Toolkit



That is all for now. Happy coding!

Tutorial on Swing Containers and Controls

Tutorial on Creating Graphical User Interfaces with Swing and AWT

Graphical User Interface, in short GUI is developed using java programming language's toolkits Swing and AWT. AWT also stands for Abstract Windowing Toolkit.


When Java programming language evolved during the year 1994 - 95,  during it's first version, AWT toolkit was introduced. AWT uses OS graphics which breaks the Java law. i.e Java programming language is platform independent. 


Platform independence means, write once and run anywhere


AWT toolkit, uses operating system resources. Hence, using AWT library is heavy, that means, using it takes maximum computers processing and computation time and slows down the computers performance.

And also, there are very limited widgets or components available. Since the native OS also provides limited widgets for Java programming language to use.


To overcome these all issues, the Java team has invented a toolkit or library called, Swing. Swing is included in JFC, which stands for Java Foundation Classes.

Swing is developed using 100 percent java code and does not depend on native OS's resources at all.

Graphics developed with Swing are very quick in response time and has elegant look. And also, Look and Feel is customizable according to the need.


I write about Look and Feel, also called as PLAF [Pluggable Look And Feel] in Swing terminology, in near future.

I also write about Swing containers and components, How actually components and containers can be created by following best coding practices and adding and changing the components' default properties etc. in coming articles.

One of  the best usecases of using Swing library is, Netbeans IDE. Netbeans is industry endorsed for developing desktop application using AWT and Swing toolkits from Java programming language.

You can download Netbeans IDE from here:

                                         Apache NetBeans Releases


To begin, learning Swing toolkit is installing Netbeans and writing Swing code using Netbeans. 


That is all for now. Happy coding :)



    


  

 

Wednesday, 11 February 2026

Tutorial on Java program for swapping 2 integers

Swapping 2 integers with temporary variable



Swapping Technique:

If there are two variables with some integer values and needed to swap variable a's value in variable b and b's value to a

Swapping two integers with temporary variable is as easy 10 lines of code. It does not include any mathematical operations.


Example:


If variable a has value 10 and variable b has value 20, the algorithm, supposed to swap values in such a way, that variable a will have value 20 and variable b will have 10.

int a = 10; // variable a has 10 as a value

int b = 20; // variable b has 20 as a value


Code



package com.allabtjava.alg;


/**

* @author Nagasharath K

*/

public class Swap_With_Temp {

public static void main(String[] args) {

int a = 10;

int b = 20;

System.out.println("Variable a has " + a + "and b has " + b + "before swapping");

int temp = 0;

temp = a;

a = b;

b = temp;

System.out.println("After swapping variable a has " + a + " and b has " + b);

}


}


Output:


Variable a has 10 and b has 20 before swapping

After swapping variable a has 20 and b has 10



Swapping 2 integers without temporary variable

Now, let us swap 2 integers without the help of temporary variable.

This is possible by storing the difference between variable a and variable b and do the simple
mathamatical operation as done in the below program


Example:


package com.allabtjava.alg;


/**

* @author NAGASHARATH K

*/

public class Swap_Without_Temp {


public static void main(String[] args) {

int a = 70;

int b = 50;

System.out.println("Variable a has " + a + " and b has " + b + " before swapping");

int c = b - a;

a = a + c;

b = b - c;

System.out.println("After swapping variable a has " + a + " and b has " + b);

}


}



Output:


Variable a has 70and b has 50 before swapping

After swapping variable a has 50 and b has 70



Monday, 9 February 2026

SUDOKU - Just for fun!

Sudoku Game

On the launch of an application user is presented with three options: Easy, Medium and Hard. Easy has 30 numbers on board, medium has 50 and hard option will have only 20 numbers on board. Once the difficulty is selected user will be presented with a Sudoku board. Below the board there will be numbers which one can select to fill the blank spaces on the board. Note that only the correct number can be placed on the specific spot. Once the user fills all the blank spots on the board, means he has correctly solved the puzzle.

Number systems and Conversions

Decimal to Hex Converter

Decimal to Hexadecimal Converter

Hexadecimal value:

Hex to Decimal Converter

Hex to Decimal Converter

Decimal Value:

Binary to Decimal Converter

Binary to Decimal Converter

Decimal Equivalent:
</!doctype>

Thursday, 11 December 2025

Tutorial on Java classes and objects

 Java Classes and Objects:

Classes and objects both are treated in similar way in all programming languages. Java is not exception.

Understanding classes and objects with real world example


Consider a physical class room with lot of real world objects. What we Observe in class room..?

  • Chairs 
  • Table
  •  Students
  •  Teacher
  •  Black board
  • Markers and Erasers etc


So, a programmer needs to instruct the computer, how to create a class room object and it's subsequent objects. like, chair, table, teacher and student etc.

If programmer wants to create or develop a School application, he would need to create:

  • class room object
  • Office room object,
  • Student object,
  • Stationary object
  • Play room object etc


Let us now create School application with above mentioned objects and classes using java programming language.


To create Objects, first of all, we have to create respective classes in java


public class School_Application{

     public Student students;

     public Teacher teachers;

     public ClassRoom classRoom;

      public School_Application(Student students, Teacher teachers, ClassRoom classRoom){

          this.students = students; 

          this.teachers = teachers; 

          this.classRoom = classRoom;

   }

}

Subsequently, programmer needs to create, Student, Teacher and ClassRoom classes and respective objects in main() method.


Objects in java are created using new operator. Example:

School_Application app = new School_Application(student, teachers, classRoom);


Happy coding! :)

Monday, 17 November 2025

Tutorial on coding and best practices

 Write code or write code in right way...

Writing a coding solution to a problem can be done in many ways. If the solution can be obtained with less number of code lines, then it is easy to understand and maintain.

But what if code is large with having millions of lines. Moreover maintenance and readability becomes difficult as the code base grows... Right? 

In this blog I want to share my views and experiences on writing code efficiently and effectively with less maintenance efforts.

Aspects to consider while coding:

  • Naming convention
  • Cyclomatic Complexity
  • SOLID principles
  • DRY Principle
  • Managing dead code
  • Comments


Naming conventions:

Every programming language mandates few norms in order to name variables, classes and functions etc. For ex: Java uses class names first letter to be a upper case and rest to be lower case letters. 

But if we dig little bit more, naming conventions are not limited to only upper case or lower case letters and using alphanumeric letters etc.

Naming a variable must be enough concise as naming your first child's name...

Few bad names for variables and constants:

          int final static TWO = 2;  [wrong]

          int final static MAX_LENGHT = 2; [Correct] 

  

Cyclomatic Complexity:

Cyclomatic complexity is a measure or metric to estimate complexity level of a snippet or block of code. 

Cyclomatic complexity and best practices:

  • Never put more than three conditional checks (&& operator and || operator etc.) in one if condition
  • Never exceed more than thirty lines in one function or method.
  • Never exceed more than 1000 lines in one class or one file.

SOLID principles:

Usually developing a large and scalable application, requires better design. Adhering to SOLID principles yields better scalable and easily maintainable applications.

There are five SOLID principles. They are:

  1. Single Responsibility Principle
  2. Open/Closed Principle
  3. Liskov's Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

DRY Principle:  

DRY stands for Do Not Repeat Yourself. Sometimes, There could be a need to use same or similar set of lines of code to be used multiple times in the same application. For ex. String utilities, DB connections or updating records in database etc.

Writing frequently used code snippets multiple times is not a good practice. Instead move this code to a function or method and use this method in places where ever required.

Simple use cases are:

  • DB Connections
  • File IO Operations
  • String Utilities etc. 

Managing Dead Code:

Sometimes there are some lines or instructions in code base, which are completely not executed in all cases at all. This is considered as dead code. 

Dead code reduces the quality with respect to the readability.  


Comments:

Comments increases the readability of the program for programmers. Comments are written at class level, method level and variable level [Constants, instance variables and class variables] differently. 

The reason for this difference is, the pattern of comments is identified by API document generator tools and generates API documents.


Conclusion:

What all I tried to share through this post is, writing code for a specific problem can be done in different ways. But making sure that the program is more readable, maintenance free and easily scalable is also very important. 

The above few solutions or hacks will definitely help in developing better applications. 

Hope you enjoyed reading the post. Comments are welcomed. Thank you!! 

Happy coding!..

Tutorial on Swing Components