Java Events and Event Handling

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

In Java GUI programming, what does an Event represent?

  • An activity that occurs between the user and the application. (correct)
  • A method for defining object properties.
  • A specific data type for handling integers.
  • A predefined class for creating user interfaces.

What is the parent class for all event objects in Java?

  • `AWTEvent`
  • `EventObject` (correct)
  • `ActionEvent`
  • `ComponentEvent`

Which of the following statements correctly describes the inheritance hierarchy of event classes in Java?

  • `Object` -> `EventObject` -> `AWTEvent` -> `ActionEvent` (correct)
  • `Object` -> `EventObject` -> `ActionEvent` -> `AWTEvent`
  • `Object` -> `AWTEvent` -> `EventObject` -> `ActionEvent`
  • `Object` -> `ActionEvent` -> `AWTEvent` -> `EventObject`

In Java event handling, which event type is typically generated when a user clicks a button?

<p><code>ActionEvent</code> (C)</p> Signup and view all the answers

When a user changes the text in a text field within a Java GUI, which event type is generated?

<p><code>TextEvent</code> (A)</p> Signup and view all the answers

Which interface must an object implement to be notified of an event?

<p><code>EventListener</code> (A)</p> Signup and view all the answers

What must an object, which responds to an event, contain?

<p>A method that accepts the event object created by the user's action. (B)</p> Signup and view all the answers

If you register a JFrame to listen for events generated by a JCheckBox, what is a necessary step in your code?

<p>Write a method that reacts to any generated event. (B)</p> Signup and view all the answers

Which method is called when an ActionListener detects an event?

<p><code>actionPerformed(ActionEvent)</code> (C)</p> Signup and view all the answers

What are the two methods that an FocusListener interface contains?

<p><code>focusGained()</code> and <code>focusLost()</code> (B)</p> Signup and view all the answers

Which method returns the object involved in the event?

<p><code>getSource()</code> (C)</p> Signup and view all the answers

What information does the getClickCount() method of the MouseEvent class provide?

<p>The number of mouse clicks associated with the event. (C)</p> Signup and view all the answers

You are writing a program that needs to respond when the mouse cursor moves onto a specific component. Which method should you use?

<p><code>mouseEntered(MouseEvent e)</code> (B)</p> Signup and view all the answers

Which interface provides methods to detect when the mouse is dragged across a component surface?

<p><code>MouseMotionListener</code> (A)</p> Signup and view all the answers

Which interface implements all the methods in both the MouseListener and MouseMotionListener interfaces?

<p><code>MouseInputListener</code> (A)</p> Signup and view all the answers

In the MouseEvent class, what does the field BUTTON3 indicate?

<p>The third mouse button. (A)</p> Signup and view all the answers

If you want the button to perform a specific action when button1 of the mouse is pressed where the variable e is of type MouseEvent, what condition would you put in the body of mousePressed() method ?

<p><code>if (e.getButton() == MouseEvent.BUTTON1)</code> (A)</p> Signup and view all the answers

What is the primary purpose of the JList class in Swing?

<p>To display a list of items. (B)</p> Signup and view all the answers

How is a JList object typically constructed?

<p>By passing an array of String objects. (D)</p> Signup and view all the answers

In a JList, what selection mode allows only one item to be selected at a time?

<p><code>SINGLE_SELECTION</code> (A)</p> Signup and view all the answers

What method is used to set the selection mode of a JList?

<p><code>setSelectionMode()</code> (B)</p> Signup and view all the answers

Which method returns the index of the first visible cell in the JList?

<p><code>getFirstVisiblelndex()</code> (C)</p> Signup and view all the answers

What is the primary purpose of the JScrollPane class?

<p>To provide a scrollable view of a component that exceeds its allocated space. (B)</p> Signup and view all the answers

If you want a JScrollPane to always display the vertical scrollbar, regardless of whether it's needed, which constant should you use?

<p><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS</code> (B)</p> Signup and view all the answers

Which method is used to add a horizontal scroll bar to the JScrollPane?

<p><code>setHorizontalScrollBar</code> (C)</p> Signup and view all the answers

What is the purpose of requestFocus() in the context of text fields?

<p>To move the input cursor back to the text field. (D)</p> Signup and view all the answers

If you have a JPasswordField component named jPasswordField1, how do you retrieve the text entered by the user as a String?

<p><code>String password = new String(jPasswordField1.getPassword());</code> (D)</p> Signup and view all the answers

What is the purpose of the following code snippet: tbl.addMouseListener(new MouseAdapter() { public void mouseClicked (MouseEvent e) { ... } });

<p>It adds a mouse listener to the table to detect mouse click events. (D)</p> Signup and view all the answers

What does the getSelectedRow() method of a JTable return?

<p>The index of the first selected row. (C)</p> Signup and view all the answers

Given a JTable named tbl with a DefaultTableModel named tbl_record, how can you retrieve the value from the cell in the first row and second column?

