Event Handling

Event Handling

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class Calculator extends JFrame
{
JLabel lblNumber=new JLabel("Number");
JLabel lblSquareRoot=new JLabel("Square Root");
JLabel lblCubicRoot=new JLabel("Cuboc Root");
JLabel lblRound=new JLabel("Round");

JTextField txtNumber=new JTextField();
JTextField txtSquareRoot=new JTextField();
JTextField txtCubicRoot=new JTextField();
JTextField txtRound=new JTextField();

JButton btnClose=new JButton("Close");
JButton btnClear=new JButton("Clear");


public Calculator()
{
Container c=getContentPane();
c.setLayout(new GridLayout(0,2));
c.add(lblNumber);
c.add(txtNumber);
c.add(lblSquareRoot);
c.add(txtSquareRoot);
c.add(lblCubicRoot);
c.add(txtCubicRoot);
c.add(lblRound);
c.add(txtRound);
c.add(btnClose);
c.add(btnClear);

pack();
setVisible(true);


txtNumber.addKeyListener(new KeyListener()
{
public void keyReleased(KeyEvent ke);
{
double number=Double.parseDouble(txtNumber.getText());
txtSquareRoot.setText(""+Math.sqrt(number));
txtCubicRoot.setText(""+Math.cbrt(number));
txtRound.setText(""+Math.round(number));
}


public void keyPressed(KeyEvent ke);
{

}


public void keyTyped(KeyEvent ke);
{

}
});




btnClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae);
{
System.exit(0);
}
});







btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae);
{
txtNumber.setText("");
txtSquareRoot.setText("");
txtCubicRoot.setText("");
txtRound.setText("");
}
});


txtSquareRoot.setEditable(false);
txtCubicRoot.setEditable(false);
txtRound.setEditable(false);
}



public static void main(String args[])
{
Calculator calculator=new Calculator();
}
}
View Answers

March 17, 2010 at 10:32 AM

Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;

public class Calculator extends JFrame{
JLabel lblNumber=new JLabel("Number");
JLabel lblSquareRoot=new JLabel("Square Root");
JLabel lblCubicRoot=new JLabel("Cuboc Root");
JLabel lblRound=new JLabel("Round");

JTextField txtNumber=new JTextField();
JTextField txtSquareRoot=new JTextField();
JTextField txtCubicRoot=new JTextField();
JTextField txtRound=new JTextField();

JButton btnClose=new JButton("Close");
JButton btnClear=new JButton("Clear");

public Calculator(){
Container c=getContentPane();
c.setLayout(new GridLayout(0,2));
c.add(lblNumber);
c.add(txtNumber);
c.add(lblSquareRoot);
c.add(txtSquareRoot);
c.add(lblCubicRoot);
c.add(txtCubicRoot);
c.add(lblRound);
c.add(txtRound);
c.add(btnClose);
c.add(btnClear);

pack();
setVisible(true);

txtNumber.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent ke){
double number=Double.parseDouble(txtNumber.getText());
txtSquareRoot.setText(""+Math.sqrt(number));
txtCubicRoot.setText(""+Math.cbrt(number));
txtRound.setText(""+Math.round(number));
}
public void keyPressed(KeyEvent ke){
}
public void keyTyped(KeyEvent ke){
String input=txtNumber.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
txtNumber.setText("");
txtSquareRoot.setText("");
txtCubicRoot.setText("");
txtRound.setText("");
}
});
txtSquareRoot.setEditable(false);
txtCubicRoot.setEditable(false);
txtRound.setEditable(false);
}
public static void main(String args[]){
Calculator calculator=new Calculator();
}
}

Thanks









