Why Swing JScrollPane doesn't respond to mouse wheel? - java

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?
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).

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);

Related

Does Java repaint the components when resize or mouse over?

I am not asking how to use Java swing nor I am asking for suggestion on using layout managers. I am just curious how Java behaves.
All along it has been a myth and many people speculate that Java automatically repaints the components when you resize the frame OR mouse over the components in the frame.
So my question is: Is it true that Java does the repainting automatically when we carry out one of the above actions?
There has been several post with similar title such as: Java repainting a component at mouse-over.
But no one can give a definite answer whether Java does the repainting automatically upon certain user actions (such as resizing & mouse over).
All along it has been a myth and many people speculate
There is no myth or speculation.
automatically repaints the components when you resize
This makes sense because the layout manager is invoked and the size or location may change which means some components may need to be repainted.
automatically repaints the components when you mouse over the components in the frame.
It depends on the component. If a MouseListener has been added to the component to do special processing (ie. rollover a button) then the component may be repainted, otherwise nothing happens. But there is no default painting unless it has been specifically added as part of the UI for the component.
These question is easily verified. Just override the paintCompent() method of your components to display a message when the component is painted and see what happens.

How do I make a Swing JComponent a bigger mouse target?

I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.

Transparent AWT component, possible?

I'm in the need of a translucent AWT component/container to be placed on top of other components to receive the mouse events.
The problem here is to create a translucent component. My investigation so far showed that Swing has the possiblity to do setOpaque(false) on e.g. a JPanel. Is there something similiar for AWT?
The idea is to make a ScrollPane scrollable by simply clicking somewhere and moving the mouse around. Thats why I need a transparent component to place it above all other components.
Long story short, I need a translucent component, no matter which one, to place on top of other elements to receive the mouse events.
Thanks in advance
AFAIK not possible correctly for prehistoric AWT Container and Components you can to try that with changing this code from Swing to the AWT, Translucency is supported for Swing JComponents only
The JNA project provides some utilities for making AWT components translucent. There's a WebStart demo of the functionality. The window transparency utilities are in platform.jar, which augments the base jna.jar.
However, if all you want to do is trap mouse events, you should look into the different layers that are already built into a JFrame. The component already has a concept of layers via JLayeredPane, and you can fairly easily use a JPanel as a glass pane which sits above all other components to filter events.

Position of JComponents using Netbeans GUI Swing Builder

I'm developing a graphic interface using Java and Swing, and I'm having a hard time getting the JButtons to stay in their position while changing from one panel to another.
There are three buttons in a row aligned in the left bottom of each panel, all the panels the same size, but somehow they manage to change their position a little when I run the application (on the design preview they show up in the right place). It's getting a bit annoying. I'd appreciate any help
Are you trying to do tabs? If you are, a JTabbedPane will do this much better than a button.
Since you are using the Netbeans GUI Builder, look at the options in the Component panel on the left. It has Swing tabbed panes and AWT panes if you really want.

Is it possible to have "movable"/"draggable" components like JButtons, JTextFields in a JFrame?

Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.

Categories

Resources