Results 1 to 5 of 5

Thread: How to use CardLayout in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    114

    How to use CardLayout in Java?

    Hi everyone,
    Last time you all guys did a very helpful work for me by providing an exact solution for my problem. So thought that posting here doesn't go waste.!! Now after using the layout manager in Java, I want to use the Card Layout in Java. But I don't know how to use it.!! So please help me now also by telling how to use CardLayout in Java.?? Hoping that you would drag me out of this problem again.!!
    |===================|
    |YAY if that made sense...|
    |===================|

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use CardLayout in Java?

    A CardLayout object is a layout manager for a container. As its name suggests it treats each component in the container as a card. Exactly like the cards, in the pack only one card is visible at a time, similarly container acts as a stack of cards. When the container is first displayed, the first component added to a CardLayout object is the visible component. If you want to associate a string identifier with a given card for fast random access then you can use the addLayoutComponent(java.awt.Component, java.lang.Object) method.

  3. #3
    Join Date
    Mar 2008
    Posts
    672

    Re: How to use CardLayout in Java?

    You can use the following constructors for the CardLayout :
    Code:
    public CardLayout()
    After using the above constructor, you will able to create a new card layout with gaps of size zero. If you want to create a new card layout with the specified horizontal and vertical gaps, you can look at the following example :
    Code:
    public CardLayout(int hgap,
                      int vgap)
    The horizontal gaps are placed at the left and right edges. The vertical gaps are placed at the top and bottom edges.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use CardLayout in Java?

    Maybe after looking at the code you will find it easy to do. So i am providing you with the coding that creates the CardLayout and the components it manages. The following sample of code demonstrates the same :
    Code:
    Panel cards;
    final static String BUTTONPANEL = "Panel having Buttons";
    final static String TEXTPANEL = "Panel having TextField";
    
    cardslay = new Panel();
    cardslay.setLayout(new CardLayout());
    
    cardslay.add(BUTTONPANEL, p1);
    cardslay.add(TEXTPANEL, p2);

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to use CardLayout in Java?

    I have provided you with the coding which you can easily understand. The following code from CardLayoutTrial.java creates the CardLayout and the components it manages :
    Code:
    JPanel cardlay;
    
    final static String BUTTONPANEL = "JPanel with JButtons";
    
    final static String TEXTPANEL = "JPanel with JTextField";
    
    JPanel card1 = new JPanel();
    
    ...
    
    JPanel card2 = new JPanel();
    
    ...
    
    cardlay = new JPanel(new CardLayout());
    
    cardlay.add(card1, BUTTONPANEL);
    
    cardlay.add(card2, TEXTPANEL);

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Setting Of Java to The Point At Manual Java
    By winni in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 10:05 PM
  3. Java Programming using Adventnet SNMP Java API
    By ROCKING_Suhas in forum Software Development
    Replies: 5
    Last Post: 17-07-2010, 06:52 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,858,116.93630 seconds with 16 queries