0

I have the following Swing UI structure.

When I scroll the mouse wheel within the blue JPanel, the JScrollPane doesn't respond to the mouse wheel event. Why?

I read from the java doc that:

JScrollPane provides a scrollable view of a lightweight component. ... Note that JScrollPane does not support heavyweight components.

So is this because my structure is too heavy? Or any other reasons?

enter image description here

ADD 1

I accidentally switched my window with the mouse middle click. And after that, the mouse wheel suddenly worked for the JScrollPane.

This leads me to think maybe it's related to the focus. Then I found below line:

this.setFocusableWindowState(false);

After I changed it to below, mouse wheel works.

this.setFocusableWindowState(true);

Though javadoc says:

Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window.

At first, I guess it's because the JDialog is not in focusable window state, so it cannot receive events. But actually, mouse click always works. So I am still not sure about the root cause.

It seems a toolbar or a floating palette cannot be focused but still can receive mouse click event. So I guess maybe only certain events are filtered by setFocusableWindowState(false).

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 2
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). *"So is this because my structure is too heavy?"* Maybe if it were 5000 text panes. The problem *might* be that the text panes are consuming the mouse wheel event. – Andrew Thompson Apr 02 '15 at 09:43
  • 2
    there isn't any problem with, including scrolling the child e.g. JScrollPane inside parent JScrollPane, JTextPane by default never to consume() those events, voting to close as too broad without an SSCCE/MCVE, short, runnable, compilable, without hardcoded value for JTextPane/(Xxx)Document/(Xxx)EditorKit – mKorbel Apr 02 '15 at 10:22

1 Answers1

2

About components being heavy weight: AWT components are meant, that use the operating system widgets. You use swing JComponents, J*. Swing components are called light weight as they do all drawing and event handling emulated, in one large native window.

The JPanel should be larger than the JScrollPane, so a "view port" may be scrolled. Have the preferred sizes set correctly.

In general I would have thought that every JTextPane would be in its own JScrollPane.

Also JScrollPane functions a bit differently.

scrollPane = new JScrollPane(panel);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • "Have the *preferred sizes* set correctly." See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) If using HTML content, the content can suggest a width in CSS, which will in turn suggest a height. If that condition (HTML content) does not apply here, it is better to *override* than *set* the preferred size of the text pane. – Andrew Thompson Apr 02 '15 at 09:48
  • 1
    BTW - I misunderstood what that first sentence was explaining the first couple of times I read it, simply because the words were not in the order I expected. Now I do understand what is written, plus one. :) – Andrew Thompson Apr 02 '15 at 09:59
  • Oh, and sorry, between writing that last comment and actually voting, I got a little distracted. :P – Andrew Thompson Apr 02 '15 at 11:41
  • 1
    Reformulated about that heavy, though still not with clear style today. And feedback always welcome; points irrelevant. – Joop Eggen Apr 02 '15 at 11:53
  • 1
    Maybe something like *"What it means by 'heavyweight components' is AWT components (e.g. `java.awt.Panel` or `java.awt.TextArea`) that use the operating system widgets, unlike the 'light weight components' from the Swing package, like `javax.swing.JPanel` or `javax,swing.JTextPane` as seen above."* Though I have to warn you, I've had around 3 hours sleep in the last 27, and I still like your 2nd sentence about Swing components better then the end of the sentence I just wrote. :P ..Maybe leave it as is. :) – Andrew Thompson Apr 02 '15 at 12:01