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"); } } }
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
posted
0
Thanks! That fixed the problem. After I read your response it made sense.