File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem with cardlayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with cardlayout" Watch "Problem with cardlayout" New topic

Problem with cardlayout

Patricia Fulk
Ranch Hand

Joined: Sep 10, 2001
Posts: 62
I have some code that I am trying to update the values on a card and show the changes. I have created a little sample program that has the same problem. In this sample program why isn't totalCount being updated? Is there a way to display the updated totalCount? Help would be greatly appreciated.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{
private CardLayout cardLayout;
private int totalCount;
private Button myButton;
public void init() {
totalCount = 0;
cardLayout = new CardLayout();
public void start() {
Panel myPanel = new Panel();
myPanel.setLayout(new GridLayout(1,2));
myPanel.add(new Label(String.valueOf(totalCount)));
myButton = new Button("click");

public void actionPerformed(ActionEvent e) {
if (e.getSource() == myButton) {
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Patricia,
Quite simply, you are not chaning it!
You will need to get a handle on the label that holds the counter:
private Label myLabel;
Then you will need to assign a label to this:
myLabel = new Label( String.valueOf( totalCount ) );
Then you can change it inside your button actionPerformed:
myLabel.setText( String.valueOf( totalCount ) );
Patricia Fulk
Ranch Hand

Joined: Sep 10, 2001
Posts: 62
Thanks! That fixed the problem. After I read your response it made sense.
I agree. Here's the link:
subject: Problem with cardlayout
It's not a secret anymore!