Solved

In the numeric keypad program, if i press sum number it should be displayed in the label in the top white space.  How do i do that? Please some one help me asap

Posted on 2009-03-29
6
442 Views
Last Modified: 2012-08-13
In the numeric keypad program, if i press sum number it should be displayed in the label in the top white space.  How do i do that? Please some one help me asap
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class NumericKeyPad extends JPanel {
    //-----------------------------------------------------------------
    //  Creates several bordered panels and displays them.
    //-----------------------------------------------------------------
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Numeric Keypad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel.setBackground(Color.white);
 
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b11 = new JButton("");
        JButton b0 = new JButton("0");
        JButton b12 = new JButton("");
 
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
        panel.add(b11);
        panel.add(b0);
        panel.add(b12);
        
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel,BorderLayout.CENTER);
        JButton clearBt = new JButton("Clear");
        JTextField number = new JTextField();
        number.setColumns(15);
        frame.getContentPane().add(number,BorderLayout.NORTH);
        frame.getContentPane().add(clearBt,BorderLayout.EAST);
 
        frame.pack();
        frame.setVisible(true);
 
    }
    
}

Open in new window

0
Comment
Question by:deeppatel1986
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24015800
you need to use an action listener.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class NumericKeyPad extends JPanel {
    //-----------------------------------------------------------------
    //  Creates several bordered panels and displays them.
    //-----------------------------------------------------------------
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Numeric Keypad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel.setBackground(Color.white);
 
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b11 = new JButton("");
        JButton b0 = new JButton("0");
        JButton b12 = new JButton("");
 
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
        panel.add(b11);
        panel.add(b0);
        panel.add(b12);
       
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel,BorderLayout.CENTER);
        JButton clearBt = new JButton("Clear");
        final JTextField number = new JTextField();
        number.setColumns(15);
        frame.getContentPane().add(number,BorderLayout.NORTH);
        frame.getContentPane().add(clearBt,BorderLayout.EAST);
 
        // create an action listener that appends the number pressed to the text field
       
        ActionListener actionListener = new ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                        
                        // get the number entered
                        
                        String digit = e.getActionCommand();
                        
                        // append number to the text field
                        
                        number.setText(number.getText()+digit);
                  }
              
        };
       
        // add action listeners to buttons

        b1.addActionListener(actionListener);
        b2.addActionListener(actionListener);
        // same for other number buttons
       
        frame.pack();
        frame.setVisible(true);
 
    }
   
}

http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24015817

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 import java.awt.event.MouseAdapter;
 
import java.awt.event.MouseEvent;
public class NumericKeyPad extends JFrame {
    //-----------------------------------------------------------------
    //  Creates several bordered panels and displays them.
    //-----------------------------------------------------------------
    int preInt=0;
        JButton clearBt = new JButton("Clear");
        JTextField number = new JTextField();
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b11 = new JButton("+");
        JButton b0 = new JButton("0");
        JButton b12 = new JButton("=");
    
    public NumericKeyPad(){
        JFrame frame = new JFrame("Numeric Keypad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel.setBackground(Color.white);
            b1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "1");
                }
            });
            b2.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "2");
                }
            });
            b3.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "3");
                }
            });
            b4.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "4");
                }
            });
            b5.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "5");
                }
            });
            b6.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "6");
 
                }
            });
            b7.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "7");
                }
            });
            b8.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "8");
                }
            });
            b9.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(number.getText() + "9");
                }
            });
             b11.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    preInt = Integer.parseInt(number.getText());
                    number.setText("");
                }
            });
            b12.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText(String.valueOf((preInt + Integer.parseInt(number.getText()))));
                }
            });
            clearBt.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    number.setText("");
                    preInt= 0;
                }
            });
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
        panel.add(b11);
        panel.add(b0);
        panel.add(b12);
        
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel,BorderLayout.CENTER);
        number.setColumns(15);
        frame.getContentPane().add(number,BorderLayout.NORTH);
        frame.getContentPane().add(clearBt,BorderLayout.EAST);
 
        frame.pack();
        frame.setVisible(true);    
    
    }
    
    
    public static void main(String[] args) {
        new NumericKeyPad();
 
 
    }
    
}

Open in new window

0
 
LVL 2

Expert Comment

by:mhaq_java
ID: 24016591
Following link can be helpful.
make a generic listener for all butons

http://www.roseindia.net/java/example/java/awt/KeyPress.shtml
0
Industry Leaders: 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:deeppatel1986
ID: 24024420
Hey savan i need actionListener for the clear button. Please help ASAP
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class NumericKeyPad extends JPanel {
    //-----------------------------------------------------------------
    //  Creates several bordered panels and displays them.
    //-----------------------------------------------------------------
int preInt=0;
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Numeric Keypad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 3));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
        panel.setBackground(Color.white);
 
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b11 = new JButton("");
        JButton b0 = new JButton("0");
        JButton b12 = new JButton("");
 
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
        panel.add(b11);
        panel.add(b0);
        panel.add(b12);
        
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(panel,BorderLayout.CENTER);
        JButton clearBt = new JButton("Clear");
        final JTextField number = new JTextField();
        
        number.setColumns(15);
        frame.getContentPane().add(number,BorderLayout.NORTH);
        frame.getContentPane().add(clearBt,BorderLayout.EAST);
 
        ActionListener actionListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                // get the number entered
                String digit = e.getActionCommand();
                // append number to the text field
                number.setText(number.getText()+digit);
            }
        };
        ActionListener actionListener1 = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                    //String clear = e.getActionCommand();
                    number.setText("");
            }
        };
        b1.addActionListener(actionListener);
        b2.addActionListener(actionListener);
        b3.addActionListener(actionListener);
        b4.addActionListener(actionListener);
        b5.addActionListener(actionListener);
        b6.addActionListener(actionListener);
        b7.addActionListener(actionListener);
        b8.addActionListener(actionListener);
        b9.addActionListener(actionListener);
        b11.addActionListener(actionListener);
        b0.addActionListener(actionListener);
        b12.addActionListener(actionListener);
        clearBt.addActionListener(actionListener);
 
        frame.pack();
        frame.setVisible(true);
    }
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24024428
>         clearBt.addActionListener(actionListener);

change to:

        clearBt.addActionListener(actionListener1);

0
 

Author Comment

by:deeppatel1986
ID: 24024471
Thanks a lot
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 465
Java Restore security prompts not working 10 259
printf performancy 11 103
junit example issue 2 56
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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