Question

Question 7 1 pts Here are some statements in a program that set up some GUI controls. The idea is that the user will type a n

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
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();
   }

}

SCREEN SHOT OF CODE

/*PROGRAM TO SET EVENT HANDELER TO BOTH BUTTON AND TEXTFIELD, So BOTH oF THEM CAN FIRED EVENT TO READ TEXT VALUE*/ import jav/ Define inner class as listener private class EventHandler implements ActionListener { public void actionPerformed (ActionEvSCREEN SHOT OF OUTPUT

X 877 Read The Number 877

Add a comment
Know the answer?
Add Answer to:
Question 7 1 pts Here are some statements in a program that set up some GUI...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...

    Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...

  • Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one...

    Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one receives the length of the sequence, and the second receive the sequence (each number separated by a comma). There may, or may not, be space in between the comma and the number element. Provide a clear button that has an event listener attached to it. When pressed, the two text fields are emptied. The event listener should be defined and implemented as an anonymous...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • ***Please use java code for the question below*** Write a program that calculates the future value...

    ***Please use java code for the question below*** Write a program that calculates the future value of a given investment at a given interest rate for a specified number of years. The formula for this calculation is: value = investmentAmount * (1 + monthly interest rate)years*12 Use text fields for the user to enter the numbers (Investment Amount, Number of Years, and Annual Interest). To get/load data from the textbox, (in this case doubles) use the following structure: (bold are...

  • Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event...

    Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event handler inner that extends WidgetViewerActionEvent it has instance variables StringSet sSet JTextField inputStr JLabel numStr JLabel numChar In the constructor, create a WidgetViewer object create sSet create a local variable JLabel prompt initialized to "Enter a String" create inputStr with some number of columns create a local variable JButton pushMe initialized to "Push to include String" create numStr initialized to "Number of Strings: 0"...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • This task is a program framework that you should complete. The program should allow the user...

    This task is a program framework that you should complete. The program should allow the user to move a circular figure with the mouse over a drawing area. The current position of the figure is displayed continuously: Given is the main program: import javafx.scene.Scene; import javafx.application.Application; import javafx.beans.value.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; public class Main extends Application { private DraggableCircle dc; private Text text; private void updateText() { text.setText("("+dc.getCenterX()+", "+dc.getCenterY()+")"); } @Override public void start(final Stage...

  • /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...

    /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate * a course average and a letter grade. In order to calculate * the average and the letter grade, a GradeCalculator must store * two essential pieces of data: the number of grades and the sum * of the grades. Therefore these are declared as object properties * (instance variables). * Each time calcAverage (grade) is called, a new grade is added to *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT