Results 1 to 5 of 5
  1. #1
    theCodes4000 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Using a CardLayout in a Java Applet

    Hey Everyone,

    I am trying to get some experience with the CardLayout manager. I have a two panels (titleView and contentView which are classes that extend JPanel) each with a button at the bottom of the panel that read "Next". Then in my applet I want to add these panels and when a button is clicked I want to go to the next panel. I keep getting an error that says cannot convert from borderlayout to cardlayout. Here is my code:

    public void init()
    {
    setLayout(new CardLayout());

    titleView = new titlePanel();
    titleView.next.addActionListener(this);
    contentView = new contentPanel();
    contentView.next.addActionListener(this);

    add(titleView, "Next");
    add(contentView, "Next");

    }

    public void actionPerformed(ActionEvent e)
    {
    CardLayout cl = (CardLayout)(this.getLayout());

    cl.show(this, "Next");
    }

    I am sure someone can point out my error. Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a CardLayout in a Java Applet

    I see a couple of problems:
    • You need to use a different String when adding each panel to the CardLayout-using container, not the same String "Next". The Strings should be String constants so there's no chance of messing them up.
    • When you add a layout to the JApplet (or JFrame or other top-level window), you are actually adding it to the contentPane not the applet itself. So you either need to get the layout from the contentPane or save the layout as a field. I prefer the latter, making a CardLayout field, and passing in the contentPane.
    • CardLayout already has a next(...) method, so there's no need for your use of show with String constants when desiring to simply go to the next "card" in the container.
    • A quibble: You're better off not having your GUI classes implement your listeners. To be cleaner use inner classes for the listeners.
    • Another nitpick: don't directly manipulate another class's fields. If you want outside classes to be able to add an ActionListener to a JButton, create a public method to allow it to do so.
    • If your two JPanel's share behaviors, might as well create them with this in mind either by having them share an abstract class or an interface. Example of abstract class below.


    For example
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FooApplet extends JApplet {
       private TitlePanel titleView;
       private ContentPanel contentView;
       private CardLayout cardlayout = new CardLayout();
    
       public void init() {
          setLayout(cardlayout);
          
          ActionListener buttonListener = new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent arg0) {
                cardlayout.next(getContentPane());
             }
          };
    
          titleView = new TitlePanel();
          titleView.addNextActionListener(buttonListener);
          contentView = new ContentPanel();
          contentView.addNextActionListener(buttonListener);
    
          add(titleView, titleView.getName());
          add(contentView, contentView.getName());
    
       }
    }
    
    @SuppressWarnings("serial")
    class TitlePanel extends BasePanel {
       public static final String NAME = "Title Panel";
    
       public TitlePanel() {
          super(NAME);
       }
    
    }
    
    @SuppressWarnings("serial")
    class ContentPanel extends BasePanel {
       public static final String NAME = "Content Panel";
    
       public ContentPanel() {
          super(NAME);
       }
    }
    
    abstract class BasePanel extends JPanel {
       private static final float FONT_SIZE = 24f;
       private JButton next = new JButton("Next");
    
       public BasePanel(String name) {
          setName(name);
          JLabel label = new JLabel(getName(), SwingConstants.CENTER);
          label.setFont(label.getFont().deriveFont(Font.BOLD, FONT_SIZE));
    
          JPanel btnPanel = new JPanel(new GridBagLayout());
          btnPanel.add(next);
    
          setLayout(new BorderLayout());
          add(btnPanel, BorderLayout.SOUTH);
          add(label, BorderLayout.CENTER);
       }
    
       public void addNextActionListener(ActionListener listener) {
          next.addActionListener(listener);
       }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    theCodes4000 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Using a CardLayout in a Java Applet

    Hey Thanks!! It works now, and my applet is turning out very nice :). However it now leads me to one more question. For a refresher, my whole applet design has 3 panels to flip back and forth through. Therefore, on the first panel I want to make my back button disabled. Likewise on my last panel I want to make the next button disabled or disappear. Are there any functions in Java to check what the current panel is in the CardLayout? Obviously I checked the CardLayout first... but I did not find anything helpful. I want to do something like this:

    if(cardPanel.current == lastCard) **NOTE** this would probably be done after the panel has been flipped from pressing the next button
    {
    nextButton.setEnabled(false);
    }

    Thank You all Java Guru's!

  5. #5
    theCodes4000 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Using a CardLayout in a Java Applet

    Additionally, I know of some simple "hacks" for solutions. For example, I am not interested in creating a counter variable and incrementing or decrementing it based upon which button is clicked and then checking if it is divisible by 3. This would tell me (or something similar) which Panel I was on. Also, I know I could use cardLayout.show(container, Constraint) with each button however that is undesirable because for a cardLayout it makes a lot of sense to use it like a double linked-list.

Similar Threads

  1. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 02:57 AM
  2. CardLayout manager
    By abetemari in forum New To Java
    Replies: 4
    Last Post: 03-27-2011, 01:46 AM
  3. Using a FlowLayout on top of a CardLayout
    By snieuw in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 06:03 PM
  4. Help with CardLayout
    By Kyle227 in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 01:03 AM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 07:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •