Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: CardLayout wont go to the next card

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default CardLayout wont go to the next card

    Hi,
    I have a JPanel with a cardLayout, however when i try to go to the next card nothing happens. Here is my code:
    01	import java.applet.*;
    02	import java.awt.*;
    03	import java.util.*;
    04	import java.awt.event.*;
    05	import javax.swing.JEditorPane;
    06	import javax.swing.*;
    07	public class FindAddApplet extends Applet implements ActionListener
    08	{
    09	    private JLabel jcomp1;
    10	    private JButton nextButton;
    11	    private JButton prevButton;
    12	    private JPanel contentPanel;
    13	    //content layout
    14	    private CardLayout cardLayout;
    15	    //jpanel cards
    16	    ClassYearCard classYear;
    17	    PeriodCard periodCard;
    18	    //labels for cards
    19	    private int currentLabel = 0;
    20	    String classYearLabel = ("card1");
    21	    String periodCardLabel = ("card2");
    22	     
    23	    public void init() {
    24	        //construct components
    25	        cardLayout = new CardLayout();
    26	        jcomp1 = new JLabel ("Main Page");
    27	        nextButton = new JButton ("Next >");
    28	        prevButton = new JButton ("< Previous");
    29	        contentPanel = new JPanel ();
    30	        classYear = new ClassYearCard();
    31	        periodCard = new PeriodCard();
    32	   
    33	        //adjust size and set layout
    34	        setPreferredSize (new Dimension (415, 414));
    35	        setLayout (null);
    36	 
    37	        //add components
    38	        add (jcomp1);
    39	        add (nextButton);
    40	        nextButton.addActionListener(this);
    41	        add (prevButton);
    42	        add (contentPanel);
    43	        contentPanel.setLayout(cardLayout); //sets layout of contentPanel
    44	         
    45	        //set content layout
    46	         
    47	        //add stuff to panel
    48	        contentPanel.add(classYear, classYearLabel);
    49	        contentPanel.add(periodCard, periodCardLabel);
    50	         
    51	        //set component bounds (only needed by Absolute Positioning)
    52	        jcomp1.setBounds (25, 5, 100, 25);
    53	        nextButton.setBounds (295, 375, 110, 25);
    54	        prevButton.setBounds (175, 375, 110, 25);
    55	        contentPanel.setBounds (15, 30, 390, 340);
    56	    }
    57	 
    58	    public void start() {
    59	    }
    60	     
    61	    public void actionPerformed(ActionEvent e){
    62	        if (e.getSource() == "nextButton"){
    63	            cardLayout.show(contentPanel, "card2");
    64	        }
    65	    }
    66	 
    67	    public void stop() {
    68	    }
    69	 
    70	    public void destroy() {
    71	    }
    72	     
    73	}

    When i click the next button the panel just stays what it is currently, do i need to do something like repaint the contentPanel?
    Thanks,
    x5


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CardLayout wont go to the next card

    if (e.getSource() == "nextButton"){
    This test is suspect.
    What does the getSource() method return? Check if it is a String. Do you mean to use a variable there?

    You should use the equals() method when comparing the contents of two objects like Strings and not the == operator.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: CardLayout wont go to the next card

    Hey,
    You are right, i was comparing it to a string rather than the variable name,
    Thanks

Similar Threads

  1. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  2. [SOLVED] Help with if statement that wont act right...
    By deathpk in forum Loops & Control Statements
    Replies: 2
    Last Post: October 10th, 2011, 02:35 PM
  3. How to use CardLAyout without using applets???
    By divyarattan in forum AWT / Java Swing
    Replies: 4
    Last Post: August 9th, 2011, 11:19 AM
  4. Cardlayout Question
    By wagb278 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 10th, 2011, 03:23 PM
  5. Cardlayout help
    By phantomswordsmen in forum AWT / Java Swing
    Replies: 1
    Last Post: December 22nd, 2010, 02:36 PM