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 handlingevent handling diff btwn
event handling in ASP.net and in HTML
Advertisements
Event handling on an imageEvent handling on an image I want to divide an image into frames and want to do
event handling on the frames
Java event handlingJava
event handling Which java.util classes and interfaces support
event handling javascript event handling examplesjavascript
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 jspevent 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
KEY EVENT HANDLINGKEY
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 BeginnersEvent 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 BeginnersEvent 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
JComboBox Event Handling - Java BeginnersJComboBox
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 JavaEvent 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 handlinghow 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 ExampleJ2ME
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
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 handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling what is the information of exception
handling?
Have a look at the following link:
Java Exception
Handling exception handlingexception handling could you please tell me detail the concept of exception
handling File HandlingFile 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 handlingexception 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
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 mechanismexception
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 handlingJava exception handling what are the constraints imposed by overriding on exception
handling 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 JavaFile
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 ExamplesException
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 ExamplesException
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