<p><code>tbl_record.getValueAt(0, 1)</code> (D)</p> Signup and view all the answers

In the given code, what is the purpose of clearRecords() method?

<p>To clear the input fields. (A)</p> Signup and view all the answers

What is the purpose of the following line of code? tbl.setRowSelectionInterval(i, i);

<p>It selects a row in the table. (C)</p> Signup and view all the answers

What will be the effect of changing the line btnsrc.addActionListener(e -> look()); to btnsrc.addActionListener(e -> clearRecords());?

<p>The text fields will be cleared every time Search button is pressed. (D)</p> Signup and view all the answers

Select valid methods to set or retrieve the text field values?

<p><code>setText()</code>, <code>getText()</code> (C)</p> Signup and view all the answers

For the following code, what happens when username is equal to admin and password is equal to admin123?

String ext = new String(jPasswordFieldl.getPassword()); if (jTextFieldl.getText().equals("admin") &&ext.equals("admin123")) JOptionPane.showMessageDialog(null, "Login Successful");

<p>A message box for login successful will appear. (A)</p> Signup and view all the answers

Which of the given lines is most likely to prevent the scrollbar to be properly placed in the scrollpane where a JTable is being added to a scrollpane?

JScrollPane scrollbar = new JScrollPane(tbl); scrollbar.setBounds(20, 200, 440, 200); add(scrollbar);

<p>Setting layout manager. (C)</p> Signup and view all the answers

Flashcards

What are events in Java?

Events represent activity between the user and the application.

What is EventObject?

The parent class for all event objects.

Events and Actions

Common actions (like button clicks) generate specific event types (like ActionEvent).

What is an Event Listener?

Object that includes a method that gets executed in response to the generated events.

Signup and view all the flashcards

What triggers ActionEvents?

Button clicks

Signup and view all the flashcards

What is MouseClicked (MouseEvent e)?

States the mouse has been clicked (pressed and released) on a component

Signup and view all the flashcards

What is mouseMoved (MouseEvent e)?

States the mouse cursor has been moved onto a component but no buttons have been pressed

Signup and view all the flashcards

What is getX() in MouseEvent?

Returns the horizontal x-position of the event relative to the source component

Signup and view all the flashcards

What is getY() in MouseEvent?

Returns the vertical y-position of the event relative to the source component

Signup and view all the flashcards

What is the JList class used for?

Used to display a list of items

Signup and view all the flashcards

What are the selection modes for JList?

Single-selection, single-interval, and multiple-interval.

Signup and view all the flashcards

What is JScrollPane used for?

Can hold components that require more display area than they have been allocated.

Signup and view all the flashcards

How to force scroll bar display?

Used to force the display of a scroll bar by using class variables defined in the ScrollPaneConstants class.

Signup and view all the flashcards

Study Notes

  • Events in Java represent all user activity within an application.
  • Like Java classes, events are Objects initiated by the user.
  • EventObject serves as the parent class for all event objects, descending from the Object class.
  • EventObject is the parent of AWTEvent, which is the parent of event classes like ActionEvent and ComponentEvent.

Events and Event Handling

  • Common user actions generate specific events.
  • Clicking a button results in an ActionEvent.
  • Clicking a component triggers a MouseEvent.
  • Selecting an item in a list box generates an ItemEvent.
  • Clicking a check box item also results in an ItemEvent.
  • Changing the text in a text field creates a TextEvent.
  • Opening a window produces a WindowEvent.
  • Iconifying a window also generates a WindowEvent.
  • Pressing a key triggers a KeyEvent.
  • GUI programs handle events originating from mouse or keyboard interactions with Components or Containers.

Event Listener

  • An event listener is an object with a method that executes in response to generated events.

  • Objects can be notified of events by implementing the appropriate interface and registering as an event listener of the event source.

  • The class responding to an event needs a method accepting the event object created by the user's action.

  • When registering a component to listen for events, a method that reacts to the generated event must be written.

Methods Reacting to Events

  • ActionListener uses actionPerformed(ActionEvent).
  • AdjustmentListener uses adjustmentValueChanged(AdjustmentEvent).
  • FocusListener uses focusGained(FocusEvent) and focusLost(FocusEvent).
  • ItemListener uses itemStateChanged(ItemEvent).

AWTEvent Class Methods

  • getSource(): Returns the Object involved in the event.
  • getComponent(): Returns the Component involved in the event.
  • getWindow(): Returns the Window involved in the event.
  • getStateChange(): Returns an integer, ItemEvent.SELECTED or ItemEvent.DESELECTED.

MouseListener Methods

  • mouseClicked(MouseEvent e): Invoked when a mouse button is clicked on a component.
  • mouseEntered(MouseEvent e): Invoked when the mouse enters a component.
  • mouseExited(MouseEvent e): Invoked when the mouse exits a component.
  • mousePressed(MouseEvent e): Invoked when a mouse button is pressed on a component.
  • mouseReleased(MouseEvent e): Invoked when a mouse button is released on a component.

