We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to get the keypad numbers to display

bdiddy05
bdiddy05 asked
on
Medium Priority
162 Views
Last Modified: 2013-11-23
I have the following code, but I am unable to get the numbers to display when they are clicked.
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.GRAY);
 
	// creates and add buttons for keypad
 
	JButton b1 = new JButton("1");
	panel.add(b1);
	b1.addActionListener(this);
	b1.setActionCommand("1");
	b1.setBackground(Color.BLUE);
 
	JButton b2 = new JButton("2");
	panel.add(b2);
	b2.addActionListener(this);
	b2.setActionCommand("2");
	b2.setBackground(Color.BLUE);
 
	JButton b3 = new JButton("3");
	panel.add(b3);
	b3.addActionListener(this);
	b3.setActionCommand("3");
	b3.setBackground(Color.BLUE);
 
	JButton b4 = new JButton("4");
	panel.add(b4);
	b4.addActionListener(this);
	b4.setActionCommand("4");
	b4.setBackground(Color.BLUE);
 
	JButton b5 = new JButton("5");
	panel.add(b5);
	b5.addActionListener(this);
	b5.setActionCommand("5");
	b5.setBackground(Color.BLUE);
 
	JButton b6 = new JButton("6");
	panel.add(b6);
	b6.addActionListener(this);
	b6.setActionCommand("6");
	b6.setBackground(Color.BLUE);
 
	JButton b7 = new JButton("7");
	panel.add(b7);
	b7.addActionListener(this);
	b7.setActionCommand("7");
	b7.setBackground(Color.BLUE);
 
	JButton b8 = new JButton("8");
	panel.add(b8);
	b8.addActionListener(this);
	b8.setActionCommand("8");
	b8.setBackground(Color.BLUE);
 
	JButton b9 = new JButton("9");
	panel.add(b9);
	b9.addActionListener(this);
	b9.setActionCommand("9");
	b9.setBackground(Color.BLUE);
 
	JButton b10 = new JButton("*");
	panel.add(b10);
	b10.addActionListener(this);
	b10.setActionCommand("*");
	b10.setBackground(Color.BLUE);
 
	JButton b11 = new JButton("0");
	panel.add(b11);
	b11.addActionListener(this);
	b11.setActionCommand("0");
	b11.setBackground(Color.BLUE);
 
 
	JButton b12 = new JButton("#");
	panel.add(b12);
	b12.addActionListener(this);
	b12.setActionCommand("#");
	b12.setBackground(Color.BLUE);
 
 
 
 
 
	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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>public void actionPerformed(ActionEvent e) {}

As i mentioned in your previous question, your ActionListener is empty
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
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
 
    JTextField number = new JTextField();

    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.GRAY);
 
        // creates and add buttons for keypad
 
        JButton b1 = new JButton("1");
        panel.add(b1);
        b1.addActionListener(this);
        b1.setActionCommand("1");
        b1.setBackground(Color.BLUE);
 
        JButton b2 = new JButton("2");
        panel.add(b2);
        b2.addActionListener(this);
        b2.setActionCommand("2");
        b2.setBackground(Color.BLUE);
 
        JButton b3 = new JButton("3");
        panel.add(b3);
        b3.addActionListener(this);
        b3.setActionCommand("3");
        b3.setBackground(Color.BLUE);
 
        JButton b4 = new JButton("4");
        panel.add(b4);
        b4.addActionListener(this);
        b4.setActionCommand("4");
        b4.setBackground(Color.BLUE);
 
        JButton b5 = new JButton("5");
        panel.add(b5);
        b5.addActionListener(this);
        b5.setActionCommand("5");
        b5.setBackground(Color.BLUE);
 
        JButton b6 = new JButton("6");
        panel.add(b6);
        b6.addActionListener(this);
        b6.setActionCommand("6");
        b6.setBackground(Color.BLUE);
 
        JButton b7 = new JButton("7");
        panel.add(b7);
        b7.addActionListener(this);
        b7.setActionCommand("7");
        b7.setBackground(Color.BLUE);
 
        JButton b8 = new JButton("8");
        panel.add(b8);
        b8.addActionListener(this);
        b8.setActionCommand("8");
        b8.setBackground(Color.BLUE);
 
        JButton b9 = new JButton("9");
        panel.add(b9);
        b9.addActionListener(this);
        b9.setActionCommand("9");
        b9.setBackground(Color.BLUE);
 
        JButton b10 = new JButton("*");
        panel.add(b10);
        b10.addActionListener(this);
        b10.setActionCommand("*");
        b10.setBackground(Color.BLUE);
 
        JButton b11 = new JButton("0");
        panel.add(b11);
        b11.addActionListener(this);
        b11.setActionCommand("0");
        b11.setBackground(Color.BLUE);
 
 
        JButton b12 = new JButton("#");
        panel.add(b12);
        b12.addActionListener(this);
        b12.setActionCommand("#");
        b12.setBackground(Color.BLUE);
 
 
 
 
 
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
 
        number.setColumns(15);
        getContentPane().add(number,BorderLayout.NORTH);
 
        pack();
        setVisible(true);
 
    }
 
    public void actionPerformed(ActionEvent e) {
          number.setText(number.getText()+e.getActionCommand());
    }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.