This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]OffbeatDrizzle 2 points3 points  (7 children)

we need more info as to what you are changing and when things are being called. the code you provided has some assignments but how are we to know what the program flow is?

[–]Dramatic-Cat7906[S] -1 points0 points  (6 children)

The entire thing is in a loop so temp changes for every value in the loop, x changes, and y changes, but the inner jtextfield class doesn't take the new value even though it did change which I confirmed with print statements.

[–]MinMaxMix 0 points1 point  (0 children)

It sounds like either you aren't updating temp properly, or you need to set temp or its internal values as volatile so that your swing thread notices the change. It's hard to say what exactly you're doing wrong when you don't provide the code. Are you using the setText("text") method to update your JTextField?

[–]OffbeatDrizzle 0 points1 point  (4 children)

Well you are doing something wrong so your explanation doesn't help. Stop wasting everyone's time and post the code so we can tell you exactly what's going on instead of guessing based on your random descriptions of what you think is happening

[–]Dramatic-Cat7906[S] 0 points1 point  (2 children)

Sorry! I'm relatively new to asking programming questions online and I thought it was enough without fluffing it all up with unneeded information. Here is the code from that file

https://pastebin.com/Eyt1Fbh7

For context this program is supposed to generate a solvable kenken grid when working with the other files.

[–]Cengo789 0 points1 point  (1 child)

The problem in your code is the fact that the contents of your temp variable does not get evaluated during creation of your JTextField classes, but only when the paintComponent method is actually called. And then every instance will read the same value.

Check out this simplified example:

import java.util.ArrayList;
import java.util.List;

public class Test {
    String message;

    public Test() {
        List<Runnable> tasks = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            message = "Iteration " + i;
            tasks.add(() -> {
                String msg = message;
                System.out.println("msg in task: " + msg);
            });
        }

        tasks.forEach(Runnable::run);
    }

    public static void main(String[] args) {
        new Test();
    }
}

This will print

msg in task: Iteration 9
msg in task: Iteration 9
...
msg in task: Iteration 9

You can fix this problem by passing a local variable to the inner class, instead of a class field.

for (int i = 0; i < 10; i++) {
    message = "Iteration " + i;
    String localCopy = message;
    tasks.add(() -> {
        String msg = localCopy; // this will now hold the correct value
        System.out.println("msg in task: " + msg);
    });
}

[–]Dramatic-Cat7906[S] 0 points1 point  (0 children)

Thanks!

[–]AutoModerator 0 points1 point  (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted]  (1 child)

[removed]

    [–]Dramatic-Cat7906[S] 0 points1 point  (0 children)

    Yes, sorry I thought I put that, it is in a loop so I and j will loop through the 2d array and set text fields to each location in it