Handle swing ScrollBar events in scala

I am trying to add a ScrollBar . ScrollBar will ScrollBar over the displayed documents. However, I am having trouble getting an event when the scrollbar changes. I'm not sure what I need to listen to, and I'm not sure which event I should respond to. I tried the following and I have some events, but I do not think these are ideal events to handle.

  listenTo(scrollBar) listenTo(scrollBar.keys) listenTo(scrollBar.mouse.moves) listenTo(scrollBar.mouse.wheel) listenTo(scrollBar.mouse.clicks) 

For example, I only get MouseClicked , MousePressed and MouseReleased when I click inside the scrollbar - not when I click on the arrows to actually change the value.

I found this discussion about scrollbars not taking events properly, but it's two years. As far as I can tell, the author did not track the ticket for the file. Perhaps he found a workaround.

Any ideas?

+4
source share
2 answers
Good question. Clicking on arrows is not processed by ScrollBar , it is processed by ScrollBarUI . I believe that the default implementation (or at least the base class for most ScrollBarUI implementations) is BasicScrollBarUI .

If you look at the source javax.swing.plaf.basic.BasicScrollBarUI , it has incrButton and decrButton , and they are the components you want to listen to.

PS I had a similar need to have a custom (key) listener for my Slider and have a custom ui that provides the necessary components / model (since you could see that almost all components are protected, so you can easily access them in subclasses and expose through public getters) worked fine for me. I did this in simple java, although perhaps in scala you can listen to buttons only by specifying a property name.

+1
source

Another piece of Scala Swing is broken. The Adjustable dash seems completely empty, nothing is connected.

The following works:

 class ScrollBarAlive extends swing.ScrollBar { me => peer.addAdjustmentListener(new java.awt.event.AdjustmentListener { def adjustmentValueChanged(e: java.awt.event.AdjustmentEvent) { publish(new swing.event.ValueChanged(me)) } }) } 

Test:

 import swing._ object ScrollBarTest extends SimpleSwingApplication { lazy val top = new Frame { val label = new Label { text = "0" } val scroll = new ScrollBarAlive { orientation = Orientation.Horizontal listenTo(this) reactions += { case event.ValueChanged(_) => label.text = value.toString + (if (valueIsAjusting) " A" else "") } } contents = new BorderPanel { add(label, BorderPanel.Position.North) add(scroll, BorderPanel.Position.South) } pack().centerOnScreen() open() } } 

The correct implementation also introduces a subtype of AdjustingEvent .

0
source

Source: https://habr.com/ru/post/1396286/


All Articles