how to call a class after clicking a button from jFrame

I am new to netbeans, I am looking of somebody who could teach me on how to call a class after clicking a button from jFrame even just to have an output in the console "Hello World", System.out.printlnThank you...
[227 byte] By [_onmosh_a] at [2007-11-27 2:42:10]
# 1

implement ActionListener interface and define actionPerformed method in your class.

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

System.out.println("Hello World");

}

}

AnanSmritia at 2007-7-12 3:06:34 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you so much for your immediate reply, AnanSmriti

Probably you were not able to get my point. Please allow to explain it again.

I have a jFrame with jButton1. After I click the button using actionperformed event it will call the class.

The content of the class basically the method with a content of System.out.println("Hello World!!!").

This is what I am doing right now but if you think I am doing the wrong thing I may try whatever you may wish to instruct me.

Thank you again.

_onmosh_a at 2007-7-12 3:06:34 > top of Java-index,Java Essentials,New To Java...
# 3

Actually this is the error...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at javaapplication3.NewJFrame.jButton1ActionPerformed(NewJFrame.java:58)

at javaapplication3.NewJFrame.access$000(NewJFrame.java:13)

at javaapplication3.NewJFrame$1.actionPerformed(NewJFrame.java:33)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

_onmosh_a at 2007-7-12 3:06:34 > top of Java-index,Java Essentials,New To Java...
# 4
I created javaapplication3 project then with 1 package, under to that is 1 jFrame and 1 jClass. I created 1 jbutton on the jFrame to be used for calling the class and the event is actionperformed.
_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 5

Thank you so much for your immediate reply, AnanSmriti

Probably you were not able to get my point. Please allow to explain it again.

I have a jFrame with jButton1. After I click the button using actionperformed event it will call the class.

The content of the class basically the method with a content of System.out.println("Hello World!!!").

This is what I am doing right now but if you think I am doing the wrong thing I may try whatever you may wish to instruct me.

Thank you again.

_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 6

Actually this is the error...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at javaapplication3.NewJFrame.jButton1ActionPerformed(NewJFrame.java:58)

at javaapplication3.NewJFrame.access$000(NewJFrame.java:13)

at javaapplication3.NewJFrame$1.actionPerformed(NewJFrame.java:33)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 7
I created javaapplication3 project then with 1 package, under to that is 1 jFrame and 1 jClass. I created 1 jbutton on the jFrame to be used for calling the class and the event is actionperformed.
_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 8
yes i misunderstood your post :). It is better if you could post your code.
AnanSmritia at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 9

code under class:

public void out(){

System.out.println("Hello World!!!");

}

code under jframe:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

haha.out();

}

private javaapplication3.NewClass haha;

_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 10
it would be possible for me to ask your email address so that I can send my project so that you could figure it out if it is okay for you... or YM I'm online right now....tnx...thank you so much...
_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 11

I assume your class haha has a method out and it prints Hello World on the console.

If my understanding is correct , please check the following code.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

haha obj = new haha();

obj.out();

}

AnanSmritia at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 12
// TODO add your handling code here://haha.out();haha obj = new haha(); /////// there is an error on this lineobj.out();
_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 13

I am not sure What you have done in your code.

//[b] class 1 [/b]

package example;

public class GUIExample extends JFrame implements ActionListener {

public GUIExample() {

//TODO

//necessary initialization and components added to frame

JButton clickBtn = new JButton("Click");

clickBtn.addActionListener(this);

}

public void actionPerformed(ActionEvent ae ) {

MsgClass obj = new MsgClass();

obj.displayData();

}

public static void main() {

}

// [b] class 2 i.e MsgClass [/b]

class MsgClass {

public void displayData() {

System.out.println("Hello World");

}

}

AnanSmritia at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 14
thanks...i'll try this...one...but i just want to ask do i need to use the main class or it is okay also if i'm going to use another class, second how to put the implements actionlistener on the jFrame class, i'm just going to encode it or is there any other instruction? tnx..
_onmosh_a at 2007-7-12 3:06:35 > top of Java-index,Java Essentials,New To Java...
# 15

You can use it another class and create an instance for that class in your main class and call the method in actionPerformed method.

You can implement the ActionListener interface just given in previous example or

write like this in your frame class

JButton btn = new JButton("Click");

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

//todo

}

};

AnanSmritia at 2007-7-21 20:30:46 > top of Java-index,Java Essentials,New To Java...
# 16
thanks...i'll try this...one...but i just want to ask do i need to use the main class or it is okay also if i'm going to use another class, second how to put the implements actionlistener on the jFrame class, i'm just going to encode it or is there any other instruction? tnx..
_onmosh_a at 2007-7-21 20:30:46 > top of Java-index,Java Essentials,New To Java...
# 17

thank you so much for everything....but I need to go its already 1 am here in our place and I need to take a ride for a very long way so until then my friend, thank you so much again if you want to keep in touch with me you can contact me through this email address sarzilla3000@yahoo.com hopefully you will give me also your email add and a million hopefully this would be a good start for our friendship....thank you so much again and have a nice day.

_onmosh_a at 2007-7-21 20:30:46 > top of Java-index,Java Essentials,New To Java...
# 18
you can get my email id from my profile. all the best for your learning.
AnanSmritia at 2007-7-21 20:30:46 > top of Java-index,Java Essentials,New To Java...