• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButtons.... nothing to do with GUI

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what sort of Component are you displaying the results on?
 
reply
    Bookmark Topic Watch Topic
  • New Topic