Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ActionListener Help?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener Help?

    I am trying to add the ActionListener coding to my code, but i can't seem to figure out the rest of the code, am i missing something, i'll provide my code and my attempted ActionListener coding that i tried implementing in there, any help would be nice, thanks.

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class Keypad extends JFrame implements ActionListener {
    private static final int i = 1;
     
    public Keypad() {
     
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(4, 3, 5, 5));
     
    //Adding Buttons
    for (int i = 1; i<= 9; i++) {
    p1.add (new Button("" + i));
    }
     
    p1.add(new JButton("*"));
    p1.add(new JButton("" + 0));
    p1.add(new JButton("#"));
     
     
    JPanel p2 = new JPanel(new BorderLayout());
    p2.add(new JTextField(""),
    BorderLayout.NORTH);
    p2.add(p1, BorderLayout.CENTER);
     
    add(p2, BorderLayout.WEST);
    add(new JButton("Clear"),
    BorderLayout.CENTER);
    }
     
                  //Main Method
    public static void main(String[] args){
    Keypad frame = new Keypad();
    frame.setTitle("Phone");
    frame.setSize(300, 350);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }

    Here is my attempted ActionListener Coding: (it doesn't work though)
    String[] btnLabels = "1,2,3,4,5,6,7,8,9,0,#,* ".split(","); {
    	for( int i = 1; i< btnLabels.length; i++ );
    	JButton btn = new JButton( btnLabels[i] );
    	btn.addActionListener( this );
    	add( btn );
    	}
     
    	public void actionPerformed( ActionEvent evt ) {
    	if( evt.getSource() instanceof JButton ) {
    	JButton btn = (JButton)evt.getSource();
    	AbstractButton label = null;
    	if( btn.getText().equalsIgnoreCase("clear"))
    	label.setText("");
    	}
    	}
    Last edited by helloworld922; March 30th, 2010 at 08:22 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ActionListener Help?

    What exactly is the problem? You must add the listener to whichever component you want to respond, for example
    for (int i = 1; i<= 9; i++) {
    JButton button = new JButton("" + i);
    button.addActionListener(this);
    p1.add (button);
    }

Similar Threads

  1. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  2. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM
  3. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  4. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM
  5. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM