Changing JLabels from another class.

0 Jonas Meise · May 18, 2015
Hey Guys
I am programming a calculator with buttons and have a separate class "Operators" for the "brain behind the buttons" (as bucky would say).
So Now when I press the Buttons they execute the Operators(int nr) method in the Operators class, which is working since the console prints out the number of the button pressed, but it does not change the text of Label1.
The thoughts behind my code is that in the Operators(int nr) method the method labels.setLabel1(nr); gets called, which is in the main.java class and has the code label1.setText(txt);. So theoretically it should change the text of Label1. But it doesn't. Anyone knows why?

Here is the full code:
/images/forum/upload/2015-05-17/d0f8970994cf9a70b09706b0616f5632.PNG
/images/forum/upload/2015-05-17/b5a0d7af83404c5b61394881314b569b.PNG
/images/forum/upload/2015-05-17/fa9cb9680edd1c64af7b1ee2e148d76e.PNG

Post a Reply

Replies

Oldest  Newest  Rating
0 Jonas Meise · May 18, 2015
I chose the first option and it works so thanks :) And whenever I solve a problem I discover a new one xD.
Here is my "Operating system" so far:
/images/forum/upload/2015-05-18/acd98b6a9b43cb9e4cdcec79fa40d2fa.PNG
So When I press the Button 4 it now changes label1 to display fvar = 4.
But when I press 4 again it doesn't add up fvar to 44 like it should.
I think the problem is that in my main.java the Object: Operators op1 = new Operators();
is in the Actionlistener part so whenever an action happens the Object creates from start and looses the saved variables.
When I place the Code where I generate that Object somewhere else( on line 20 for example) it does not show me a warning sign but when I run the program it doesn't work. It shows me an error message for the line where I placed the Object.
Here is my main.java so far:
/images/forum/upload/2015-05-18/67e03eb032011e843350c493aaf8a5c5.PNG
/images/forum/upload/2015-05-18/c499415151610da00241ef6061f0a33e.PNG

Sorry if I ask alot of dumb questions^^ I'm quite new to Object oriented programming in general
0 Jonas Meise · May 18, 2015
Hmm if I put it in Main with "public" it says that for op1 only final is allowed and if I put it there without "public" this error happens:




Exception in thread "main" java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.(Unknown Source)
at java.awt.Frame.(Unknown Source)
at java.awt.Frame.(Unknown Source)
at javax.swing.JFrame.(Unknown Source)
at Main.(Main.java:19)
at Operators.(Operators.java:6)
at Main.(Main.java:9)
at Operators.(Operators.java:6)
at Main.(Main.java:9)
at Operators.(Operators.java:6)
at Main.(Main.java:9)
at Operators.(Operators.java:6)
at Main.(Main.java:9)

//This 2 lines of the error code continue for a really long time
//about 50 more times
at Operators.(Operators.java:6)
at Main.(Main.java:9)
0 Jonas Meise · May 18, 2015
Dayum even when I put final the same error messgae shows up. FOr line 20 (where
 final Operators op1 = new Operators();)

is and for line 6 in Operators.java
0 Jonas Meise · May 18, 2015
whoops that wink smiley is not mean to be there xD
0 Jonas Meise · May 18, 2015
Oh okay I found it. If create the object Operators which extends Main it kinda doesn't work. So i just deleted "extends Main" since I don't need that anywhere. Thanks for your help :)
0 Jonas Meise · May 18, 2015
Oh okay thanks alot :) I just had to change it around abit because my "result" was a variable of type double so I had to convert it to a String first but now it works :) Looks like this :
/images/forum/upload/2015-05-18/4017d0c4718ea956644388f0ccf294f9.PNG
0 Jonas Meise · May 18, 2015
Uhm I'm quite new on the forum. Do moderators close topics when the question is answered or do I somehow do that? ;)
0 Jonas Meise · May 18, 2015
Hmm wait I just noticed a problem. If I do it like that it will always place the Number of the Button I clicked in Label1. But I have in mind that if i press an operator button (+,-,*,/) and then click a Number button again it should place the second number in label 2. So it would be more ideal if it worked my way so I can just call the function setLabelx from the other class...
Is there another way so that I can do it my way?
  • 1

Java / Android Development

106,928 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator