File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Buttons don Watch "Buttons don New topic
Author

Buttons don't work

Jeannette Lundstedt
Greenhorn

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!!

Thanks!


Here is what I have so far.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18


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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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
Greenhorn

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Buttons don't work
 
Similar Threads
combo box
Help with errors again
GUI
last week of class last assignment. Any help is appreciated.
One error in code..not understanding what it tells me. Code works it is on line 55.