I have a counter in a JPanel in my java applet. I want the counter to update every time the int of the counter changes. How would I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Make a method
changeCounterthat allows you to change the counter. In that method update the control in yourJPanel.The proper way to go about this would be data binding, but afaik that’s still not in Java. There are plenty of libraries to do so, though.
Another option would be to implement it the usual way, i.e. Observer pattern, using listeners. There is a tutorial for that, even. Generally it works that way in Swing. You have objects that themselves can fire property change events whenever one of their properties changes and you add listeners for those events to change control values accordingly (and vice versa when the user changes a value in a control).