Skip to main content

Posts

Swing JLabel demo with example

This article is continuation of previous post JFrame usage demo . Please read it before continuing. This post uses the same example code shown in  JFrame usage demo  with JLabel changes and introduces layout manager BorderLayout. Layout Manager: Layout manager is responsible to arrange components on containers. Usually every swing container is created with a default layout manager but can be changed with setLayout() api. For example JFrame uses BorderLayout as default layout amanger. Java program for JLabel 1  2   import java.awt.BorderLayout; 3   import java.awt.Color; 4   import java.awt.Font; 5  6   import javax.swing.JFrame; 7   import javax.swing.JLabel; 8   import javax.swing.SwingUtilities; 9  10  /** 11   * 12   * Simple class to demonstrate JLabel of swing toolkit 13   * 14   * @author Nagasharath 15   * 16  ...

Swing: JFrame usage demo

This post helps in understanding usage of JFrame class with an example Swing toolkit categorizes components into 2 types. They are: Containers Controls Containers allows Controls to be arranged on them. JPanel, JFrame and JDialog are few which are frequently used. Controls are components like Buttons, labels, tables etc. These controls are arranged on containers using different layout managers. All Swing class names starts with J stands for J ava which symbolizes swing is pure java!   Layout Managers: Layout manager is responsible for laying out controls on containers according to the business requirement. Example layout managers are BorderLayout, GridLayout, GridBagLayout and FlowLayout. Few points about JFrame: JFrame is considered as a main window of the application.for almost all swing based applications All other controls and containers are created as child components of JFrame JFrama holds special components like  Menu bar, Tool bar etc...

Java Multi threaded programming basics with Reentrant lock

Java Multi threaded programming basics with Reentrant locks As we have seen in earlier post that implicit locking mechanism achieved using synchronized keyword slows down the application performance. Hence java concurrency api gives explicit locking mechanism to achieve mutual exclusion among shared object. How using explicit locks are better than implicit lock? Explicit lock acquires lock per Thread basis and not per method invoke basis. It is inverse in the case of implicit lock.   If a method is synchronized then every invocation of that method involves acquiring and releasing of lock. This process really slows down the application performance.  Hence it is always good idea to prefer reentrent lock or explicit lock to implicit lock. ReentrantLock is a class available in java.util.concurrency package lock() and unlock() methods are used used to acquire and release the lock. Condition class is used in place of Object class wait(), notifyAll() and notify(). ...

Java desktop GUI.

Graphical user interface in short GUI makes computer users easy to understand and use the application software comfortably. Almost all modern programming languages allows application developers to develop GUIs. Java has AWT/Swing as default API as part of Java Foundation classes in short JFC which are shipped with JDK. Hence these toolkits can be used directly in our applications with out adding external libraries like jars to our application's class path. But there are some other toolkits which found useful and industry endorsed in developing GUI.      SWT       JFaces [Framework for SWT]      Java FX      Swing      AWT AWT: Abstract window toolkit is available since java first version. AWT uses native operating system resources like Fonts, colors and components like table, tree, dialogues, windows etc. Few notable points about AWT: AWT is heavy weight. It uses OS resources There are l...

Java Multi threaded programming basics

All modern computer hardware architectures and operating systems are capable and optimized for multi processor system and multi threaded system.  Process Vrs Thread: Process is heavy weight where as Thread is light weight. Thread is light weight because process uses it's own address space and thread shares the Process's address space. So Threads are considered to be subset of a process. more over threads consumes less OS resources in terms of physical memory and uses CPU time at most. Junior programmers think concurrency is hard.  Experienced programmers think concurrency is easy.  Senior programmers think concurrency is hard.                                                                                    _ Java Concurrency Essentials by Martin ...

Every Java programmer must read books

Every Java programmer must read and understand the following books. 1. Effective Java 2. Concurrency in practice Effective Java: This book is authored by Joshua Bloch. He took the major role in authoring the java.util package which is [collection framework] one of the java core libraries. He explains the best practices that every programmer should consider while writing java programs. Some of them are: Contract between hashcode() and equals() of Object class when programmer overrides one of these methods in their classes Exceptions concurrency: Inter thread communication, synchronization etc Immutable Objects The above points are few among others. Concurrency in practice: This book gives complete explanation about how efficient concurrency is achieved in java programs using concurrency utility classes which were introduced in Java 5 version. This book is authored by: Brian Goetz Tim peierls Joshua Bloch Joseph Bowbeer David Holmes and Doug Lea ...