implement ActionListener interface and define actionPerformed method in your class.
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Hello World");
}
}
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.
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)
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.
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)
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;
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();
}
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");
}
}
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
}
};
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.