Wednesday, 2 April 2014

KeyListener

The listener interface for receiving keyboard event.



public interface KeyListener extends EventListener {
    public void keyTyped(KeyEvent e);
    public void keyPressed(KeyEvent e);
    public void keyReleased(KeyEvent e);
}

void keyTyped(KeyEvent e)
Invoked when a key has been typed. See the class description for KeyEvent for a definition of a key typed event.

void keyPressed(KeyEvent e)
Invoked when a key has been pressed. See the class description for KeyEvent for a definition of a key pressed event.

void keyReleased(KeyEvent e)
Invoked when a key has been released. See the class description for KeyEvent for a definition of a key released event.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

public class KeyListenerEx1{
 JTextArea displayArea;
 JTextField typingArea;
 JFrame frame1;
  
 KeyListenerEx1(){
  
  /* Initialize typingArea */
  typingArea = new JTextField(20);
  
  /* Initialize Display Area */
  displayArea = new JTextArea();
  displayArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(displayArea);
  scrollPane.setPreferredSize(new Dimension(375, 125));
   
  /* Initialize Frame */
  frame1 =new JFrame("Sample Frame");
  frame1.pack();
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.getContentPane().add(typingArea, BorderLayout.PAGE_START);
  frame1.getContentPane().add(scrollPane, BorderLayout.CENTER);
  frame1.setSize(300, 500);
    
  /* Add Key Listener */
  typingArea.addKeyListener(new KeyListener(){
   public void keyTyped(KeyEvent e){
    displayArea.append("Key Typed\n" + e +"\n");
   }
    
   public void keyPressed(KeyEvent e){
    displayArea.append("Key Pressed\n " + e +"\n");
   }
    
   public void keyReleased(KeyEvent e){
    displayArea.append("Key Released\n" + e +"\n");
   }
  });   
 }
 
 public static void main(String args[]){
  new KeyListenerEx1();
 }
}

Output




 
Prevoius                                                 Next                                                 Home

Tuesday, 1 April 2014

ItemListener

The listener interface for receiving item events.


public interface ItemListener extends EventListener {
    void itemStateChanged(ItemEvent e);
}

void itemStateChanged(ItemEvent e)
Invoked when an item has been selected or deselected by the user. The code written for this method performs the operations that need to occur when an item is selected (or deselected).

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ItemListenerEx {

    public ItemListenerEx(){

        JComboBox box = new JComboBox();
        box.addItem("One");
        box.addItem("Two");
        box.addItem("Three");
        box.addItem("Four");

        box.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){
                System.out.println(e.getItem() + " " + e.getStateChange() );
            }
        });

        JFrame frame = new JFrame();
        frame.getContentPane().add(box);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String [] args) {
        ItemListenerEx obj1 = new ItemListenerEx();
    }
}

Select Some Item on Combo Box and observe the Command Prompt
Output





Prevoius                                                 Next                                                 Home

InputMethodListener

The listener interface for receiving input method events.


public interface InputMethodListener extends EventListener {
   void inputMethodTextChanged(InputMethodEvent event);
   void caretPositionChanged(InputMethodEvent event);
}

void inputMethodTextChanged(InputMethodEvent event)
Invoked when the text entered through an input method has changed.

void caretPositionChanged(InputMethodEvent event)
Invoked when the caret within composed text has changed.


Prevoius                                                 Next                                                 Home

get : Get ArrayList Item by using Index

E get(int index)
    Returns the element at the specified position in this list.

import java.util.*;

class ListGetByIndex{
 public static void main(String args[]){
  List<Integer> myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(13);
  myList.add(100);
  myList.add(95);
  myList.add(123);
  
  System.out.println("Elements in the List are");
  System.out.println(myList+"\n");
  
  for(int i = 0; i < myList.size(); i++){
   System.out.println("Index " + i + " has " + myList.get(i));
  } 
 }
}

Output
Elements in the List are
[10, 13, 100, 95, 123]

Index 0 has 10
Index 1 has 13
Index 2 has 100
Index 3 has 95
Index 4 has 123

1. throws IndexOutOfBoundsException if the index is out of range

import java.util.*;

class ListGetIndexOut{
 public static void main(String args[]){
  List<Integer> myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(13);
  myList.add(100);
  myList.add(95);
  myList.add(123);
  
  System.out.println("Elements in the List are");
  System.out.println(myList+"\n");
  
  /* Trying to access the element which is out of index */
  myList.get(5);
 }
}

Output
Elements in the List are
[10, 13, 100, 95, 123]

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size:5
        at java.util.LinkedList.checkElementIndex(Unknown Source)
        at java.util.LinkedList.get(Unknown Source)
        at ListGetIndexOut.main(ListGetIndexOut.java:18)





