Advertisement



< Prev
Next >



CardLayout Manager




CardLayout manager is used to position the components in a manner of deck of cards, with only the card at the top of deck is visible at a time. All the components of CardLayout Manager are of the type - JPanel and they are contained in a main parent JPanel container. The layout manager of this main parent JPanel is set to CardLayout manager.




Constructors of CardLayout


Constructor Description
public CardLayout() Creates a CardLayout with gap of size zero between the components..
public CardLayout(int hGap, int vGap) Creates a CardLayout with a specified number of gap between the components.





CardLayout methods


Methods Description
public void first(Component comp) This method flips to the first card in the container.
public void next(Component comp) This method flips to the next card in the container.
public void last(Component comp) This method flips to the last card in the container.
public void show(Component comp,String name) This method shows a specific card in the container with a specific name.
public void setHgap(int horizontalGap) This method sets the horizontal space between components.
public void setVgap(int verticalGap) This method sets the vertical space between components.

LayoutManager is an interface which is implemented by CardLayout manager class.


Advertisement




An example display the use of CardLayout Manager.



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

public class CardLayoutEx 
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});	

}//Closing the main method
}//Closing the class A


class A implements ActionListener
{
JFrame jf;
JPanel parentPanel;
CardLayout cd;


A()
{
jf = new JFrame("CardLayout Example");
cd = new CardLayout();


//Creating a main parent panel that will contain two child panels
parentPanel = new JPanel();

//Creating two child panels.
JPanel childPanel1 = new JPanel();
JPanel childPanel2 = new JPanel();


JButton button1 = new JButton("Alphabets");
JButton button2 = new JButton("Numbers");

JButton button3 = new JButton("4");
JButton button4 = new JButton("5");
JButton button5 = new JButton("6");

//Adding buttons to childPanel1
childPanel1.add(button3);
childPanel1.add(button4);
childPanel1.add(button5);

JButton button6 = new JButton("A");
JButton button7 = new JButton("B");
JButton button8 = new JButton("C");

//Adding buttons to childPanel2
childPanel2.add(button6);
childPanel2.add(button7);
childPanel2.add(button8);


//Setting the positioning of the components in parentPanel, JPanel(that contains childPanel1 and childPanel2) to CardLayout
parentPanel.setLayout(cd);

//Adding childPanel1 and childPanel2 to parentPanel
parentPanel.add(childPanel1, "Num");
parentPanel.add(childPanel2, "Alp");

button1.addActionListener(this);
button2.addActionListener(this);


//Setting container JFrame's layout to FlowLayout.
jf.setLayout(new FlowLayout());

//Adding two buttons to JFrame, these buttons will remain commonly visible to all panels 
jf.add(button1); 	
jf.add(button2);

//Adding JPanel, parentPanel to JFrame
jf.add(parentPanel);

jf.setSize(300,200);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
//If "Numbers" button is clicked, open the JPanel with buttons showing numbers.
if(ae.getActionCommand()=="Numbers") 
cd.show(parentPanel,"Num");

//If "Alphabets" button is clicked, open the JPanel with buttons showing alphabets.
if(ae.getActionCommand()=="Alphabets") 
cd.show(parentPanel,"Alp");
}
}


When you run the code, you are presented a window showing the main frame, which contains a parent Panel which further contains two child panels, which looks like Figure1.

Figure 1.


When you click on the button Alphabets you are presented a JPanel with buttons showing you Alphabets, like Figure2.

Figure 2


When you click on the button Numbers you are presented a JPanel with buttons showing you numbers, like Figure3.

Figure 3





Please share this article -





< Prev
Next >
< GridLayout Manager
GridBagLayout Manager >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement