wood burning stoves*
The moose likes Beginning Java and the fly likes JButtons.... nothing to do with GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JButtons.... nothing to do with GUI" Watch "JButtons.... nothing to do with GUI" New topic
Author

JButtons.... nothing to do with GUI

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Hey... so i have to create this application that i create an array and the array represents an apartment building. I have to read in the location in the array which represents the apartment number, as well as the number of occupants in the apartment. This all has to be done through GUI. Here is what I have so far...

The last line where I call the actionPerformed method is giving me an illegal start of expression error.... any ideas?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

When calling a method you don't specify the type of the parameter. That's why you get this error.

Also, this is not how you call the actionPerformed method. You should keep a reference to the JButton you addn, then call addActionListener(someApartment) on that JButton reference:
Now, when you click on the button, Swing will create an ActionEvent and call someApartment.actionPerformed for you. Also, notice how I added the ActionListener before showing the JFrame. When you show the JFrame, the user can already interact with it. You really want to have it finished by then.


Oh, and please use a meaningful subject line. "GUI" doesn't really give us any information what this problem is about. The compiler error isn't even related to the GUI code but to an invalid statement.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok so when i add the ActionListener like this...

I get an error saying that I cannot reference the non-static variable this from a static context... But i don't know how to fix this... any thoughts?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

Apparently you have just started programming in Java; the two mistakes you've made so far are really basic errors. I'm moving this to Beginning Java.

Look for a tutorial about the use of static vs non-static.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Rob Prime wrote: . . . Look for a tutorial about the use of static vs non-static.
Or search my posts from yesterday because I wrote twice about things static.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok... So i think i might have gotten around the static non-ststic thing... but now when i try to compile i get this error: variable submit might not have been initialized. It comes from this chunk of code..

I'm really new to java and even newer to GUI... so dealing with buttons and actionListeners and stuff all seems to be beyond me... but i need to get this done... Thanks for the help..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Bryan,
Did you notice Rob requested you to provide a meaningful subject line?
You can do so by clicking on the on your original post


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

As I've said before, this has nothing to do with GUI programming. Uninitialized local variables is basic Java 101.

You have a variable of type Button (which should be JButton) which is never assigned. You never include that button in your GUI. You attach an ActionListener to it but since it's not in the GUI that won't make any difference.
On the other hand you have a new JButton that is part of the GUI but does not have an ActionListener attached. Can you find a way to combine the unnamed JButton and the uninitialized JButton variable?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
If i knew what i was doing would I be on here asking questions? I thought this was supposed to be a friendly place for greenhorns??? If its not obvious I am a greenhorn.. So I'm sorry if I'm asking stupid questions...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

You may be a greenhorn, but you also don't read posts. The very first reply in this thread has all the code you need to fix your original code, but that must've slipped through. After that, I've given you the hint to look at static vs non-static, and my last post even gave you a hint on how to solve it yourself. IF you followed that hint you would have gotten to the code from post #2 on your own.

And you're not asking stupid questions, we just prefer to let you find the answers yourself so you learn something from it.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Sorry about that little outburst... Sometimes i just get so frustrated and don't understand what is being said to me and you guys all make it sound so easy and straightforward... So i finally got it to work. Except after i submit my first round of input it prints those results on a new container nd waits for more input... But when i enter a nother round of input its prints the old resluts AND the new results. I just want it to update the old results with the new results... Does that make any sense?
For example if i enter apartment number 1 and 5 occupants the first time it will show me that the average is 5... thats correct... but when i enter apartment 2 and 15 occupants i get two read outs for average... one says 5 from the first input and the other says 15 from the second input... here is all the updated code..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Why haven't you got rid of all the static declarations?
You will have to move all the GUI-building code out of the main method. It belongs either in the constructor or in a private void setUpGUI() method which is called from the constructor. You will now have to work out which constructor.

But before you try adding a GUI, you need to ensure that the logic works in the first place. Have you any way to calculate the average of whatever it is you are averaging out? Can you display it at the command line/terminal? Have you tested it and does it still work if you throw rubbish information to it? When you can answer "yes" to all those questions, the consider getting a GUI together.
Don't try doing the whole thing in one stage (unless you are very experienced). Get a GUI together which gives the correct appearance. Don't add any functionality or Listeners yet. That can be done later.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
I'm not sure i understand what you are saying... I compiled all my methods and tested them before i started doing the gui stuff. So as far as formulas and methods they all do when i need them to do. Is there more that I have to do to the GUI stuff? The GUI is laid out the way i want it and it works and compiles just fine... It's just that I am getting duplicate outputs... Like instead of adding apartment one and apartment two and averaging them it is just averaging one apartment at a time and printing a result for each apartment... not a tally...
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Actually... nevermind my last post... What is happening is everytime i add new data it is doing all the calculation correctly... but it doesn't refresh the results...
For example:
first run apartment #1, Occupants 10
it'll print...

Total occupants:10
Average occupants in building: 1
Apartments above average: 1
paartments below average: 9

so second run is apartment #2, occupants: 20
it'll print...
Total occupants:10
Average occupants in building: 1
Apartments above average: 1
paartments below average: 9
Total occupants:30
Average occupants in building: 3
Apartments above average: 2
paartments below average: 8

So i get the first output plus the second output... The output is correct... but i just want it to keep refreshing.. so for output #2 just print that info and forget about the first input... Does that makes sense???

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
So, you have some Component where you want to display the results?
Have you found the repaint methods, or in the text components, setText?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
yeah after i hit the submit button the computer calculates the results and prints them to the GUI window i'm working on.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
And what sort of Component are you displaying the results on?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButtons.... nothing to do with GUI