• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

How to use Wicket component events

February 23, 2020 Posted by Roman Sery wicket No Comments

I’ve mentioned the event/messaging system in my top five reasons you should be using Wicket post. Here let’s take a more in-depth look at usage and demonstrate an initialization pattern that can greatly improve your code.

Wicket events are a way for components and pages to communicate with each other. Using this feature we can create very complex, yet decoupled, component structures. A component can broadcast a message without needing to know who will receive the message. When a component is interested in a particular type of message, it can simply register to be notified when that message is broadcast.

Basic usage

When sending an event/message in Wicket, you need to specify three things:

  • Destination – This can be the WebPage or a specific component.
  • Broadcast type – This dictates the order in which the event is propogated to the child components of the destination.  The options include breadth-first, depth-first, bubble-up, and exact.  You would choose the type based on whether you want the children to recieve the event first, or the parent.  If you only want to send the event to the destination without propogation, you would use an exact broadcast.
  • Payload – Any additional data you want to send with the event. Typically you would at least include the AjaxRequestTarget so that receivers can use it to add components to the target.

Broadcasting an event with payload

Let’s look at an example of how to broadcast an event. We define a class CriticalUpdate which will be our payload. We broadcast to the parent WebPage using breadth-first propagation meaning the WebPage will receive the event first, followed by its children.

send(getPage(), Broadcast.BREADTH, new CriticalUpdate(target, payload));

Responding to the event

Any component that wants to receive the CriticalUpdate event would do so by overriding the onEvent() method:

public void onEvent(IEvent<?> event) {
   if (event.getPayload() instanceof CriticalUpdate) {      
      CriticalUpdate msg = (CriticalUpdate) event.getPayload();
      //do something with the msg
   }
}

Using event-based component initialization

A pattern you can use that will make it easier for you to create your component hierarchies is event-based initialization. The idea is to add all of your UI elements (components, Panels, fields) to the WebPage with a constructor that does a minimum amount of work to initialize without actually creating any child components.

Then when you are done adding all of your components you broadcast an initialization event to the WebPage which will be propagated to all the child components. Upon receiving the event, they can create child components and any other initialization that is needed. This is the approach I use in my Wicket UI library.

The main advantage of this approach is when your components initialize themselves in response to the event, you have access to its parent container and to the page. This gives you more flexibility in your code because your Panel/Component is more aware of its environment.

No Comments
Share
5

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

Using MySQL JSON columns to simplify your data storage: Part 1

Nov 28, 2019

Simplify data storage in your apps by using JSON column types instead of relying on database normalization.

Themeable React Monopoly board

Themeable React Monopoly board

Sep 10, 2020

Early design for upcoming monopoly game.

How to write a JSON response in Wicket

Jan 26, 2020

Learn how to create JSON response endpoints in Wicket

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application ·  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization ·  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket ·  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket ·  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket ·  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Prev Next