Programming Java Event Listener - Action Event | Multiple Widgets...

Event Listener – Action Event | Multiple Widgets | Content pane Layout

-

Previously we have discussed that jframe content pane Layout can contain five things by default, see here. We will see them now in action. Previously we also introduced java event listener and Action event. These things all are related with java GUI interaction. Java GUI all begins with jframe object and java swing components also called widgets. Such as buttons, text area, checkbox etc. Lets begin with simple 2D graphics with button on content pane that change color every time we press the button.

Here we build the frame with two widgets (drawing panel, and a button). Therefore we implement an ActionListener to define what should be done when a user performed certain operation. A swing components (such as button, text area, etc) are the event sources. We know that in java an event source is an object that turn user action into event. Therefore like virtually everything, an event is also on object of some event classes, which we can find in java.awt.event package – are bunch of event classes. Furthermore, every even has a matching listener interface. Hence we create a listener and register with the button. Then we display the frame that wait for user click.

[[We already have discussed in the previous article that a listener interface is the bridge between listener (class that implement interface) and event sources.

Event Listener - Action Event  | Multiple Widgets | Content pane Layout
Event Listener – Action Event

An event source (like button) creates an event object user does something that matter (like click the mouse). So if our class want to know about a buttons Action event, then we need to implement the actionlistener interface.]]

Now, when a user click the button, then the button creates an event object and called listener’s event handler. The event handler calls repaint() on the frame, and the system calls paintComponent() on the drawing panel.

Event Listener – Action Event | Multiple Widgets

import javax.swing.;
imprt java.awt.;
import java.awt.event.*;

public class SimpleGui3C implements ActionListener {
JFrame frame;
public static void main (String[] args) {
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}
  public void go() {
  frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JButton button = new JButton(“Change colors”);
  button.addActionListener(this) ;      

MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);

frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}

Here add listener “this” to the button. We also added button and drawPanel two widget on content pane layout. Furthermore we also defines actionPerformed method with frame.repaint(). Therefore, it indicates when user clicks, that tell the frame to repaint itself. Which means paintcomponent() is called on every widget in the frame. 

class MyDrawPanel extends JPanel {
   public void paintComponent(Graphics g) {
     // Code to fill the oval with a random color
   }
}

[An Incomplete Article]

Latest Articles

Property Decorator | Getters Setters and Deleters in Python

In this article, we will talk about the Property Decorator in Python. It enables the class functionality...

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

You might also likeRELATED
Recommended to you