Results for java

All About INTERFACE LayoutManager in Java

October 23, 2018

A Container in java is a placeholder that contains different GUI attributes(like Frame,Panel,Label etc).Each container object has its own layout manager to place the child components on the screen.Each container object has a Layout manager which is an instance of any class that implements LayoutManager interface.Thus each component(Frame,Panel,Label etc) will have its default layout manager associated with it which may be changed by developer when an instance of that container is created.Each layout manager keeps track of its child components.The layout manager is notified each time one adds a component to the container.Java supports five different Layout Managers.They are as follows:

  1. FlowLayout
  2. BorderLayout
  3. GridLayout
  4. GridBagLayout
  5. CardLayout.
The interface LayoutManager specifies the methods that all the above layout manager must implement.A LayoutManager is responsible for laying out components of a container.

The interface structure of the LayoutManager is given as:

public interface java.awt.LayoutManager {
//methods
public abstract void addLayoutComponent(String name,Component comp);
public abstract void layoutContainer(Container target);
public abstract Dimension minimumLayoutSize(Container target);
public abstract Dimension preferredLayoutSize(Container target);
public abstract void removeLayoutComponent(Component comp);
} 
All About INTERFACE LayoutManager in Java All About INTERFACE LayoutManager in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

All About Class FlowLayout in AWT in Java

October 23, 2018
The most basic and default layout in java is FlowLayout. The manager implements a simple Layout style which is similar to how words flow in a text editor.Components are laid from upper left corner,left to right and top to bottom.When no more components fit on a line,the next one will appear in the next line.A small space is left between components,above and below.However user can change this gap,alignment etc.The default gap is five pixels and default alignment is center.Below are the different functions for FlowLayout.FlowLayout is typically used to arrange buttons in a panel.
The class structure of class FlowLayout is given as:


public class java.awt.FlowLayout extends java.lang.Object implements java.awt.LayoutManager{
//member elements
public final static int CENTER;
//this indicates that each row of components should be at center.
public final static int LEFT;
//this indicates that each row of components should be at left justified.
public final static int RIGHT;
//this indicates that each row of components should be at right justified.
//constructor
public FlowLayout();
public FlowLayout(int alignment);//alignment can be LEFT,CENTER,RIGHT
public FlowLayout(int alignment,int hgap,int vgap));//alignment can be LEFT,CENTER,RIGHT
//hgap and vgap are the space between two components horizontally and vertically.
//methods
public void addLayoutComponent(String name,Component comp);
public void layoutContainer(Container target);
}
 
public FlowLayout();
Creates a new FlowLayout manager with a centered alignment and a default 5 pixel horizontal and vertical gap.
public FlowLayout(int alignment);
Creates a new FlowLayout manager with a centered alignment and a default 5 pixel horizontal and vertical gap.The alignment argument must be FlowLayout.LEFT,FlowLayout.RIGHT,FlowLayout.CENTER
All About Class FlowLayout in AWT in Java All About Class FlowLayout in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

All About Class GridBagLayout in AWT in Java

October 23, 2018


This is an upgraded version of GridLayout.With this we can specify the width,height of the individual grids in a container.The GridBagLayout manager is a flexible layout manager which aligns components horizontally and vertically,without requiring the component to be of the same size.Each GridBagLayout manager uses a rectangular grid of cells,with each component occupying one or more cells(display area).Each component in a GridBagLayout is associated with a set of constraints contained within a GridBagConstraints,instance contained within a GridBagConstraints,instance that specifies how the component is to be laid out within it's display area.The manner in which the GridBagLayout manager places a set of components depends on each components constraints and it's minimum size as well as the preferred size of the component'c container.To use a GridBagLayout effectively,one or more components must have a customized GridBagConstraints objects created for it.
The class structure of GridBagLayout is given as:

public class java.awt.GridBagLayout extends java.lang.Object implements java.awt.LayoutManager{
//member elements 
protected final static int MAXGRIDSIZE;
//MAXGRIDSIZE is 128 the maximum number of grid position both horizontally and vertically that 
//can be laid out by GridBagLayout .
protected final static int MINSIZE;
//MINSIZE is 1.The smallest grid that can be laid out by GridBagLayout .
//constructor
public GridBagLayout();//creates a GridBagLayout manager
//methods
public void addLayoutComponent(String name,Component comp);
public GridBagConstraints getConstraints(Component comp);
protected GridBagConstraints lookupConstraints(Component comp);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutComponent(Component comp);
public void setConstraints(Component comp,GridBagConstraints constraints)
public String toString();
}
 
All About Class GridBagLayout in AWT in Java All About Class GridBagLayout in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

All About Class GridLayout in AWT in Java

October 23, 2018


GridLayout provides flexibility for placing components.It causes container's component to be laid out in a rectangular grid.The container is split into equal sized rectangles.One component is placed into each rectangle.We can create the manager with a number of rows and columns.Components then fill up the calls defined by the manager.
The class structure of class GridLayout is given below:


public class java.awt.GridLayout extends java.lang.Object implements java.awt.LayoutManager{
//constructor
public GridLayout(int rows,int cols);
public GridLayout(int rows,int cols,int hgap,int vgap);
//methods
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();
}
 
public GridLayout(int rows,int cols);
creates a grid layout with the specified number of rows and columns.All components in the layout are given equal size.One but not both of rows and columns can be zero,which means that any number of objects can be placed in a row or in a column.
public GridLayout(int rows,int cols,int hgap,int vgap);
creates a grid layout with the specified number of rows and columns.All components in the layout are given equal size.In addition the horizontal and vertical gaps are placed at the left and right edge and between each of the columns.The vertical gaps are placed at the top and bottom edge and between each rows.One but not both of rows and columns can be zero,which means that any number of objects can be placed in a row or in a column.

Example

Button b1,b2,b3,b4,b5,b6;
Frame frame=new Frame();
frame.setLayout(new GridLayout(3,2));//3 rows and 2 columns
b1=new Button("B1");
b2=new Button("B2");
b3=new Button("B3");
b4=new Button("B4");
b5=new Button("B5");
b6=new Button("B6");
frame.add(b1);frame.add(b2);frame.add(b3);
frame.add(b4);frame.add(b5);frame.add(b6);
 
All About Class GridLayout in AWT in Java All About Class GridLayout in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

All About Class Canvas in AWT in Java

October 23, 2018


A canvas component represents a blank rectangular area of the screen onto which the application can draw or form which the application can trap input events from the user.An application must sub class the canvas class in order to get useful functionality such as creating a custom components. The paint method must be overridden in order to perform custom graphics on the canvas.The mouse,keyboard and focus events that may occur over canvas can be understood or sensed.The below methods may be overridden in order to catch user events-

  • gotFocus()
  • lostFocus()
  • keyDown()
  • keyUp()
  • mouseEnter()
  • mouseExit()
  • mouseMove()
  • mouseDrag()
  • mouseDown()
  • mouseUp()
The structure of the Class Canvas is given as:

public class java.awt.Canvas extends java.awt.Component{
//constructor
public Canvas();
//Methods
public void addNotify();
public void paint(Graphics g);
}
 
All About Class Canvas in AWT in Java All About Class Canvas in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

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.