Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:54:30+00:00 2026-05-26T03:54:30+00:00

I got a JLabel Scoresp1 which I want to change using Scoresp1.setText(mijnScore + );

  • 0

I got a JLabel Scoresp1 which I want to change using Scoresp1.setText(mijnScore + "");. But the text on the JLabel stays the same.

I got a class Dobbelsteen which looks like this:

public class Dobbelsteen extends Spel {

    ...

    public void aantalOgen(int aantalogen) {

        oudepositie = huidigepositie;
        nieuwepositie = (huidigepositie + aantalOgen);
        if (nieuwepositie == eindronde) {
            System.out.println("Speler Piet heeft de ronde gewonnen!");
            updateUI();
        }
    }
}

Which calls updateUI which is in the class Spel

public class Spel {
    ...
   public void updateUI() {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {  
               ikWin = true;
               while(ikWin){
                   mijnScore = mijnScore+1;
                   Scoresp1.setText(mijnScore + "");
                   System.out.println("mijnScore" + mijnScore);
                   ikWin = false;
                   positie = 0;
                   }
              }

          });
       }
    ...
}

Scoresp1 is declared as public JLabel Scoresp1;. If I use String l = Scoresp1.getText(); I get the right value, but the JLabel doesn’t get updated visually.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:54:31+00:00Added an answer on May 26, 2026 at 3:54 am

    I’ve looked at some of you code, and my first concern (other than an over-use of static variables) is that you’re using inheritance inappropriately and because of this are calling methods on the wrong reference.

    Many classes inherit from Spel but don’t appear that they should be doing this. For instance, your Dobbelsteen class inherits from Spel, and yet it also has a separate Spel instance — why? What Spel object is currently visible at the time this code is run? I doubt it is the one that Dobbelsteen extends. Because of this, I think that you’re trying to changing the JLabel that is held by the the Dobbelsteen class, but it is not the “Spel” object that is currently visualized. To properly change the visualized JLabel, you’ll need a valid reference to the currently visualized Spel object that holds it, and call the appropriate public method on that class.

    In all, you might want to re-write this project from the ground up, with a goal of separating out your model (the data) from the view (the GUI), and with an eye towards good OOP principles.

    Edit 1:

    This may only be a bandaid, but what if you got your Spel reference passed to you in Dobbelsteen’s constructor, something like this (changes noted with !! comments: //!!):

    //!! public class Dobbelsteen extends Spel {
    public class Dobbelsteen { //!!
    
       int dobbelsteen;
       int nieuwepositie;
       int nieuwepositie2;
       public static String newPos;
       public static String newPos2;
       int oudepositie;
       int oudepositie2;
       int huidigepositie = Spel.positie;
       // int huidigepositie2 = Spel.positie2;
       int aantalOgen = Spel.aantalogen;
       int aantalOgen2 = Spel.aantalogen2;
       static boolean heeftgewonnen = false;
    
       // !! Spel spiel = new Spel();
       Spel spiel; // !!
    
       // !!
       public Dobbelsteen(Spel spiel) {
          this.spiel = spiel;
       }
    
       public void aantalOgen(int aantalogen) {
          oudepositie = huidigepositie;
          nieuwepositie = (huidigepositie + aantalOgen);
          if (nieuwepositie == Spel.eindronde) { //!!
             System.out.println("Speler Piet heeft de ronde gewonnen!");
             spiel.updateUI(); //!! ****** here in particular ******
          } else if (nieuwepositie > Spel.eindronde) {
             Spel.positie = huidigepositie; //!!
             spiel.output.setText("Je hebt teveel gegooid"); //!!
             spiel.output.setForeground(Color.red); //!!
          } else {
             Spel.oudpositie = oudepositie; //!!
             Spel.positie = nieuwepositie; //!!
             newPos = String.valueOf(nieuwepositie);
             if (SpelHost.host) {
                SpelHost.verstuurPositie("Positie" + newPos);
             } else if (SpelClient.client) {
                SpelClient.verstuurPositie("Positie" + newPos);
             }
          }
    
       }
    }
    

    And call it like so:

    class GooiDobbelsteen extends MouseAdapter {
    
       @Override
       public void mouseClicked(MouseEvent e) {
          aanBeurt = false;
          dobbelsteen = new Random();
          aantalogen = dobbelsteen.nextInt(6) + 1;
          aantalOog = String.valueOf(aantalogen);
          Dobbelsteen dobbel = new Dobbelsteen(Spel.this); // !!
          dobbel.aantalOgen(aantalogen);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a JScrollPane in which I want to place a list of radio
I'm coding a prototype, but got problems with the GUI. I want the JPanel
I've got a status JLabel in one class (named Welcome) and the timer in
I've got one class that has two text fields in it: name and surname.
I've got the idea of how I want my graphics to look but I've
Got a class that serializes into xml with XMLEncoder nicely with all the variables
Got a requirement to rebuild mssql full-text index. Problem is - I need to
So i got the grip on the basic MVC patterns in java using Observer
I've got a few windows, in a wizard-like fashion. Some of them are using
Got a issue with Facebook Graph API. I want to read page notifications via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.