Posts

Showing posts from February, 2022

Swing and JButton

Importance of Swing  Swing was created to address the limitations present in the AWT. It does this through two key features: lightweight components and a pluggable look and feel. Together they provide an elegant, yet easy-to-use solution to the problems of the AWT. More than anything else, it is these two features that define the essence of Swing. Swing Components are LightWeight With very few exceptions, Swing components are lightweight. This means that they are written entirely in Java and do not map directly to platform-specific peers. Thus, lightweight components are more efficient and more flexible. Furthermore, because lightweight components do not translate into native peers, the look and feel of each component is determined by Swing, not by the underlying operating system. As a result, each component will work in a consistent manner across all platforms. Swing Supports a Pluggable Look and Feel Swing supports a pluggable look and feel (PLAF). Because each Swing component is ren

The KeyListener Interface

The KeyListener Interface This interface defines three methods. The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively. The keyTyped( ) method is invoked when a character has been entered. For example, if a user presses and releases the A key, three events are generated in sequence: key pressed, typed, and released. If a user presses and releases the HOME key, two key events are generated in sequence: key pressed and released.  The general forms of these methods are shown here:  void keyPressed(KeyEvent ke)  void keyReleased(KeyEvent ke)  void keyTyped(KeyEvent ke)  

LinkedHashSet in JAVA

  The LinkedHashSet Class The LinkedHashSet class extends HashSet and adds no members of its own. It is a generic class that has this declaration:   class LinkedHashSet Here, E specifies the type of objects that the set will hold. Its constructors parallel those in HashSet.   LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set. That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted. This is also the order in which they are contained in the string returned by toString( ) when called on a LinkedHashSet object. Example(Source Code): import java.util.*; class Main{     public static void main(String[] args){         LinkedHashSet<String> hs = new LinkedHashSet<String>();         hs.add("Beta");         hs.add("Alpha");         hs.add("Eta");         hs.add("Ga

Threads In JAVA

 Creating a Thread In the most general sense, you create a thread by instantiating an object of type Thread. Java defines two ways in which this can be accomplished:  • You can implement the Runnable interface.  • You can extend the Thread class, itself.  Implementing Runnable  The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run( ), which is declared like this:  public void run( )   Inside run( ), you will define the code that constitutes the new thread. It is important to understand that run( ) can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that run( ) establishes the entry point for another, concurrent thread of execution within your program. This thread will end when run( ) return