MouseMotionListener Methods

  • mouseDragged(MouseEvent e): Invoked when the mouse is pressed on a component and dragged.
  • mouseMoved(MouseEvent e): Invoked when the mouse cursor is moved onto a component with no buttons pressed.

Methods and Fields of MouseEvent Class

  • getButton(): Returns which mouse button has changed state, if any.

  • getClickCount(): Returns the number of mouse clicks associated with the event.

  • getX(): Returns the horizontal x-position of the event, relative to the source component.

  • getY(): Returns the vertical y-position of the event, relative to the source component.

  • BUTTON1: Indicates mouse button #1, used by getButton().

  • BUTTON2: Indicates mouse button #2, used by getButton().

  • BUTTON3: Indicates mouse button #3, used by getButton().

  • NOBUTTON: Indicates no mouse buttons, used by getButton().

  • Constants for specific mouse events include MouseEvent.MOUSE_CLICKED, MouseEvent.MOUSE_DRAGGED, MouseEvent.MOUSE_ENTERED, and MouseEvent.MOUSE_EXITED.

InputEvent Class

  • getModifiers(): Returns an integer indicating which mouse button was clicked.

KeyEvent Class

  • getKeyChar(): Returns the Unicode character entered from the keyboard.
  • getClickCount(): Returns the number of clicks

Mouse Events

  • The MouseMotionListener interface offers mouseDragged() and mouseMoved() to detect mouse movements.
  • The MouseListener interface includes mousePressed(), mouseClicked(), andmouseReleased().
  • The MouseInputListener interface implements all methods from both MouseListener and MouseMotionListener.

Other Components of Event Handling

Abstract Button Class

  • A GUI application with an button, performs an action when the button is clicked.
  • Demonstrates event handling for button clicks, where the button text changes upon clicking.

JList Class

  • The JList class displays a list of items like students or files.
  • JList objects are created by passing an array of strings.
  • Three Selection Modes: Single Selection, Single Interval, and Multiple Interval.

Methods of the JList Class

  • getFirstVisibleIndex(): Returns the index of the first visible cell.
  • getLastVisibleIndex(): Returns the index of the last visible cell.
  • getMinSelectionIndex(): Returns the smallest selected cell index.
  • getMaxSelectionIndex(): Returns the largest selected cell index.
  • getSelectedIndex(): Returns the first selected index, or -1 if no item is selected.
  • setSelectedIndex(int index): Selects a single cell.
  • getSelectionMode(): Returns whether single-item or multiple-item selections are allowed.
  • setSelectionMode(int selectionMode): Determines whether single-item or multiple-item selections are allowed.
  • isSelectionEmpty(): Returns true if nothing is selected.

JScrollPane Class

  • JScrollPane can hold components needing more space than allocated.

  • The constructor can come in four forms:

  • JScrollPane(): Creates an empty pane with scroll bars appearing when needed.

  • JScrollPane(Component): Displays the content of a specified component.

  • JScrollPane(Component, int, int): Displays a component with specified vertical and horizontal scroll bar policies.

  • JScrollPane(int, int): Creates a scroll pane with specified vertical and horizontal scroll bar policies.

  • Use class variables defined in ScrollPaneConstants class to force the bar to appear.

  • Example of a pane displaying a picture with a vertical, but no horizonal scroll bar.

JScrollPane Methods

  • createHorizontalScrollBar(): Returns a default JScrollPane.ScrollBar.
  • createVerticalScrollBar(): Returns a default JScrollPane.ScrollBar.
  • getHorizontalScrollBar(): Returns the horizontal scroll bar that controls the viewport's horizontal view position.
  • getVerticalScrollBar(): Returns the vertical scroll bar that controls the viewport's vertical view position.
  • setHorizontalScrollBar(JScrollBar horizontalScrollBar): Adds the scrollbar that controls the viewport's horizontal view position.
  • setLayout(LayoutManager layout): Sets the layout manager for this JScrollPane.
  • setVerticalScrollBar(JScrollBar verticalScrollBar): Adds the scrollbar that controls the viewport's vertical view position.

Interaction Between Components

  • setText(""); // setting text field value
  • getText(""); // getting text field value
  • requestFocus(); // setting focus(cursor) back to the text field

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Discover Java
10 questions

Discover Java

InvulnerableBlackTourmaline avatar
InvulnerableBlackTourmaline
Java Events and Handling
30 questions

Java Events and Handling

ContrastyMoldavite6567 avatar
ContrastyMoldavite6567
Flood and Landslide Events in Central Java
5 questions
AWT Event Handling Basics
40 questions

AWT Event Handling Basics

WorldFamousLife7260 avatar
WorldFamousLife7260
Use Quizgecko on...
Browser
Browser