• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buttons don't work

 
Jeannette Lundstedt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • 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
  • 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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic