Go Back   Java Programming Forums > Java Standard Edition Programming Help > What's Wrong With My Code?


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2010, 06:15 AM
Junior Member
 

Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
fari is on a distinguished road
Default ACTION LISTENER HANDLING

caN ANYONE HELP ME IN RUNNING MY CODE SO THAT WHENVER I PRESS ANY BUTTON IT DISPLAYS MESSSAGE IN TEXT FIELD...

Java Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



class NewClass1{
public static void main(String args[]){
  JFrame f =new JFrame("faria");
       JTextField t=new JTextField(10);
            JLabel l=new JLabel("");
            JButton j= new JButton("a");
            JButton j1= new JButton("b");
            JButton j2= new JButton("c");
            l.setLayout(new FlowLayout());
            f.add(l);
l.add(t);
            l.add(j);
             l.add(j1);
              l.add(j2);
j.addActionListener(new A());

j1.addActionListener(new B());
j2.addActionListener(new C());
f.setVisible(true);
f.setSize(400,400);




}
}
class A implements ActionListener{
   public void actionPerformed(ActionEvent e) {
      //  counter++;
      
      
         System.out.println("hiii" );

   }
        }
class B implements ActionListener{

   public void actionPerformed(ActionEvent e) {
  
            System.out.println("hello" );

   }
        }

   class C implements ActionListener{
   public void actionPerformed(ActionEvent e) {
    
            System.out.println("how r u" );
   }

        }



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 12-06-2010, 11:12 AM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,585
Thanks: 104
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Mellow
Default Re: ACTION LISTENER HANDLING

Hello fari,

Welcome to the Java Programming Forums

Please take a look at this link, im sure it will help.

How to Add ActionListener to a JButton. Java Swing
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #3 (permalink)  
Old 12-06-2010, 01:18 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
Default Re: ACTION LISTENER HANDLING

Or instead of using anonymous classes you could define the classes A, B, etc to implement the ActionListener interface and put the actionPerformed() method in them.
Reply With Quote
  #4 (permalink)  
Old 12-06-2010, 03:31 PM
Member
 

Join Date: May 2010
Posts: 38
Thanks: 1
Thanked 8 Times in 7 Posts
Lord.Quackstar is on a distinguished road

I'm feeling Hungover
Default Re: ACTION LISTENER HANDLING

And mind not using all caps? I know you need help but screaming isn't going to make it come any faster, as a matter of fact it might put people off
Reply With Quote
  #5 (permalink)  
Old 12-06-2010, 09:48 PM
javapenguin's Avatar
Member
 

Join Date: May 2010
Location: North Central Illinois
Posts: 236
Thanks: 95
Thanked 0 Times in 0 Posts
javapenguin is on a distinguished road

I'm feeling Cool
Send a message via AIM to javapenguin
Default Re: ACTION LISTENER HANDLING

Java Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
 import java.awt.Component;
 import java.awt.Container;
    import javax.swing.JPanel;
	   import java.awt.*;
   import java.awt.event.*;
	import java.io.*;
	import javax.swing.AbstractButton;
	import java.util.*;
	



class NewClass1 extends JFrame implements ActionListener{

 JTextField t;


 JLabel l;

private JButton j, j1, j2;

JFrame f;


public NewClass1()
{
 // JFrame f =new JFrame("faria");
       JTextField t= new JTextField(15);
            JLabel l=new JLabel("Label");
            JButton j= new JButton("a");
				j.addActionListener(this);
            JButton j1= new JButton("b");
				j1.addActionListener(this);
            JButton j2= new JButton("c");
				j2.addActionListener(this);
				t.setText("5");
         //    l.setLayout(new FlowLayout());
            // f.add(l);
// l.add(t);
         //   l.add(j);
          //   l.add(j1);
         //     l.add(j2);
				  
				   setTitle("farris");
				   Container pane = getContentPane();
				  
				   pane.add(l);
				 pane.add(t);
				  pane.add(j);
				  pane.add(j1);
		      pane.add(j2);
				
				
				  
// j.addActionListener(new A());

// j1.addActionListener(new B());
// j2.addActionListener(new C());
 pane.setLayout(new GridLayout(4,4));
setVisible(true);
setSize(400,400);
 setDefaultCloseOperation(EXIT_ON_CLOSE);



}

public static void main(String args[]){
 NewClass1 refVar = new NewClass1();
 
}

public void actionPerformed(ActionEvent e)
{

      if (e.getActionCommand().equals("a"))
      {
         System.out.println("hiii" );
			t.setText("hi");
			}
      else if (e.getActionCommand().equals("b"))
		{
             System.out.println("hello" );
       t.setText("hello");
   }
        

   else if (e.getActionCommand().equals("c"))
	{
            System.out.println("how r u" );
				t.setText("how r u");
   }

        }
		  
		  }
Code should work but for mysterious NullPointerExceptions.

at NewClass1.actionPerformed(NewClass1.java:98)
which is
t.setText("how r u");
Reply With Quote
  #6 (permalink)  
Old 12-06-2010, 09:54 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
Default Re: ACTION LISTENER HANDLING

What is the scope of the t variable assigned a value in the Constructor?
Reply With Quote
The Following User Says Thank You to Norm For This Useful Post:
javapenguin (12-06-2010)
  #7 (permalink)  
Old 12-06-2010, 10:07 PM
javapenguin's Avatar
Member
 

Join Date: May 2010
Location: North Central Illinois
Posts: 236
Thanks: 95
Thanked 0 Times in 0 Posts
javapenguin is on a distinguished road

I'm feeling Cool
Send a message via AIM to javapenguin
Default Re: ACTION LISTENER HANDLING

What was scope again? I forgot.

Do I have to assign it coordinates or something?
Reply With Quote
  #8 (permalink)  
Old 12-06-2010, 10:10 PM
Norm's Avatar
Senior Member
 

Join Date: May 2010
Location: SW Missouri
Posts: 788
Thanks: 0
Thanked 141 Times in 140 Posts
Norm will become famous soon enoughNorm will become famous soon enough

I'm feeling Fine
Default Re: ACTION LISTENER HANDLING

What text book are you using?
I bet you could get a definition using Google.

Nothing to do with coordinates.
Reply With Quote
The Following User Says Thank You to Norm For This Useful Post:
javapenguin (12-06-2010)
  #9 (permalink)  
Old 12-06-2010, 10:15 PM
javapenguin's Avatar
Member
 

Join Date: May 2010
Location: North Central Illinois
Posts: 236
Thanks: 95
Thanked 0 Times in 0 Posts
javapenguin is on a distinguished road

I'm feeling Cool
Send a message via AIM to javapenguin
Default Re: ACTION LISTENER HANDLING

I think scope is whether or not Java has access to something, or can find it at least.

Why isn't it finding it?

I'm baffled.
Reply With Quote
  #10 (permalink)  
Old 12-06-2010, 10:22 PM
javapenguin's Avatar
Member
 

Join Date: May 2010
Location: North Central Illinois
Posts: 236
Thanks: 95
Thanked 0 Times in 0 Posts
javapenguin is on a distinguished road

I'm feeling Cool
Send a message via AIM to javapenguin
Default Re: ACTION LISTENER HANDLING

Ok, it's not carrying over. Should I change it to 3 handlers?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Action Listener Suzanne What's Wrong With My Code? 7 29-05-2010 03:50 PM
Beginner Help (Action Listener) gradstudent AWT / Java Swing 2 30-04-2010 03:26 PM
Action Listener kray66 AWT / Java Swing 2 19-04-2010 08:26 PM
Problem with Action Listener JonoScho AWT / Java Swing 4 19-03-2010 05:03 AM
Need Help Action Listener.... vhizent23 AWT / Java Swing 2 09-10-2009 06:46 PM


100 most searched terms
Search Cloud
action listener in java actionlistener actionlistener in java addactionlistener addactionlister adding elements to two dimensional arraylist arraylist value into hash cannot find symbol method create an abstract class called shape with abstract methods+java double format java double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread main java.lang.outofmemoryerror java heap space in eclipse exception in thread main java.lang.reflect.invocationtargetexception format double java get mouse position java how to convert list to map in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java coding - user entered input java double format java double to int java font attributes java format double java forum java forums java get mouse position java grect center java jbutton java programming codes using astirisks java programming forum java programming forums java.lang.outofmemoryerror: java heap space java.lang.reflect.invocationtargetexception jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextfield output from formula jxl.read.biff.biffexception: unable to recognize ole stream mean value decimal double java programmer forum scanner char smack api messagelistener transaction using gui and 2 dimensional array code in java two dimensional arraylist in java

All times are GMT. The time now is 09:28 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.