Prevoius                                                 Next                                                 Home

hashCode of the List

int hashCode()
   Returns the hash code value for this list. If two lists are equal, then their hash codes also equal.

import java.util.*;

class ListHashCode{
 public static void main(String args[]){
  List<Integer> myList1 = new ArrayList<> ();
  List<Integer> myList2 = new LinkedList<> ();
  List<Integer> myList3 = new LinkedList<> ();
  
  /* Add Elements to myList1 and myList2*/
  for(int i=0; i<10; i++){
   myList1.add(i);
   myList2.add(i);
  }
  
  /* Add Elements to myList3 */
  for(int i=9; i>-1; i--){
   myList3.add(i);
  }
  
  /*Display Elements in the lists */
  System.out.println("Elements in myList1 are \n" + myList1);
  System.out.println("\nElements in myList2 are \n" + myList2);
  System.out.println("\nElements in myList3 are \n" + myList3);
  
  System.out.println("\nIs myList1 and myList2 are Equal " + myList1.equals(myList2));
  System.out.println("myList1 hashCode is " + myList1.hashCode());
  System.out.println("myList2 hashCode is " + myList2.hashCode());
  
  System.out.println("\nIs myList1 and myList3 are Equal " + myList1.equals(myList3));
  System.out.println("myList1 hashCode is " + myList1.hashCode());
  System.out.println("myList3 hashCode is " + myList3.hashCode());
  
  System.out.println("\nIs myList2 and myList3 are Equal " + myList2.equals(myList3));
  System.out.println("myList2 hashCode is " + myList2.hashCode());
  System.out.println("myList3 hashCode is " + myList3.hashCode());
 }
}

Output
Elements in myList1 are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in myList2 are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in myList3 are
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Is myList1 and myList2 are Equal true
myList1 hashCode is -1631921466
myList2 hashCode is -1631921466

Is myList1 and myList3 are Equal false
myList1 hashCode is -1631921466
myList3 hashCode is -353583012

Is myList2 and myList3 are Equal false
myList2 hashCode is -1631921466
myList3 hashCode is -353583012








Prevoius                                                 Next                                                 Home

equals : Compare two lists

boolean equals(Object o)
   Two lists are defined to be equal if they contain the same elements in the same order.

import java.util.*;

class ListEquals{
 public static void main(String args[]){
  List<Integer> myList1 = new ArrayList<> ();
  List<Integer> myList2 = new LinkedList<> ();
  List<Integer> myList3 = new LinkedList<> ();
  
  /* Add Elements to myList1 and myList2*/
  for(int i=0; i<10; i++){
   myList1.add(i);
   myList2.add(i);
  }
  
  /* Add Elements to myList3 */
  for(int i=9; i>-1; i--){
   myList3.add(i);
  }
  
  /*Display Elements in the lists */
  System.out.println("Elements in myList1 are \n" + myList1);
  System.out.println("\nElements in myList2 are \n" + myList2);
  System.out.println("\nElements in myList3 are \n" + myList3);
  
  System.out.println("\nIs myList1 and myList2 are Equal " + myList1.equals(myList2));
  System.out.println("Is myList1 and myList3 are Equal " + myList1.equals(myList3));
  System.out.println("Is myList2 and myList3 are Equal " + myList2.equals(myList3));
 }
}

Output
Elements in myList1 are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in myList2 are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in myList3 are
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Is myList1 and myList2 are Equal true
Is myList1 and myList3 are Equal false
Is myList2 and myList3 are Equal false



Prevoius                                                 Next                                                 Home

Clear() : Removes all of the elements from this list

clear()
    Removes all of the elements from this list

import java.util.*;
class ListClear{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();

  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }

  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  
  myList.clear();

  System.out.println("Elements in myList After calling clear");
  System.out.println(myList +"\n");
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in myList After calling clear
[]

1. throws UnsupportedOperationException if the clear operation is not supported by this list.
import java.util.*;
class ListClearUnsupported{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }

  unmodifiable = Collections.unmodifiableList(myList);
  
  System.out.println("Elements in unmodifiable List are ");
  System.out.println(unmodifiable +"\n");
  
  unmodifiable.clear();

  System.out.println("Elements in unmodifiable List After calling clear");
  System.out.println(unmodifiable +"\n");
 }
}

Output
Elements in unmodifiable List are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.clear(Unknown Source)
        at ListClearUnsupported.main(ListClearUnsupported.java:17)



Prevoius                                                 Next                                                 Home