Related Tutorials/Questions & Answers:
event handling
event handling  diff btwn event handling in ASP.net and in HTML
handling Button Event in iphone
handling Button Event in iphone  handling Button Event in iphone
Advertisements
Event handling on an image
Event handling on an image  I want to divide an image into frames and want to do event handling on the frames
Java event handling
Java event handling  Which java.util classes and interfaces support event handling
javascript event handling examples
javascript event handling examples  javascript event handling examples   var handleClick = function(e) { // Older IEs set the `event... event listeners. document.attachEvent('onclick', handleClick); } else
event handling in jsp
event handling in jsp  i am developing a web application. i am getting a problem in event handing in jsp. in this web application there is a file... to generate event on each row of retrieved row from the database
Java event handling
Java event handling  What event results from the clicking of a button
Java event handling
Java event handling  What is the purpose of the enableEvents() method
KEY EVENT HANDLING
KEY EVENT HANDLING  I am trying to write a program that receives every key stroke even when the window is not active or it's minimized. Is there anyway to do
Event handling - Java Beginners
Event handling  Hi, This is a similar kind of question i had asked before, with a little difference. Two JCombobox and a JtextField. When I select an Item from the first Combo(i.e.,Select,First,Second or Third),the set
Event Handling - Java Beginners
Event Handling  import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame { JLabel lblNumber=new JLabel("Number"); JLabel lblSquareRoot=new JLabel("Square Root
What are events? Explain how Event handling in Java?
What are events? Explain how Event handling in Java?  What are events? Explain how Event handling in Java?   Hi, The Events..., and introduces us to AWT event handling. For Details about Event Handling in Java
JComboBox Event Handling - Java Beginners
JComboBox Event Handling  Hi, I have problem with event handling here.There are Two JComboBox and a JTextField.When I select an item in first combo(Select,First,Second in the below program) the items in the second combo change
Event Handling In Java
Event Handling In Java In this section you will learn about how to handle.... In Java event handling may comprised the following four classes : Event Sources... that happens and your application behaves according to that event. For example
Understanding the jQuery event handling mechanism
Understanding the jQuery event handling mechanism... handling mechanism First of all , you need to know -What is an event... to handle these event. Event handling methods basically associates an event
how to add audio clip in swings with event handling
how to add audio clip in swings with event handling  hello dear, i wanna to play audio clip in java program in JFrame in swings by event handling ,it should be like when i click on a button , audio clip should play , plz tell me
J2ME Event Handling Example
J2ME Event Handling Example       In J2ME programming language, Event Handling are used to handle certain... event like selecting an item from a listADS_TO_REPLACE_1 And low level events like
Handling Key Press Event in Java
Handling Key Press Event in Java   ... the handling key press event in java. Key Press is the event is generated when you press any key to the specific component. This event is performed by the KeyListener
how to open a web page using proxy in java and handling button event in that web page..?
how to open a web page using proxy in java and handling button event in that web page..?  Sir, what i need is a code which uses proxy address and opens a web page i.e for example and then clicks a "yes" button in that page
Handling Mouse Clicks in Java
Handling Mouse Clicks in Java       Introduction In this section, you will learn about handling the mouse click event in the awt application. This program simply implements
exception handling
exception handling  explain about exception handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  could you please tell me detail the concept of exception handling
File Handling
File Handling  how we do file handling of parsed XML code bu DOM in java
exception handling
exception handling   Give the exception hierarchy of Java.   Please visit the following links: http://www.roseindia.net/java/java-exception/exception-java.shtml http://www.roseindia.net/java/exceptions/exception
Event handling in Java AWT
Event handling in Java AWT     ... related to the event handling through this example and used methods through which you can implement the event driven application. This example shows you how to handle
exception handling
exception handling  What is an exception and what are the advantages of exception handling mechanism.   Java Exception Exception... the working code and the error-handling code can be disintegrated. It also gives us
error handling mechanism
error handling mechanism  What is Java error handling mechanism
delegation event model
delegation event model  What is delegation event model?   delegation event model In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event
Exception Handling in Struts.
Exception Handling in Struts.  How you can do Exception Handling in Struts
exception handling in php - PHP
exception handling in php  i need few examples on exception handling in php
exception handling mechanism
exception handling mechanism  Explain Java's exception handling mechanism in detail.   Please visit the following links: http.../exceptions/catching-and-handling-exceptions.shtml
Event on Slide bar In Java
Event on Slide bar In Java       Introduction In this section, you will learn about the event handling for a swing component named Slide bar. This section provides a complete
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
unicode handling in java
unicode handling in java  reading a unicode text file in java
jquery session handling
jquery session handling  What is session handling in JQuery
php error handling function
php error handling function  Is there any library or function that can be used to handle errors in PHP
JSF Application
;   Immediate Event Handling Example Event Handling... application, which explains how to implement ?immediate? event handling in JSF. Immediate event handling is useful in cases where you do not need to validate an entire
File handling in Java
File handling in Java  Hi, Can any one tell me the basics of File handling in Java? ThanksADS_TO_REPLACE_1   Hi, File handling... provide very good file handling features in their api.ADS_TO_REPLACE_2 You can read
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how... for exception handling and it comes with many pre-build exception classes
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how... for exception handling and it comes with many pre-build exception classes

Ads