Results 1 to 12 of 12
  1. #1
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default Problem about JLabel not update in JPanel

    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(){
    		
    	}
        
    
    	
    }
    I want to display the value(score) from JPanel class to show in JApplet.
    But it is not work.
    I searched on internet and the book but I still not get the right answer.
    Can anybody hint me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 11:02 PM.

  3. #3
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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.

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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 05:14 PM.

  7. #7
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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().

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
          }
       }
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fantasyme View Post
    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.
    Note that I don't usually code applets, but it is my understanding that stop and destroy are called by the browser or applet viewer and not by the applet itself.

  11. #11
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    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

  12. #12
    handbagsca is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default 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

  1. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 06:10 AM
  2. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  3. Update JPanel
    By collin389 in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 12:30 AM
  4. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 09:59 PM
  5. [SOLVED] JLabel not showing on JPanel
    By onefootswill in forum New To Java
    Replies: 11
    Last Post: 08-23-2008, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •