This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with cardlayout

 
Patricia Fulk
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That fixed the problem. After I read your response it made sense.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic