[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with event listeners

Posted on 2009-02-09
8
Medium Priority
?
311 Views
Last Modified: 2013-11-23
I am having a problem adding action listeners to my code. The code worked before i tried adding them but I need the numbers to appear when clicked on.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
 
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
 
 
public class celluar extends JFrame implements ActionListener{
    // creates borders for display
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Celluar");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
 
        panel.setBackground(Color.black);
 
   // creates and add buttons for keypad
 
        JButton b1 = new JButton("1");
        panel.add(b1);
        b1.addActionListener(this);
        b1.setActionCommand("1");
 
        JButton b2 = new JButton("2");
        panel.add(b2);
        b2.addActionListener(this);
         b2.setActionCommand("2");
 
       JButton b3 = new JButton("3");
        panel.add(b3);
        b3.addActionListener(this);
         b3.setActionCommand("3");
 
       JButton b4 = new JButton("4");
        panel.add(b4);
        b4.addActionListener(this);
         b4.setActionCommand("4");
 
       JButton b5 = new JButton("5");
        panel.add(b5);
        b5.addActionListener(this);
         b5.setActionCommand("5");
 
       JButton b6 = new JButton("6");
        panel.add(b6);
        b6.addActionListener(this);
         b6.setActionCommand("6");
 
       JButton b7 = new JButton("7");
        panel.add(b7);
        b7.addActionListener(this);
         b7.setActionCommand("7");
 
       JButton b8 = new JButton("8");
        panel.add(b8);
        b8.addActionListener(this);
         b8.setActionCommand("8");
 
        JButton b9 = new JButton("9");
        panel.add(b9);
        b9.addActionListener(this);
         b9.setActionCommand("9");
 
        JButton b10 = new JButton("*");
        panel.add(b10);
        b10.addActionListener(this);
         b10.setActionCommand("*");
 
        JButton b11 = new JButton("0");
        panel.add(b11);
        b11.addActionListener(this);
         b11.setActionCommand("0");
 
       JButton b12 = new JButton("#");
        panel.add(b12);
        b12.addActionListener(this);
         b12.setActionCommand("#");
 
 
 
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel,BorderLayout.CENTER);
 
        JTextField number = new JTextField();
        number.setColumns(15);
        frame.getContentPane().add(number,BorderLayout.NORTH);
 
        System.out.println(frame.getContentPane().getLayout());
        System.out.println(frame.getContentPane().getComponentCount());
        frame.pack();
        frame.setVisible(true);
 
    }
}

Open in new window

0
Comment
Question by:bdiddy05
  • 4
  • 2
  • 2
8 Comments
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 23592798
Two things:

First, you say "implements ActionListener", but you don't have an actionPerformed method in the class.
Second, you don't have a reference to "this" in your main method. You need to move most of the code to the constructor of your class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
 
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
 
 
public class celluar extends JFrame implements ActionListener{
    // creates borders for display
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Celluar");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    public celluar() {  
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
 
        ... 
 
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(panel,BorderLayout.CENTER);
 
        JTextField number = new JTextField();
        number.setColumns(15);
        this.getContentPane().add(number,BorderLayout.NORTH);
 
        System.out.println(this.getContentPane().getLayout());
        System.out.println(this.getContentPane().getComponentCount());
        this.pack();
        this.setVisible(true);
 
    }
 
    public void actionPerformed(ActionEvent evt) {
        // Perform your actions here
    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 23592810
main method should be:
    public static void main(String[] args) {
        JFrame frame = new celluar();
        frame.setTitle("Celluar");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 23592822
You had a number of errors - see below:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
 
 
public class celluar extends JFrame implements ActionListener{
    // creates borders for display
 
    public static void main(String[] args) {
	celluar c = new celluar();
    }
    public celluar() {
 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JPanel panel = new JPanel();
	panel.setLayout(new GridLayout(4, 3));
 
	panel.setBackground(Color.black);
 
	// creates and add buttons for keypad
 
	JButton b1 = new JButton("1");
	panel.add(b1);
	b1.addActionListener(this);
	b1.setActionCommand("1");
 
	JButton b2 = new JButton("2");
	panel.add(b2);
	b2.addActionListener(this);
	b2.setActionCommand("2");
 
	JButton b3 = new JButton("3");
	panel.add(b3);
	b3.addActionListener(this);
	b3.setActionCommand("3");
 
	JButton b4 = new JButton("4");
	panel.add(b4);
	b4.addActionListener(this);
	b4.setActionCommand("4");
 
	JButton b5 = new JButton("5");
	panel.add(b5);
	b5.addActionListener(this);
	b5.setActionCommand("5");
 
	JButton b6 = new JButton("6");
	panel.add(b6);
	b6.addActionListener(this);
	b6.setActionCommand("6");
 
	JButton b7 = new JButton("7");
	panel.add(b7);
	b7.addActionListener(this);
	b7.setActionCommand("7");
 
	JButton b8 = new JButton("8");
	panel.add(b8);
	b8.addActionListener(this);
	b8.setActionCommand("8");
 
	JButton b9 = new JButton("9");
	panel.add(b9);
	b9.addActionListener(this);
	b9.setActionCommand("9");
 
	JButton b10 = new JButton("*");
	panel.add(b10);
	b10.addActionListener(this);
	b10.setActionCommand("*");
 
	JButton b11 = new JButton("0");
	panel.add(b11);
	b11.addActionListener(this);
	b11.setActionCommand("0");
 
	JButton b12 = new JButton("#");
	panel.add(b12);
	b12.addActionListener(this);
	b12.setActionCommand("#");
 
 
 
	getContentPane().setLayout(new BorderLayout());
	getContentPane().add(panel,BorderLayout.CENTER);
 
	JTextField number = new JTextField();
	number.setColumns(15);
	getContentPane().add(number,BorderLayout.NORTH);
 
	pack();
	setVisible(true);
 
    }
 
    public void actionPerformed(ActionEvent e) {}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bdiddy05
ID: 23592945
CEHJ,

I copied and pasted your code. The applet came up, but when I click on a button, the number is not displaying.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23593138
The ActionListener has not been connected - you'll have to do that. It wasn't even present in your original code
0
 

Author Comment

by:bdiddy05
ID: 23593202
how would i go about connecting the actionlistener?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23594555
Something like
public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();
    if (o instanceof JButton) {
         number.setText(e.getActionCommand());
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23599821
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question