Updating GUI by modifying a variable from another class not working java
I am trying to disable a JButton in the GUI class, by incrementing the turn variable from another class - the test class. This is a simplified and modified excerpt from a bigger school project. Trying to modify the view or GUI from the controller class based on data given back from the model.
This is the GUI class
import java.awt.event.*;
import javax.swing.*;
public class test implements ActionListener {
private int turn;
private JFrame d;
private JButton a;
private int clicked;
public test() {
clicked=0;
turn=0;
d = new JFrame();
a = new JButton();
d.add(a);
a.addActionListener(this);
d.setSize(75,150);
d.setVisible(true);
}
public int getTurn() {
return turn;
}
public void setTurn(int turn) {
this.turn = turn;
}
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
public static void main(String args) {
// TODO Auto-generated method stub
new test();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
clicked=1;
if(turn==3) {
((JButton)e.getSource()).setEnabled(false);
}
}
}
While this is the class that attempts to make the GUI changes
public class test2 {
private int turn;
private test gui;
public test2() {
gui = new test();
while(gui.getTurn()<3){
play();
}
}
public void play() {
if(gui.getTurn()<=3 && gui.getClicked()==1) {
gui.setTurn(gui.getTurn()+1); //update the turn variable in the gui
gui.setClicked(0);
}
}
public static void main(String args) {
new test2();
}
}
Any help is highly appreciated
java user-interface
|
show 6 more comments
I am trying to disable a JButton in the GUI class, by incrementing the turn variable from another class - the test class. This is a simplified and modified excerpt from a bigger school project. Trying to modify the view or GUI from the controller class based on data given back from the model.
This is the GUI class
import java.awt.event.*;
import javax.swing.*;
public class test implements ActionListener {
private int turn;
private JFrame d;
private JButton a;
private int clicked;
public test() {
clicked=0;
turn=0;
d = new JFrame();
a = new JButton();
d.add(a);
a.addActionListener(this);
d.setSize(75,150);
d.setVisible(true);
}
public int getTurn() {
return turn;
}
public void setTurn(int turn) {
this.turn = turn;
}
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
public static void main(String args) {
// TODO Auto-generated method stub
new test();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
clicked=1;
if(turn==3) {
((JButton)e.getSource()).setEnabled(false);
}
}
}
While this is the class that attempts to make the GUI changes
public class test2 {
private int turn;
private test gui;
public test2() {
gui = new test();
while(gui.getTurn()<3){
play();
}
}
public void play() {
if(gui.getTurn()<=3 && gui.getClicked()==1) {
gui.setTurn(gui.getTurn()+1); //update the turn variable in the gui
gui.setClicked(0);
}
}
public static void main(String args) {
new test2();
}
}
Any help is highly appreciated
java user-interface
if(turn==3)
- where isturn
updated?
– Scary Wombat
Nov 22 '18 at 0:49
How can this be true?if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
Alsoplay
method is only going to be entered intoonce
– Scary Wombat
Nov 22 '18 at 1:04
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
1
TBH I am not sure what you are trying to achieve.while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.
– Scary Wombat
Nov 22 '18 at 1:13
|
show 6 more comments
I am trying to disable a JButton in the GUI class, by incrementing the turn variable from another class - the test class. This is a simplified and modified excerpt from a bigger school project. Trying to modify the view or GUI from the controller class based on data given back from the model.
This is the GUI class
import java.awt.event.*;
import javax.swing.*;
public class test implements ActionListener {
private int turn;
private JFrame d;
private JButton a;
private int clicked;
public test() {
clicked=0;
turn=0;
d = new JFrame();
a = new JButton();
d.add(a);
a.addActionListener(this);
d.setSize(75,150);
d.setVisible(true);
}
public int getTurn() {
return turn;
}
public void setTurn(int turn) {
this.turn = turn;
}
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
public static void main(String args) {
// TODO Auto-generated method stub
new test();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
clicked=1;
if(turn==3) {
((JButton)e.getSource()).setEnabled(false);
}
}
}
While this is the class that attempts to make the GUI changes
public class test2 {
private int turn;
private test gui;
public test2() {
gui = new test();
while(gui.getTurn()<3){
play();
}
}
public void play() {
if(gui.getTurn()<=3 && gui.getClicked()==1) {
gui.setTurn(gui.getTurn()+1); //update the turn variable in the gui
gui.setClicked(0);
}
}
public static void main(String args) {
new test2();
}
}
Any help is highly appreciated
java user-interface
I am trying to disable a JButton in the GUI class, by incrementing the turn variable from another class - the test class. This is a simplified and modified excerpt from a bigger school project. Trying to modify the view or GUI from the controller class based on data given back from the model.
This is the GUI class
import java.awt.event.*;
import javax.swing.*;
public class test implements ActionListener {
private int turn;
private JFrame d;
private JButton a;
private int clicked;
public test() {
clicked=0;
turn=0;
d = new JFrame();
a = new JButton();
d.add(a);
a.addActionListener(this);
d.setSize(75,150);
d.setVisible(true);
}
public int getTurn() {
return turn;
}
public void setTurn(int turn) {
this.turn = turn;
}
public int getClicked() {
return clicked;
}
public void setClicked(int clicked) {
this.clicked = clicked;
}
public static void main(String args) {
// TODO Auto-generated method stub
new test();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
clicked=1;
if(turn==3) {
((JButton)e.getSource()).setEnabled(false);
}
}
}
While this is the class that attempts to make the GUI changes
public class test2 {
private int turn;
private test gui;
public test2() {
gui = new test();
while(gui.getTurn()<3){
play();
}
}
public void play() {
if(gui.getTurn()<=3 && gui.getClicked()==1) {
gui.setTurn(gui.getTurn()+1); //update the turn variable in the gui
gui.setClicked(0);
}
}
public static void main(String args) {
new test2();
}
}
Any help is highly appreciated
java user-interface
java user-interface
edited Nov 22 '18 at 1:18
osayi
asked Nov 22 '18 at 0:46
osayiosayi
42
42
if(turn==3)
- where isturn
updated?
– Scary Wombat
Nov 22 '18 at 0:49
How can this be true?if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
Alsoplay
method is only going to be entered intoonce
– Scary Wombat
Nov 22 '18 at 1:04
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
1
TBH I am not sure what you are trying to achieve.while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.
– Scary Wombat
Nov 22 '18 at 1:13
|
show 6 more comments
if(turn==3)
- where isturn
updated?
– Scary Wombat
Nov 22 '18 at 0:49
How can this be true?if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
Alsoplay
method is only going to be entered intoonce
– Scary Wombat
Nov 22 '18 at 1:04
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
1
TBH I am not sure what you are trying to achieve.while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.
– Scary Wombat
Nov 22 '18 at 1:13
if(turn==3)
- where is turn
updated?– Scary Wombat
Nov 22 '18 at 0:49
if(turn==3)
- where is turn
updated?– Scary Wombat
Nov 22 '18 at 0:49
How can this be true?
if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
How can this be true?
if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
Also
play
method is only going to be entered into once
– Scary Wombat
Nov 22 '18 at 1:04
Also
play
method is only going to be entered into once
– Scary Wombat
Nov 22 '18 at 1:04
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
1
1
TBH I am not sure what you are trying to achieve.
while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.– Scary Wombat
Nov 22 '18 at 1:13
TBH I am not sure what you are trying to achieve.
while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.– Scary Wombat
Nov 22 '18 at 1:13
|
show 6 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422425%2fupdating-gui-by-modifying-a-variable-from-another-class-not-working-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422425%2fupdating-gui-by-modifying-a-variable-from-another-class-not-working-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if(turn==3)
- where isturn
updated?– Scary Wombat
Nov 22 '18 at 0:49
How can this be true?
if(gui.getTurn()>3
– Scary Wombat
Nov 22 '18 at 0:54
Also
play
method is only going to be entered intoonce
– Scary Wombat
Nov 22 '18 at 1:04
Thanks i updated it but unfortunately still does not work @ScaryWombat
– osayi
Nov 22 '18 at 1:08
1
TBH I am not sure what you are trying to achieve.
while(gui.getTurn()<3){ play(); }
is going to be looping very fast and any multi^threaded updates are not going to be noticed.– Scary Wombat
Nov 22 '18 at 1:13