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:
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
Re: CardLayout wont go to the next card
Quote:
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.
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