Featured Posts

[Travel][feat1]

All About Checkbox in AWT in Java

October 22, 2018
A checkbox is a graphical component provides an 'ON'(true) and 'OFF'(false) state. Clicking on the checkbox changes its state from ON to OFF or OFF to ON.When the checkbox is clicked,AWT send an action event to the checkbox.This event's target is the checkbox and its object is a boolean,giving the new state to the checkbox.An application should override the action method of the checkbox or one of it's containing window in order to cause some action to occur.
Optionally several checkboxes can be grouped together into a check box group.At most one button in a group can be in the 'ON' state at any given point of time.Pushing a checkbox to turn it 'ON' forces any other checkbox in the group(that is 'ON') to 'OFF' state.

The class structure of the Checkbox is given as:


public class Checkbox extends java.awt.Component{
//constructors
public Checkbox();//default constructor with no label.The checkbox is set to off (by default) and 
//is not part of any checkbox group
public Checkbox(String value);//creates a check box with label as the value given in the parameter
public Checkbox(String value,fencingType,boolean initialState);
public CheckBox(String label,ChechboxGroup group,boolean state);
//creates a checkbox with the specified label in the specified checkboxGroup and set to the specified state.
//Methods
public void addNotify();
public String getLabel();
public boolean getState();
protected String paramString();
public CheckBoxGroup getCheckboxgroup();
public void setCheckboxGroup(CheckboxGroup g);
public void setLabel(String label);
public void setState(boolean state);
An Example:

Checkbox c3=new Checkbox("value",null,true);
All About Checkbox in AWT in Java All About Checkbox in AWT in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5

All About CardLayout in Java

October 22, 2018


This is unique from other layout managers in the sense that it represents several different layouts as living on separate index cards that can be shuffled so that any one card is on top at a given point of time. That means only one card is visible at a time,allowing applications to flip through the cards.This is useful for user interface that have optional components which can be dynamically enabled or disabled upon user input.
The structure of the class CardLayout is given as:

public class java.awt.CardLayout extends java.lang.Object implements java.awt.LayoutManager{
//constructors
public CardLayout();//default constructor ,creates a new card layout
public CardLayout(int hgap,int vgap);
//creates a new card layout with the specified horizontal and vertical gaps. The specified 
//space between the components(gaps)The horizontal gaps are placed at the left and right edge.
//The vertical gaps are placed at the top and bottom edge.
//methods:
public void addLayoutComponent(String name,Component comp);
public void first(Container target);
public void last(Container target);
public void next(Container target);
public void previous(Container target);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public void show(Container target,String name);
public String toString();
}
 
An Example

Frame frame=new Frame();
CardLayout layout=new CardLayout();
frame.setLayout(layout);
frame.add("1",new Button("Card1"));
frame.add("2",new Button("Card2"));
frame.add("3",new Button("Card3"));
frame.add("4",new Button("Card4"));
public boolean keyDown(Event e,int key){
layout next(this);
return true;
}
 
add() method needs labels to pass a card name.We can refer the cards by card name. Here layout object is created first and then refer the same to frame.This is because we need to refer the CardLayout object after we have created it.
All About CardLayout in Java All About CardLayout in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5

All About Class Button in AWT in Java

October 22, 2018


This class Button is responsible for creating a labeled or unlabeled button in the GUI.The structure of the class Button is given as :

public class Button extends java.awt.Component{
//constructors
public Button();//creates an button without a label in it
public Button(String Label);//creates a button with the label -Label
//methods:
public void setLabel(String label);//sets a label for the button.
public void addNotify();
public String getLabel();
protected String paramString();
}
 
All About Class Button in AWT in Java All About Class Button in AWT in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5

All About Class BorderLayout in Java

October 22, 2018


BorderLayout manager implements a common layout style which has five zones.Each of these zones is named with a String like North,South,East,West and Center.A BorderLayout lays outs a container to contain number of GUI elements like buttons,labels,grids,texts etc.

The structure of the class BorderLayout is given as:



public class java.awt.BorderLayout extends java.lang.Objects implements java.awt.LayoutManager{
//constructors
public BorderLayout();//default constructor ,constructs a new border layout.
public BorderLayout(int hgap,int vgap);
//creates a new border layout with the specified horizontal and vertical gaps. The specified 
//space between the components(gaps)
//method
public void addLayoutComponent(String name,Component comp);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public String toString();
}
 
Example

Frame frame=new Frame();
frame.setLayout(new BorderLayout(20,40));
Button buttonN,buttonS,buttonE,buttonW,buttonC;
buttonN=new Button("North");
buttonS=new Button("South");
buttonW=new Button("West");
buttonE=new Button("East");
buttonC=new Button("Center");
frame.add("North",buttonN);
frame.add("South",buttonS);
frame.add("West",buttonW);
frame.add("East",buttonE);
frame.add("Center",buttonC);
}
 
