Results 1 to 12 of 12
- 04-16-2010, 11:57 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Problem about JLabel not update in JPanel
I want to display the value(score) from JPanel class to show in JApplet.Java Code:public class game extends JApplet { private roll mygame; private JLabel result; public game(){ setLayout(new BorderLayout()); result = new JLabel("Welcome"); result.setFont(new Font("Sansserif", Font.PLAIN, 14)); getContentPane().add(result,BorderLayout.NORTH); mygame = new roll(); getContentPane().add(mygame,BorderLayout.CENTER); } public void start(){ result.setText(""+mygame.score); setSize(240, 300); } public void stop(){ } public void destroy(){ } }
But it is not work.
I searched on internet and the book but I still not get the right answer.
Can anybody hint me?
-
edit: reply deleted. I see that I replied to your previous similar post (previous thread) but you never responded. I'll hold off replying to your posts til I am sure that you're actually reading them. Otherwise how do I know that I'm not just wasting my time? Best of luck.
Last edited by Fubarable; 04-16-2010 at 10:02 PM.
- 04-16-2010, 07:02 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
For the previous thread, the problem is totally solved.
Thanks for that.
It works well on java application.
I am sorry that I never know that I should report the result.
And I think it should work well on java applet but it doesn't.
-
If you want the JLabel to hold the updated score, you will either have to pass a listener into the roll object so that when the score is updated, the listener will be notified and the score extracted from roll and passed into the JLabel or else pass the JLabel into the roll object (via a method or constructor parameter) and have roll update the JLabel itself.
- 04-17-2010, 06:02 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
You mean that I set score's label in JPanel (roll()), right?
I already done it but the reason that I want to derive the score data to JApplet because
I think I want to set the condition when the game should stop like when the score = something then in stop() and destroy will change to another page or something.
- 04-17-2010, 04:11 PM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
I'll give you example.
I think it's useful.
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Container; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; public class game extends JApplet { GamePanel panel = new GamePanel(); public void init() { Container content = getContentPane(); content.add(panel); } } class GamePanel extends JPanel implements ActionListener { private int newScore; JButton button = new JButton("Change score"); JLabel scoreLabel = new JLabel("Score: "); public GamePanel() { button.addActionListener(this); add(button); add(scoreLabel); } @Override public void actionPerformed(ActionEvent arg0) { newScore += 10; String score = Integer.toString(newScore); scoreLabel.setText("Score: " + score); } }Last edited by cselic; 04-17-2010 at 04:14 PM.
- 04-18-2010, 04:58 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Thanks, cselic.
However, it is not actually what I am looking for.
the score data was set in JPanel class(which you set as GamePanel ....or whatever)
And the button also set in GamePanel class.
So there is no need to put button in Applet.
I just want to know how applet will retrived the data for GamePanel.
Because I will use as comparison for game over.
For example, (if score == 10){game finised}
But the problem is score is not in Applet but in GamePanel.
sorry if my words confused you.
I may think in the wrong way...
Hope your advice again
-
You might want to have your game class accept either a ChangeListener or PropertyChangeListener as well as have a public int getScore() method. When the score changes, it notifies all the listeners who can then extract the score via getScore().
-
An example that uses a ChangeListener:
Java Code:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class DemoChangeApplet extends JApplet { private JLabel appletScoreLabel = new JLabel("0", SwingConstants.CENTER); public void init() { try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } private void createGUI() { final GamePanel gamepanel = new GamePanel(); gamepanel.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { // method simply gets score from the game panel int score = gamepanel.getScore(); // and then uses this to update a JLabel in the JApplet appletScoreLabel.setText(String.valueOf(score)); } }); JPanel statusPanel = new JPanel(); statusPanel.add(new JLabel("Score (in the Applet): ")); statusPanel.add(appletScoreLabel); getContentPane().add(gamepanel, BorderLayout.CENTER); getContentPane().add(statusPanel, BorderLayout.SOUTH); } } class GamePanel extends JPanel { protected static final int ADD_AMOUNT = 5; private int score = 0; // list to hold any change listeners private List<ChangeListener> changeListenerList = new ArrayList<ChangeListener>(); // object that's passed to each change listener's stateChanged method private ChangeEvent changeevent = new ChangeEvent(this); private JLabel scoreLabel = new JLabel("0"); GamePanel() { JButton addToScoreButton = new JButton("Add To Score"); addToScoreButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { addToScore(ADD_AMOUNT); } }); setBorder(BorderFactory.createTitledBorder("Game Panel")); add(new JLabel("Score (in the GamePanel): ")); add(scoreLabel); add(addToScoreButton); } public void addChangeListener(ChangeListener changelistener) { changeListenerList.add(changelistener); } public int getScore() { return score; } public void addToScore(int amountToAdd) { score += amountToAdd; // add to score scoreLabel.setText(String.valueOf(score)); // show new score in game panel // notify the listeners, if any for (ChangeListener changelistener : changeListenerList) { changelistener.stateChanged(changeevent); } } }
-
- 04-18-2010, 10:38 AM #11
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Thanks,
the problem was solved. And I still curious about
for (ChangeListener changelistener : changeListenerList) {
changelistener.stateChanged(changeevent);
}
I don't understand the fomula meaning,
is it same as for(i=0:i ??:i++)? something like that?
And after I search on internet, stop() and destroy () seems for stop THREADS' thing.
However, I read the definition and still do not understand.
It said like the applet will be destroyed before change to another webpage.
Anyway, I want to know how to change webpage by using applet.
I cannot find the good sample now.
Maybe I did not use exactly right keyword...
anyway, thanks a lot
- 10-16-2011, 07:00 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Re: Problem about JLabel not update in JPanel
The seized gold hogan and silver enough to take a year to support our military
Similar Threads
-
Jlabel update problem
By fantasyme in forum AWT / SwingReplies: 3Last Post: 04-14-2010, 05:10 AM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Update JPanel
By collin389 in forum New To JavaReplies: 3Last Post: 11-25-2009, 11:30 PM -
JPanel won't update
By ibanez270dx in forum New To JavaReplies: 3Last Post: 01-06-2009, 08:59 PM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks