• 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

Buttons don't work

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am taking a Java class currently and we are learning swing. I am creating a mortgage gui using swing. I have created a very nice looking gui but the buttons don't work. I have looked at 4 differenct books on how to create actionListeners etc. and it looks like they should work but they don't. I hope I have added my code correctly if not I apologize!!

Thanks!


Here is what I have so far.
 
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

Here you are creating new, local variables with the same names as your instance variables, and assigning the action listeners to those.
Also, your tossing those buttons away too, since later on you create yet again new buttons:


So, what you need to do:

1) remove lines 2-4 of your go method. Your creating new buttons here which you will throw away immediately

2) remove the JButton in front of calculate, reset, exit, option1, option2 and option3 in the go method. This will make you initialize the instance variables, not new local variables.

3) Add calculate, reset, exit, option1, option2 and option3 to panela and panelb instead of yet again new buttons.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeannette Lundstedt:
I have created a very nice looking gui but the buttons don't work.



At line E you are checking for equality with the variable at line A, however that variable is never set to anything.

At line B you create a button (and assign it to a local variable that has the same name as, but is different from, the one at line A) and add a listener to it. But you never add this button to a container.

At line C you create another button (with the same text as, but different from, the one at line B) and add it to a container. It is this button you are seeing in your GUI, but this is not the button you added the listener to.

If you change line B to "option1 = new JButton("7 yrs at 5.35%");" without the "JButton" at the beginning, and you change line C to "panelb.add(option1);" then your listener will be called when the user presses the button.

Unfortunately, at line D your listener is calling getActionCommand(), not getSource(), so it is comparing the wrong object with 'option1' at line E. Either change line D to "Object source = e.getSource();" or change line E to "if (source.equals("7 yrs at 5.35%")) {" [but then the variable name 'source' is misleading] and then the code at line F will actually run when the user presses the button.

----

While I'm here, let me make some other comments:

(1) Usually you would call frame.pack() or frame.setSize(w, h) but not both.

(2) What's with the windowActivated() / windowClosed() / windowClosing() / exit() methods? Something like that would work if you declared to implement WindowListener and called addWindowListener(), but that's the hard way. The easy way is to call "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);".
 
Jeannette Lundstedt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are awesome!! Thank you soooo much!!! I guess I got a little button crazy. I found another problem I had and now the buttons work!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic