This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help setting up user interface  RSS feed

 
sander johnson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spent hours reading my (beginners) javabook but I cannot find a solution to this. It seems very simple and googling I've come across similiar threads but the solutions are too difficult. What I'm trying to do is show a loginscreen. After logging in it should show another contentpane with actual content.

This is my main class:



And this is is inside another class:



The compiler gives me an error when I try to refer to "frame". What I've tried to do is refer to frame by using
Project.frame but it still gives me the same error. The compiler shows the message that it cannot find "frame" in class Project, but that's exactly where it is!

Is there a simple way to make this work? I've read about threads and loops but they don't seem the most basic solution. Maybe there's a way to get the current frame so I can use that?




 
Campbell Ritchie
Sheriff
Posts: 55280
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Where did you import JPanel and JFrame?
The frame variable is not in the Project class at all. It is a local variable in the main method and has no existence anywhere else.
 
sander johnson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. I rewrote my code and I understand that I somehow need to pass information on to keep it saved.





I'm able to pass on a string. What I'm trying to do for my testproject however is to pass on an object (reference?) so I can use it in the class Frame. I've tried passing on "frame" using the same logic but it didn't work.
 
Campbell Ritchie
Sheriff
Posts: 55280
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you have frame as a local variable in one method and you can't use it in another method. But you don't want to pass it from method to method. There is a better way to do it.

Don't call a class Frame because it already exists. I don't like extending Swing components, but you often have to extend a JPanel to override its paintComponent method.
Create a class called GUI. You are allowed to have very long methods for setting up GUIs (or long constructors).Now all you have to do to get that to appear is call new GUI(). You really should start a special thread but calling new GUI(); in the main method will get that to appear, so you can get away without starting the thread for the time being. You will obviously need import statements, too.
You can pass information from method to constructor. All you need to do is set up the GUI constructor to take a String as a parameter and use that String wherever you need it.
 
Campbell Ritchie
Sheriff
Posts: 55280
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass a reference to any object from method to method if there is an appropriate parameterThis example however is rather poor design.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!