Class CardLayout in Java

0
223
Spread the love
  • 24
  • 34
  • 14
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    119
    Shares

Class CardLayout in Java

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 anyone’s card is on top at a given point of time.

The CardLayout LayoutManager provides the means to manage multiple components, displaying one at a time. Components are displayed in the order in which they are added to the layout, or in an arbitrary order by using an assignable name.

That means only one card is visible at a time, allowing applications to flip through the cards. This is useful for the user interface that has optional components that 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.LayoutManager2,java.io.Serializable{
//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 (Component comp,Object constraints);
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 int getHgap();
public int getVgap();
public abstract void invalidateLayout(Container target);
public abstract float getLayoutAlignmentX(Container target);
public abstract float getLayoutAlignmentY(Container target);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public abstract Dimension maximumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public void setHgap (int hgap);
public void setVgap (int vgap);
public void show(Container target,String name);
public String toString();
} 

The details of the class structure are given as follows:

public CardLayout();

public CardLayout() constructs a CardLayout object.

public CardLayout (int hgap, int vgap);

public CardLayout (int hgap, int vgap) Constructs a CardLayout object with the values specified as the gaps around the container managed by this instance of CardLayout.

Parameter
hgap – Horizontal space around left and right of container
vgap – Vertical space around top and bottom of container

public void addLayoutComponent (Component comp,Object constraints);

public void addLayoutComponent (Component comp,Object constraints) method Adds the component comp to a container subject to the given constraints. This is a more generalized version of addLayoutComponent(String, Component). It corresponds to java.awt.Container’s add(Component, Object). In practice, it is used the same in Java 1.1 as in Java 1.0.2, except with the parameters swapped:

Panel p = new Panel();
p.setLayoutManager(new CardLayout());
p.add(new Button("OK"), "My Test");

Parameter
comp – The component being added.
constraints – An object describing the constraints on this component.

public void addLayoutComponent (String name, Component component);

public void addLayoutComponent (String name, Component component) method Places component under the layout’s management, assigning it the given name. This has been replaced in version 1.1 with the more general addLayoutComponent(Component,Object).

Parameter
name – Name of the component to add.
component – The actual component being added.

public void first (Container parent);

public void first (Container parent) method sets the container to display the first component in parent.

Parameter
parent – The container whose displayed component is changing.

public int getHgap();

public int getHgap() method returns the horizontal gap for this CardLayout instance.

This method returns the horizontal gap for this CardLayout instance.

public abstract float getLayoutAlignmentX(Container target);

public abstract float getLayoutAlignmentX(Container target) method returns the preferred alignment of the given container target. A return value of 0 is left aligned, .5 is centered, and 1 is right aligned.
This method returns the value .5 for all containers.
Parameter
target – The container to inspect.

public abstract float getLayoutAlignmentY(Container target);

public abstract float getLayoutAlignmentY(Container target) method returns the preferred alignment of the given container target. A return value of 0 is top aligned, .5 is centered, and 1 is bottom aligned.

This method returns the value .5 for all containers.

Parameter
target – The container to inspect.

public int getVgap();

public int getVgap() method returns the vertical gap for this CardLayout instance.

This method returns the vertical gap for this CardLayout instance.

public abstract void invalidateLayout(Container target);

public abstract void invalidateLayout(Container target) method actually does nothing.

Parameter
target – The container to invalidate.

public void last(Container parent);

public void last (Container parent) method sets the container to display the final component in parent.

Parameter
parent – The container whose displayed component is changing.

public void layoutContainer(Container target);

public void layoutContainer (Container target) method displays the currently selected component contained within target.

Parameter
target – The container that needs to be redrawn.

public abstract Dimension maximumLayoutSize(Container target);

public abstract Dimension maximumLayoutSize(Container target) method returns a maximal Dimension for CardLayout.

This method returns a Dimension whose horizontal and vertical components are Integer.MAX_VALUE.

Parameter
target – The container to inspect.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method calculates minimum size of the target container.

This method returns the minimum Dimension of the container target.

Parameter
target – The container whose size needs to be calculated.

public void next (Container parent);

public void next (Container parent) method sets the container to display the following component in the parent.

Parameter
parent – The container whose displayed component is changing.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates preferred size of the target container.

This method returns the preferred Dimensions of the container target.

Parameter
target – The container whose size needs to be calculated.

public void previous (Container parent);

public void previous (Container parent) method sets the container to display the prior component in parent.

Parameter
parent – The container whose displayed component is changing.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method removes component from the layout manager’s internal tables.

Parameter
component – Component to stop tracking.

public void setHgap (int hgap);

public void setHgap (int hgap) method sets the horizontal gap for the left and right of the container.

Parameter
hgap – The horizontal gap value.

public void setVgap (int vgap);

public void setVgap (int vgap) method sets the vertical gap for the top and bottom of the container.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 14
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    119
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here