Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing - replacement for Qt signal/slots

In Qt GUIs it is very convenient use signals & slots - it decouple events passing. When I create some widget that throw signal, I don't have to know in advance who can get it, and later with connect I specify connections.

What is parallel in Java/Swing? Can you point to good resources on this issue?

like image 963
zaharpopov Avatar asked Jun 01 '11 14:06

zaharpopov


People also ask

Are Qt signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.

Can we connect signal to signal in Qt?

This ensures that truly independent components can be created with Qt. You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal.

Can a Qt signal return a value?

All signals and slots have a "void" return value. You cannot return anything that way. This code assumes that there is only one slot that will be connected to the signal.


2 Answers

If none of the existing EventListener implementations meet your requirements, you can create your own custom event. Every JComponent contains a field of type EventListenerList. You can use the approach outlined in the EventListenerList API to enable your custom JComponent subclass to fire your custom event.

Regarding the signal/slot mechanism, Swing has several ways to implement the observer pattern, outlined here.

like image 101
trashgod Avatar answered Sep 24 '22 04:09

trashgod


This Event Listener tutorial goes through the basics of handling events with listeners.

Connecting your slot for a signal is analogous to adding your EventListener to an event-producing object.

like image 25
Atreys Avatar answered Sep 25 '22 04:09

Atreys