DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    11

    Card Layout, what is wrong?

    I've complied my applet file sucessfully but when I hit any button, a long list of errors appear. Can anyone point me in the right direction?

    (Sorry, don't know how to do a code box on this board)

    //Input Card
    JLabel labelInput = new JLabel ("Input", SwingConstants.CENTER);
    JPanel inputCard = new JPanel();
    inputCard.add (labelInput);
    deck.add (inputCard, labelInput.getText());

    ...other cards declared like this too...

    //add listeners
    input.addActionListener (this);
    admit.addActionListener (this);
    display.addActionListener (this);

    public void actionPerformed (ActionEvent event)
    {

    //show cards
    if (event.getSource() == input)
    cardManager.first(deck);
    else if (event.getSource ()== admit)
    cardManager.next(deck);
    else if (event.getSource()==display);
    cardManager.last(deck);

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Whatever the errors are they are not in the code you have posted here.
    eschew obfuscation

  3. #3
    Join Date
    Oct 2005
    Posts
    11
    OK, I wanted to avoid posting the entire code but the error must be here somewhere.

    entire code follows.
    ==

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

    public class ApplicationCentre extends JApplet implements ActionListener
    {

    private JPanel main, deck;
    private JLabel labelN, labelS, labelE, labelW;
    private JButton input, admit, display;
    private Container container;
    private CardLayout cardManager;
    // End of variables declaration

    public void init ()
    {
    initComponents();
    setSize(400, 100);
    }//end init

    public void initComponents()
    {
    container = getContentPane();
    container.setLayout( new BorderLayout() );

    // Creating instances of each item
    main = new JPanel();
    input = new JButton("Input");
    admit = new JButton("Admit");
    display = new JButton("Display");
    labelE = new JLabel("East");
    // End Creation

    //set up main menu
    main.setLayout (new GridLayout (3,0));
    main.add(input);
    main.add (admit);
    main.add (display);

    //set up deck
    CardLayout cardManager = new CardLayout();
    deck = new JPanel();
    //deck.setLayout (cardManager);

    //Input Card
    JLabel labelInput = new JLabel ("Input", SwingConstants.CENTER);
    JPanel inputCard = new JPanel();
    inputCard.add (labelInput);
    deck.add (inputCard, labelInput.getText());

    //Admit Card
    JLabel labelAdmit = new JLabel ("Admit");
    JPanel admitCard = new JPanel();
    admitCard.add (labelAdmit);
    deck.add (admitCard);

    //Display Card
    JLabel labelDisplay = new JLabel ("Display");
    JPanel displayCard = new JPanel();
    displayCard.add (labelAdmit);
    deck.add (displayCard);

    //add listeners
    input.addActionListener (this);
    admit.addActionListener (this);
    display.addActionListener (this);

    container.add( main, BorderLayout.WEST);

    container.add(deck, BorderLayout.EAST);
    }//end initcomponents


    //buttons!!
    public void actionPerformed (ActionEvent event)
    {

    //show cards
    if (event.getSource() == input)
    cardManager.first(deck);
    else if (event.getSource ()== admit)
    cardManager.next(deck);
    else if (event.getSource()==display);
    cardManager.last(deck);
    }//end button handler


    }

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    See my comments

    There was some problems with your use of a cardlayout

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ApplicationCentre
        extends JApplet
        implements ActionListener {
    
      private JPanel main, deck;
      private JLabel labelN, labelS, labelE, labelW;
      private JButton input, admit, display;
      private Container container;
      private CardLayout cardManager;
      // End of variables declaration
    
      public void init() {
        initComponents();
        setSize(400, 100);
      } //end init
    
      public void initComponents() {
        container = getContentPane();
        container.setLayout(new BorderLayout());
    
        // Creating instances of each item
        main = new JPanel();
        input = new JButton("Input");
        admit = new JButton("Admit");
        display = new JButton("Display");
        labelE = new JLabel("East");
        // End Creation
    
        //set up main menu
        main.setLayout(new GridLayout(3, 0));
        main.add(input);
        main.add(admit);
        main.add(display);
    
    
        //set up deck
        //CardLayout cardManager = new CardLayout();
        // ERROR ******************
        // the above is a redeclaration, the cardManager here is local to the
        // initComponents method, the cardManager declared at class level
        // is still null
    
        cardManager = new CardLayout();
        deck = new JPanel(cardManager);
        //deck.setLayout (cardManager);
    
        //Input Card
        JLabel labelInput = new JLabel("Input", SwingConstants.CENTER);
        JPanel inputCard = new JPanel();
        inputCard.add(labelInput);
        deck.add(inputCard, "Input");
    
        //Admit Card
        JLabel labelAdmit = new JLabel("Admit");
        JPanel admitCard = new JPanel();
        admitCard.add(labelAdmit);
        deck.add(admitCard, "Admit");
    
        //Display Card
        JLabel labelDisplay = new JLabel("Display");
        JPanel displayCard = new JPanel();
        //displayCard.add(labelAdmit); // <--- ooops
        displayCard.add(labelDisplay);
        deck.add(displayCard,"Display");
    
        //add listeners
        input.addActionListener(this);
        admit.addActionListener(this);
        display.addActionListener(this);
    
        container.add(main, BorderLayout.WEST);
    
        container.add(deck, BorderLayout.EAST);
      } //end initcomponents
    
      //buttons!!
      public void actionPerformed(ActionEvent event) {
    
        //show cards
        if (event.getSource() == input)
          cardManager.show(deck,"Input");
        else if (event.getSource() == admit)
          cardManager.show(deck,"Admit");
        else if (event.getSource() == display)
          cardManager.show(deck,"Display");
      } //end button handler
      public static void main(String[] args) {
          JFrame f = new JFrame("Applet tester");
          f.getContentPane().setLayout(new GridLayout());
          ApplicationCentre applet = new ApplicationCentre();
          f.getContentPane().add(applet);
          applet.init();
          f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });
          f.setBounds(100, 100, 300, 300);
          f.setVisible(true);
        }
    
    }
    eschew obfuscation

  5. #5
    Join Date
    Oct 2005
    Posts
    11
    OK, I fixed my code as indicated above but pushing the buttons on the West still does nothing to flip the cards.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Then you have missed out on a detail or two cause this code flips cards like mad
    on my computer.

    Check it again.
    eschew obfuscation

  7. #7
    Join Date
    Oct 2005
    Posts
    11
    Got it! Thanks!

Similar Threads

  1. Replies: 1
    Last Post: 03-29-2009, 11:49 AM
  2. Replies: 4
    Last Post: 01-22-2007, 02:45 AM
  3. Replies: 0
    Last Post: 04-25-2001, 09:29 PM

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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links