This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Buttons don't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Buttons don Watch "Buttons don New topic

Buttons don't work

Jeannette Lundstedt

Joined: Jan 07, 2008
Posts: 2
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!!


Here is what I have so far.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20078

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.

How To Ask Questions How To Answer Questions
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 875
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);".

bitguru blog
Jeannette Lundstedt

Joined: Jan 07, 2008
Posts: 2
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!!!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Buttons don't work
jQuery in Action, 3rd edition