Here are some statements in a program that sets up some GUI controls. The idea is that the user will type a number into the text field and, after that, press the button when s/he wants the program to read-in that number:

// set up a button that, when pressed, will read what's in the text field.
btnRead = new JButton("Read the Number");
// set up a text field that will read a number when enter is hit.
txtNumber = new JTextField("0.0", 15);

If we want the button event to cause the number in the text field to be read by the program when either the button is pressed or the text field is modified and the enter key was hit, we will need to: (Check all that apply. There may be more than one.)
A) add an ActionListener to btnRead (among other things).
B) from inside the txtNumber ActionListener method definition, we will need to refer to the btnRead control in some statement or statements.
C) from inside the btnRead ActionListener method definition we will need to refer to the txtNumber control in some statement or statements.
D) add an ActionListener to the txtNumber (among other things).
E) add an ActionListener to only the btnRead but not to txtNumber.

Respuesta :

Answer:

Action Listener class has to present in a program only once, actionPerformed() method is overriding method also present once.

When we have multiple source of event like here button and textfield, single actionPerformed() method will provide implementation

We can detect the source from when event is fired, and accordingly write separte code for each source or same code (like here).

Hence we have to add common addActionListener to both button and textfield.

Same reading mechanism will work inside that common actionPerformed method.

Both source will fired event only if they have both bounded to addActionListener

AS our case Button will fire event and TextField will also fire event.

So both of them registered with addActionListener method.

So from question of choices

a (add an actionListener to btnRead (Among other things)

d (add an actionListener to txtNumber (Among other things)

are correct

TO VERIFY THE CONCEPT A SAMPLE PROGRAM WITH OUT PUT HAS GIVEN BELOW

*/

/* PROGRAM TO SET EVENT HANDELER TO BOTH BUTTON AND TEXTFIELD, SO BOTH oF THEM CAN FIRED EVENT TO READ TEXT VALUE*/

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class TestInput {

JButton btnRead;

JTextField txtNumber,txtDispaly;

JFrame f;

public TestInput() //// Constructor

{

f=new JFrame();

f.setLayout(new FlowLayout()); //// SETTING FRAME TO FLOW LAYOUT

txtNumber=new JTextField("0.0",15); //// CREATED A TEXTFIELD WHEN ENTER PRESSED, IT SHOULD READ OWN VALUE

f.add(txtNumber);

btnRead=new JButton(" Read The Number "); // CREATED A BUTTON WHEN PRESSED TO READ FROM NUMBER TEXTFIELD

f.add(btnRead); // ADD BUTTON TO FRAME

txtDispaly=new JTextField("0.0",15); // VALUE TO BE DISPLAY HERE AFTER READ , EVENT FIRED EITHER BUTTON OT TEXTFIELD

f.add(txtDispaly); // ADD TEXTFIELD TO FRAME

f.setSize(300,300); // FRAME SIZE TO DISPLAY

f.setVisible(true);

ActionListener myHandler = new EventHandler(); // INSTANCE OF CUSTOME EVENT HANDELER

btnRead.addActionListener(myHandler); /// Binding the custom event handler TO BUTTON

txtNumber.addActionListener(myHandler); /// Binding event handler to TextField, so on enter key press it will also read

}

/// Define inner class as listener.

private class EventHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

// EVENT IS TRIGGERED EITHER BUTTON IS CLICKED OT TEXTFIELD IS ENTER KEY PRESSED

if(e.getSource() == btnRead || e.getSource() == txtNumber )

{

txtDispaly.setText(txtNumber.getText()); // VALUE DISPLAYED IN DISPLAY TEXTFIELD AFTRE READ FROM NUMBER TEXTFIELD

}

}

}

public static void main(String[] args)

{

new TestInput();

}

}

Explanation:

For Detailed and better Explanation the screenshot of code and output is given in the attached files. Please have a look on them.

Ver imagen abidhussain7972
Ver imagen abidhussain7972