GeeCON Prague 2014*
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
Author

Problem with cardlayout

Patricia Fulk
Ranch Hand

Joined: Sep 10, 2001
Posts: 62
Hello,
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.
Thanks,
Trish

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");
myButton.addActionListener(this);
myPanel.add(myButton);

setLayout(cardLayout);
add(myPanel,"1");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myButton) {
totalCount++;
cardLayout.show(this,"1");
}
}
}
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:
totalCount++;
myLabel.setText( String.valueOf( totalCount ) );
Regards,
Manfred.
Patricia Fulk
Ranch Hand

Joined: Sep 10, 2001
Posts: 62
Thanks! That fixed the problem. After I read your response it made sense.
 
wood burning stoves
 
subject: Problem with cardlayout