wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes GUI Big trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI Big trouble" Watch "GUI Big trouble" New topic
Author

GUI Big trouble

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
So i have this program that creates an apartment object and and creates an array. Each index in the array represents an apartment, with each value being store in the array representing the number of occupants.. I wanted to build the program and test it so i did and i got that working... now i need to implement GUI into it to retrieve the data from the user. But my textbook does not even really explain GUI in a way I can understand... If someone can just give me osme pointer to get me going. Here is my code for the apartment class


and this is what the textbook gives me as an example of how to construct GUI... this is copied right out of the book word for word and it will not compile for me.

Please help... otherwise i'll be forced to take a zero on this assignment..
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
this is copied right out of the book word for word...


Not quite... your code has a lot of typographical errors in it that I suspect are errors that occurred when you tried to copy the books code to your computer. I recommend that first and foremost you proof-read your code and find all the mistakes that are there, then repost the corrected code. Programming is not very forgiving of typos or other seemingly small errors. After you get your demo GUI running, then try to adapt it to your current program and why not post that attempt here.

Best of luck!
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
I found the code online from my textbook... here it is exactly as they give it to me and i still cannot compile it:

it is giving me two errors...
cannot find symbol variable outputLabel
cannot find symbol class ButtonHandler

Am i even heading in the right direction with this?
and once i get this to compile how do i tell it i want to read in from my method in apartment?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
edit: nope I'm wrong. Does your book's code have all those "static" declarations there? That seems the biggest error to me off the bat.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
no the book doesn't have them all declared static... i'll remove them?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
when i removed the static a get a non-static variable cannot be referenced from a static context error for all the variables
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Bryan Peach wrote:no the book doesn't have them all declared static... i'll remove them?


Yes, remove them. By adding them you are solving the wrong problem. Since your book is on line, can you just paste the code in here without changing anything? Let's see what they give you to start with.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
and when i add (this) like you said for the done.addActionListener(this); i get a non-static variable this cannot be referenced from a static context error from it..
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
this is the online source... i added some stuff earlier to make it match the book code better... this is it with nothing added
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mixing and matching your two bits of code, I'm guessing that the book's original code looked something like this:



Am I close?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
yes thats exactly how it was.. except

is getText...
and they have the main method in a different class but that shouldn't matter should it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
The reason for having all those fields declared static is because you are calling everything from inside the main() method. Agree with Pete Stein that putting everything into the main method is an abomination against object-oriented programming. Yes, I know a lot of books do it. That is because it takes less space to put it in the main method. You would do well to move all that code into a constructor for the GUI class then the main method can be reduced toor . . . or something like that.

The older version with "implements ActionListener" is also an abomination against object-oriented programming. I have said so before on these fora, but not for few years. Fortunately the newer version has "new ButtonHandler()" which is good object-oriented design. At least it would be if you actually had a ButtonHandler class anywhere. That is one of your compiler problems. The other is that you haven't declared your outputLabel anywhere.

Yes, I know people will disagree with me, but I am right I am also moving this thread to where we usually discuss GUIs.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Campbell Ritchie wrote:Yes, I know people will disagree with me, but I am right I am also moving this thread to where we usually discuss GUIs.

Of course you're right. It's also an abomination for these books to jump into Swing GUI design so soon, probably before even going through arrays, collections, and other basics. I'm wondering how we can teach him enough so he can create a simple GUI given this demo code that uses only GridLayout (because that's all they've shown so far I'll bet) , allows the user to enter occupancy data for 10 apartments, and then displays the results. I think that this calls for smarter heads than mine.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
thanks for trying... I am doing this course thorugh correspondance.. and its crazy hard... I'll just send in my apartment program without the gui and hope i get some marks for doing atleast that much...
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Bryan Peach wrote:thanks for trying... I am doing this course thorugh correspondance.. and its crazy hard... I'll just send in my apartment program without the gui and hope i get some marks for doing atleast that much...


Don't give up yet. Just because I may not be the best teacher doesn't mean that someone else might not be able to help you. The key though is your putting in much effort to not just do the coding but learn the underlying concepts. Programming is unforgiving of errors, and is tough to learn, especially at the beginning, but get over that first hump and it becomes a joy to do. Best of luck.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
By the way, do you know how to do arrays? Because one way to do a portion of the GUI is to have an array of 10 JTextFields, each accepting a number from the user indicating how many people reside in that apartment. Other concepts you would need would be how to translate a String into an int. When your button is pushed, you could loop through all the jtextfields, getting the text held there and converting each one to an int using Integer.parseInt(...), then performing your calculations with these numbers.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
i know basically how to do arrays... I designed this array... but its my first one ever...lol so lets say beginners knowledge
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
You should find a brief introduction to arrays in this Java™ Tutorials section. Even though the Java Tutorials have much code in the main method ( ) they are still a really useful resource, which you ought to bookmark.
Pete has mentioned GridLayout; this is almost the easiest layout to understand. You can find details here. I suggest you use the constructor with 4 parameters, giving gaps of maybe 10 and 10 (last 2 parameters). The units are pixels. The first two parameters are how many columns and rows you want. You will have to check which number means row and which column. So if you specify new GridLayout(3, 3, 10, 10) you get space on screen for 9 Components arranged in three rows, with 10px gaps all around, and the Components are all the same size. It will probably look better with gaps than without.
Try the example on the API link I gave you earlier, then change it to add different numbers of buttons to that layout. There are more examples in the Java™ Tutorials.

If you are following Pete's suggestion you would want a 10-row GridLayout and add in turn JLabels ("Apartment " + number) and JTextfields ten times.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
pete stein wrote:I think that this calls for smarter heads than mine.
You're not doing badly.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

I hope you checked the book's website for possible errata. Sample code in books is quite frequently defective to some degree or other - either because it was incorrectly typeset of because the author fired off the code without actually testing it for the inevitable "gotchas".

Not that I've ever done anything like that.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GUI Big trouble
 
Similar Threads
How to write an algorithm for an average of values?
JButtons.... nothing to do with GUI
Cannot throw exception
Input Validation Help
Array and GUI