• Post Reply Bookmark Topic Watch Topic
  • New Topic

Referring to static variables from within a non-static instance-to-be class  RSS feed

 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I'm writing a simple dice rolling application.
In the main window, a panel is supposed to change to another panel with a click of a button from another panel, that is occupied with a custom toolbar.
It's actually a lot simpler that I painted it out to be. Think about it like:



Now, my action listener for the buttons in the toolbar are in an inner-class inside the custom toolbar class.
I can't refer to said panel from the action listener because it is (by necessity) a static class.

What do you guys think I should do?

Thank you all very much in advance!

Expectantly,
Eden.

Edit: Umm... Bit of a problem with the explanatory art, but you get the idea, I hope!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put your explanatory art inside code tags, which retains formatting like multiple spaces. If that's not exactly what you wanted to show, you can edit the post and correct it.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly how I wanted it to show, didn't know code tags keep empty spaces.
So thank you about that, but it still doesn't answer my question.
At least it'll be clearer now!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

Are you using a CardLayout?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why have you got static fields at all?

As an alternative to code tags, you can make spaces appear with the   character.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no static FIELDS at all.
I want to access a non-static variable (or method, I can write a method for that) for an actionListener.

As for the example code, I'll finish writing it soon and post it.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eden Landau wrote:I have no static FIELDS at all. . . .
Good. Shall we presume “static” in the thread title was a spelling mistake?

You probably want something like thisAny local variables used in the anonymous class must be marked final. Fields don’t have to be final. Example about writing anonymous classes here and more about listeners here. I usually view the idiom addActionListener(this) as an abomination of non-object-oriented programming.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: This doesn't compile for me. The compiler gives me a long list of errors regarding anything that is Swing in my code, so... Yeah.
If this is myself doing something wrong, I'd like to be corrected about that as well, but regardless, the code should give you guys a fairly clear notion at what I'm attempting to do.
The code in the 'insert code here' sections should call to the setPanel method to switch to the appropriate panel. Thank you very much!

This is the example code:



 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eden Landau wrote:Note: This doesn't compile for me. The compiler gives me a long list of errors regarding anything that is Swing in my code, so... Yeah.

Yeah. Lets take it from the top. Post the entire text (no paraphrasing!) of the first compiler error and indicate with which line it is associated.

You do understand variable scope ... or don't you? Variables declared inside a method or otherwise inside a pair of braces { } are visible only inside that pair of braces.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what were the errors? We can only help if you give us the details.
Design suggestion: you can call initUI() indirectly by calling the constructor. You probably won’t want it called any other way, so give that method private access.
Your setPanel method won’t compile because there is no such thing as bottomPanel in scope. I would suggest you can delete that method altogether. You probably won’t want to reassign bottomPanel like that anyway.
You have bottomPanel as a local variable in initUI, which looks correct, then you are reassigning it to panelOne. Why? You are also using panelOne before you declare it. The compiler won’t like that.
You are then declaring panelTwo and panelThree, and making no use of them.
I would suggest you try using setBounds rather than setSize, because that will move your frame out of the corner of the screen.
You are probably all right using System.exit in a non-threaded environment, but if you are threading and use exit, you might terminate another thread before it has finished its task, leaving corrupted file etc. Beware of System.exit. You probably can’t use it, because you didn’t add the exit button to anything!
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler throws said errors for the example code. The errors it gives me for the actual program only relate to the problem I'm asking about.
Seems legit.
That is my entire problem right there. How can I refer to a method like that from CustomToolBar, or even just the variable, or something else that'll achieve the same effects.
The bottomPanel variable is reassigned to panelOne so that it could be reassigned to panelTwo\Three from the CustomToolBar, using, assumably, the buttons there.
That is why I added the insert code here sections in the first place, so that you could fill in the blanks so that the program'll be able to switch panels at the press of a JButton, so to speak.
I'll do that.
Yeah, I know about the System.exit multi-thread problem. Hopefully I'll remember it when I'll start working with threads!
And lastly, whoops. The idea was that panelOne was supposed to contain labelOne, panelTwo labelTwo, and panelThree exit. I'll edit it now.

Please address the issues presented here.
Thank you again!

Edit: You are referring to small stuff. The example code was meant only to clarify what I'm trying to do, not to run and work. (:
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: This doesn't compile for me. The compiler gives me a long list of errors regarding anything that is Swing in my code, so... Yeah.
If this is myself doing something wrong, I'd like to be corrected about that as well, but regardless, the code should give you guys a fairly clear notion at what I'm attempting to do.
The code in the 'insert code here' sections should call to the setPanel method to switch to the appropriate panel. Thank you very much!

This is the example code:



 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comment out 90% of your code, and try again. You want a load of empty methods and constructors, everything else being commented out. That probably won’t cure the compiler errors, but will reduce them to a manageable number. Sort them out, and gradually comment the code back in. Then you will start getting more compiler errors, but probably again only a few at a time.

And you still seem to have the same errors I pointed out earlier, eg about location of panelOne. And you said “said errors” without telling us what the errors are.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eden Landau wrote:Edit: You are referring to small stuff. The example code was meant only to clarify what I'm trying to do, not to run and work. (:

^ That. Ignore the code I posted, it's not even meant to be compilable. My question is very specific, and is just about to the referral to the main panel, from the actionListener.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!