Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Event Handling in Java GUI | Java GUI Development
Advanced Java 2077
course content

Зміст курсу

Advanced Java 2077

Advanced Java 2077

1. Data Structures
2. Sorting and Searching
3. Concurrent Programming
4. Input-Output (I-O) and Networking
5. Java GUI Development

bookEvent Handling in Java GUI

In the previous chapters, we learned about creating basic GUI components such as buttons, labels, and text fields. In this chapter, we will learn about event handling, which is an essential part of GUI development in Java.

What is Event Handling?

Event handling is the process of responding to user actions, such as button clicks or mouse movements. In Java GUI development, you can use event listeners to handle events. An event listener is an object that is notified when an event occurs, and it can perform some action in response to the event.

Event Types

In Java, there are many types of events that can occur in a GUI application, such as:

  • Action events: occur when a user interacts with a button or menu item.
  • Mouse events: occur when a user interacts with a mouse, such as by clicking or moving the mouse.
  • Key events: occur when a user presses or releases a key on the keyboard.
  • Window events: occur when a window is opened or closed.

Event Listeners

To handle events in a Java GUI application, you need to create event listeners. There are several types of event listeners available in Java, such as:

  • ActionListener: handles action events.
  • MouseListener: handles mouse events.
  • KeyListener: handles key events.
  • WindowListener: handles window events.

Each event listener interface contains one or more methods that are called when an event occurs. For example, the ActionListener interface contains the actionPerformed() method, which is called when an action event occurs.

Here's an example of how to create an ActionListener to handle a button-click event:

java

Main

copy
123456
JButton button = new JButton("Click me"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button clicked"); } });

This code creates a new JButton and adds an ActionListener to it. When the button is clicked, the actionPerformed() method is called, and the message "Button clicked" is printed to the console.

Here are some other examples of how to use event listeners in Java GUI development.

MouseListener

java

Main

copy
123456
JButton button = new JButton("Click me"); button.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse clicked"); } });

This code creates a new JButton and adds a MouseListener to it. When the button is clicked, the mouseClicked() method is called, and the message "Mouse clicked" is printed to the console.

KeyListener

java

Main

copy
12345678
JTextField textField = new JTextField(); textField.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { System.out.println("Enter key pressed"); } } });

This code creates a new JTextField and adds a KeyListener to it. When the user presses the Enter key, the keyPressed() method is called, and the message "Enter key pressed" is printed to the console.

Event Dispatch Thread

In Java GUI development, all user interface events are processed by a single thread known as the Event Dispatch Thread (EDT). This thread is responsible for handling user input, updating the user interface, and running event listeners.

It is important to remember that the EDT is a single-threaded environment, which means that all code that interacts with the user interface must run on the EDT. If you try to update the user interface from a different thread, you may get unexpected results or errors. To ensure that your code runs on the EDT, you can use the SwingUtilities class, which provides a set of methods for running code on the EDT.

Here's an example of how to run code on the EDT using the SwingUtilities class:

java

Main

copy
12345
SwingUtilities.invokeLater(new Runnable() { public void run() { // code to be run on the EDT } });

This code creates a new Runnable object and passes it to the invokeLater() method of the SwingUtilities class. The code inside the run() method will be executed on the EDT.

1. Which event listener interface is used to handle mouse events in Java GUI development?
2. Which class provides a set of methods for running code on the Event Dispatch Thread (EDT) in Java GUI development?
Which event listener interface is used to handle mouse events in Java GUI development?

Which event listener interface is used to handle mouse events in Java GUI development?

Виберіть правильну відповідь

Which class provides a set of methods for running code on the Event Dispatch Thread (EDT) in Java GUI development?

Which class provides a set of methods for running code on the Event Dispatch Thread (EDT) in Java GUI development?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 4
some-alt