BorderLayout relies on position string,being case sensitive passing "NORTH" or "north" instead of "North" will not work.
All About Class BorderLayout in Java All About Class BorderLayout in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5

Switch Case Simplified in Java

October 22, 2018

Switch in java is a conditional statement that activates only if a matching condition found out of the given input.We can use if else or if ladder instead of switch.But If else can be optimized using switch statements.Switch removes the nested if else and keep the code more user friendly,crisp and clean.When compared to high number of iterations the switch is faster as compared to if else statements.Switch is classified as selection statement.
Selector case is an expression the produces some value. the switch compares the result of selector case with selector case value.If a valid match is found the corresponding statement(s) executes.If no match occurs the default gets executed.In switch each case ends with a break which causes execution to jump to the end of select body but break statement is optional.If it is missing,the code for the following case statements executes until next break or end of select case.It is used to multi way selection  implementation.Even though characters can be used in switch,but actually down the line,it produces the integral value of a character.

the switch statement take the value of a variable or expression against a list of case values and when a match is found,a block of statements associated with case is executed.

Generic Syntax:

switch(selectCase)
{
case selectCase1 :statement;break;
case selectCase2 :statement;break;
case selectCase3 :statement;break;
...
...
...
default: statement;

}
 


An Example:

class MySwitchBoard{
public static void main(String args[])
{
int iSwitch=4;
switch(iSwitch)
{
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
case 4:
System.out.println("four");
break;
default:
System.out.println("default");
}
}
}
 


Another Example:

switch(e)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("vowel");
break;
case 'y':
case 'w':
System.out.println("some times vowel");
break;
default:
System.out.println("Consonent");
}
 
Importance of break:
break statement will break the switch statement once the condition is satisfied.So break will come out of the switch block. The control will not flow to the next case statement.It also signals the end of particular case and causes an exit from switch statement,referring the control out of the switch of the next statement.
Importance of default:
Default is an optional case,when present it will be executed if the value of the expression does not match with any of the case values.If not present,no action takes place when all matches fail and the control goes out of the switch statements.This will be executed if no condition matches with cases.So the remaining steps condition will be checked and finally execute the default statement.
There is no position for default.It can be placed anywhere in the switch block.Bast practice is to put to the end of the switch block.

Rules for switch:

  1. There is no limit for switch case
  2. switch takes input in the form of integer ,characters and String(java -8 onward).
  3. for non integral,non character or non string cases,we can not use switch rather a series of if statements are required.
  4. The select case values are constant expression known as labels
  5. The select cases are unique within a switch statement.
  6. Cases may contain zero or n lines
  7. There is no need to put braces for block of statements.
  8. Case labels ends with a colon(:).

cases can be stacked on top of each other to provide multiple matches for a code.It is essential to put a break otherwise the control will go to the unwanted block of cases and execute.Read here for switch case in enum.

Switch Case Simplified in Java Switch Case Simplified in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5
Powered by Blogger.