0% found this document useful (0 votes)
47 views

Adv Java Lec-9 Event Hierarchy, CardLayout.c018d12

The document discusses the CardLayout in Java. CardLayout allows only one component to be visible at a time, treating each like a card that can be flipped between. It describes the CardLayout class, its constructors and methods to change the visible component, and provides an example program to demonstrate CardLayout functionality.

Uploaded by

Neha Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Adv Java Lec-9 Event Hierarchy, CardLayout.c018d12

The document discusses the CardLayout in Java. CardLayout allows only one component to be visible at a time, treating each like a card that can be flipped between. It describes the CardLayout class, its constructors and methods to change the visible component, and provides an example program to demonstrate CardLayout functionality.

Uploaded by

Neha Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Adv. Java Lecture-9


Topics: Event Hierarchy , CardLayout
Event Classes Hierarchy:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Card Layout :
 There are many different layout managers in Java. Some of and most
common layout managers are.
 BorderLayout
 FlowLayout
 GridLayout
 NullLayout
 GridbagLayout
 BoxLayout
 CardLayout

 We have already seen some of them. In java it is also possible to create


your own Custom Layout.

 Every Layout managers decides the size and location of Component


when we add it on container.

 In this lecture we are going to study CardLayout.

CardLayout :
 The CardLayout class manages the components in such a way that only
one component is visible at a time.

 It treats each component as a card in the container. Only one card is


visible at a time, and the container acts as a stack of cards.

 The first component added to a CardLayout object is the visible


component when the container is first displayed.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Constructors of CardLayout
1) CardLayout ()
This constructor creates a CardLayout using a horizontal and vertical gap
of zero pixels.

2) CardLayout (int hgap, int vgap)


This version of the constructor allows you to create a CardLayout with a
horizontal gap of hgap and vertical gap of vgap to add some space
around the outside of the component that is displayed. The units for
gaps are pixels.

Methods of CardLayout
1) void first (Container parent)
The first() method flips to the initial component in parent.

2) void next (Container parent)


The next() method flips to the following component in parent, wrapping
back to the beginning if the current component is the last.

3) void previous (Container parent)


The previous() method flips to the prior component in parent, wrapping
to the end if the current component is the first.

4) void last (Container parent)


The last() method flips to the final component in parent.

5) void show (Container parent, String name)


The show() method displays the component in parent that was assigned
the given name when it was added to the container. If there is no
component with name contained within parent, nothing happens.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Following Program demonstrate CardLayout:


import java.awt.*;
import java.awt.event.*;
class CardLayoutTest extends Frame implements ActionListener
{
Button n = new Button("Next");
Button p = new Button("Previous");
Button f = new Button("First");
Button l = new Button("Last");

Panel center = new Panel();


Panel south = new Panel();

CardLayout cl = new CardLayout(20,20);

CardLayoutTest()
{
setSize(600,500);
setTitle("CardLayout");
setBackground(Color.LIGHT_GRAY); //Frame Background

south.add(f);
south.add(p);
south.add(n);
south.add(l);
south.setBackground(Color.CYAN); //South Panel Background

n.addActionListener(this);
p.addActionListener(this);
f.addActionListener(this);
l.addActionListener(this);
add(south,"South");

Panel pA = new Panel(); //Creating Panels to add on CardLayout


pA.setBackground(Color.red);
pA.add(new Button("Info Planet"));

Panel pB = new Panel();


pB.setBackground(Color.blue);

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Panel pC = new Panel ();


pC.setBackground(Color.green);

Panel pD = new Panel ();


pD.setBackground(Color.pink);

center.setLayout (cl); //Center Panel Uses CardLayout

center.add ("One", pA);


center.add ("Two", pB);
center.add ("Three", pC);
center.add("Four",pD);

add(center); //adding center panel on Frame


}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("First"))
cl.first(center);
else
if(s.equals("Next"))
cl.next(center);
else
if(s.equals("Last"))
cl.last(center);
else
cl.previous(center);
}
public static void main(String args[])
{
CardLayoutTest f = new CardLayoutTest();
f.setVisible(true);
}
}

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Output:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like