• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton Action Listener Problem

 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Head First Java and Analysis and Design.

I am able to create JButton, Panel and Frame.

I want to beable to Hit the JButton and Have it open another class.

I created a class that opens a text file in a Joption Pane.

I want to select that JButton and have it open my other class in the same
package.

I know I need Action Listner I just don't know how to make it all work.

any ideas

Thank You in advance
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
prateek chaudhari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Did it Very Corrrect

You need to have a ActionListener Class (A class which implements an ActionListener) which will listen to the Events, Here Gregg has done that with annonymous class.

This class will have a single method named actionPerformed(ActionEvent e) which will be invoked as and when the button pressed event is fired. You can add your functionality here.

After which you need to register that with the Action Generator here in this cas A JButton by using addActionListener() method.

That's it and you are there....
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this and it doesn't work.

Am I putting it in the wrong place or is this not right.

This cannot be this hard. I am missing something.

Thank You. Here is my code maybe that will help.

[ October 01, 2007: Message edited by: Gregg Bolinger ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your code I would infer that when pressing the bnJK button you could get the following on your console:

IT Worked

Is this happening? I guess the real question is what are you expecting the code you've shown?
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't even run the code.

I am getting bracket error or something is wrong with the below.
I thought it should be this simple to get the button to work.

Actually I want it to run a class and give me the results of that class.
But I think I should start with this simple example but it doesn't even work. I am confused.


bnJK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

System.out.println("IT Worked");
}
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright. Take a deep breath. Now, look at your code:



And now look at my code:



See the difference? Also, you don't need the extra public void actionPerformed method you have arbitrarily stuck in your code. And just for the sake of learning, you don't run a class. Typically you would refer to this as instantiating a class. So your statement would be:


Actually I want it to instantiate a class and give me the results of that class.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I took a breath (smile...) Thanks for that...I needed it.
It worked it printed out IT WORKED but then it gives me the below error.
Can I not have a actionPerformed and a action listener together???
Again Thank You very much for your help.

public void actionPerformed(ActionEvent e)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Collections.CollectionsJAC.actionPerformed(CollectionsJAC.java:90)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
[ October 01, 2007: Message edited by: Justin Charpenter ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you repost your code now that you have fixed a few things? Be sure and enclose it in UBB Code tags.

Thanks.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope my UBB is right.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want my main program that kicks the above program off let me know.

Thank you again
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Well, you've stumbled on something that can be a real pain when it comes to Swing. There's more than one way to skin a cat. What you have is not wrong per say. But it's unnecessary.

Your class implements ActionListener. This requires you to implement the actionPerformed method in the body of your class, which you have done. You've also added this class as the action listener for your button.

bnJK.addActionListener(this);

Perfectly fine. Then you added an anonymous inner class for your button with another event:



Again, perfectly fine. A component can have multiple event listeners attached to them. However, for your purpose, its pointless. You should just choose one over the other.

As far as your actual error is concerned I believe it might have something to do with:



Which I honestly am not sure what you are trying to do. You already have a visible frCollectionsJAC frame. But you are calling visible on it again. I'm not sure where the parent stuff is coming from since that code was not included.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling toString() on the result of e.getActionCommand()? That already returns a string (or null! hint hint!).
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my Main program
This program is opening the collection jpanel with a button on it
Now I want to click this button and open another jpanel that is reading data from a text file which I have working. I can read and write files but I can't get these buttons to work right.

 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob are you saying I should take out the too string and just leave it as

parent.CollectionsJAC = e.getActionCommand();
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.

As I said, getActionCommand() already returns a string (or null). Calling toString() on a string will just return the string itself. Calling toString() on a null reference however will throw a NullPointerException.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out toSTring on line 91 and I still get a line 91 error.

The Button works but I connot figure out why I am getting red (errors).

Any more suggestions. I hope one day I am giving the advice.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerExceptions (NPEs) are always caused because something before the . is null. Especially when chaining commands like e.getActionCommand().toString(), if the intermediate part (getActionCommand()) is null then it is harder to find.

If that line is still causing the NPE, then there are two probable causes:
1) parent is null
2) e is null

Seeing as 2) won't happen, parent has to be null. Seeing as you set it correctly in the constructor, it must be the value passed to the constructor. How do you instantiate your CollectionsJAC?
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I instantiate in my main program collectionsJAC = new CollectionsJAC(this);

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I just found it:

This line is commented out. As a result, the parent never gets set, and remains null.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the message I get when I uncomment it.



"The assignment to parent variable has no effect"

So I commented it out because it says it has no effect.

Then when I run the program I still get the line 91 error
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try renaming the constructor parameter from Parent to parent
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you already know this...

It really worked..

Thanks to you and Gregg I learned a few
things along the way. Thanks for explaining everything.

Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!