How disable mouse wheel scroll on all component except parent - java

I have a jFrame along with jScrollPane in this jScrollPane there are few other component such as jTextField jTextArea jTable etc.. (this form I have designed with NetBeans form designer)
jScrollPane (the Parent Component) has vertical scroll bar and it scroll with the mouse wheel. But when mouse move on a jTextArea or a jTable (they have there own scroll bars) Mouse Wheel focus goes to them and scrolling them instead of scrolling jScrollPane. I want to keep scroll focus on jScrollPane without going it to any other component in it.

found a solution
scrollPane = new JScrollPane() {
#Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
if (!isWheelScrollingEnabled()) {
if (getParent() != null)
getParent().dispatchEvent(
SwingUtilities.convertMouseEvent(this, e, getParent()));
return;
}
super.processMouseWheelEvent(e);
}
};
scrollPane.setWheelScrollingEnabled(false);

I did this in my panel and it worked...
jScrollPane1.removeMouseWheelListener(jScrollPane1.getMouseWheelListeners()[0]);

Related

Javafx text area scroll pane border color problem

I have a problem in javafx text area: When I focus the text area, border is applied... that is ok.
But when I drag with scroll-bar handle, the text area border focus is lost.
See the image below:
This is the my simple text area:
Text area changed when focused like this:
But when I scroll in text area with the scroll handle, the border is changed like before (un-focused) state:
Is there any way to control text area within scroll pane (in text area)?
One possible work around is to not allow focus on the ScrollPane within TextArea. ie., the moment ScrollPane gets focus we force to focus on TextArea. This way the focus will be always on TextArea.
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
public class CustomTextArea extends TextArea {
private ScrollPane textAreaScrollPane;
#Override
protected void layoutChildren() {
super.layoutChildren();
if (textAreaScrollPane == null) {
textAreaScrollPane = (ScrollPane) lookup(".scroll-pane");
textAreaScrollPane.focusedProperty().addListener((obs, oldVal, focused) -> {
if (focused) {
requestFocus();
}
});
}
}
}
And you will use this CustomTextArea across your application.
TextArea textArea = new CustomTextArea();

JScrollPane step size when dragging

Is it possible to specify the step size of a JScrollPane when dragging it?
I'm aware of setUnitIncrement and setBlockIncrement, but these do not work when dragging the scrollbar.
Edit: AdjustmentListener
final AdjustmentListener myAdjustmentListener = new AdjustmentListener() {
#Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if (e.getAdjustmentType() == AdjustmentEvent.TRACK){
e.getAdjustable().setUnitIncrement(500);
e.getAdjustable().setBlockIncrement(500);
}
}
};
Setting setUnitIncrement and setBlockIncrement values only changes the scroll speed when scrolling with a mouse wheel, the arrow keys, and the scroll bar arrows. These values do not change the scroll speed when the scroll bars are dragged.
However the AdjustmentListener can be added to a scrollbar to listen to the scrollbar movements.
An AdjustmentListener should do the magic for you. Did you look at this:
How to make JScrollPane scroll 1 line per mouse wheel step?

Java JScrollPane Detect Scroll Near Bottom

I've been unable to find anything related to my question on the Internet.
I have a JScrollPane which adds a JPanel, and the JPanel gets populated with JLabels.
Currently, the user scrolls to the bottom of the JScrollPane, and then clicks the 'Next' button.
What I would like to do is to detect when the user is at/near the bottom of the JScrollPane, so that I can then trigger the 'Next' behaviour without any input from the user.
Is this even possible?
Thanks in advance everyone.
You could use an AdjustmentListener registered with your the vertical scrollbar in your JScrollPane and take action based on the event value.
Here is an example.
JScrollBar uses a BoundedRangeModel, and this will accept a ChangeListener -- so you can listen for changes to the scrollbar and respond accordingly.
Here is an example showing how to trigger an event when the scrollpane is near the bottom (90% of the height):
final JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.getViewport().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
JScrollBar scrollbar = scrollPane.getVerticalScrollBar();
if (scrollbar.getValue() + scrollbar.getVisibleAmount() > scrollbar.getMaximum() * 0.9) {
// trigger the 'Next' behaviour here
}
}
});
Alternatively, the listener can also be added to the model:
final BoundedRangeModel model = scrollPane.getVerticalScrollBar().getModel();
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
if (model.getValue() + model.getExtent() > model.getMaximum() * 0.9) {
// trigger the 'Next' behaviour here
}
}
});

JScrollPane - automatically scroll to bottom?

I've been trying to create a a scroll pane that automatically keeps scrolling down as the user adds more lines of text to the window. I implemented this:
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
"pane" is a JScrollPane that is holding a JList.
Unfortunately, the code above sticks the scroll bar to the bottom of the pane (which is what I wanted) but then I am unable to move the scroll bar from its position (so now it's stuck at the bottom). Is there a simple way to have the scroll bar stick to the bottom, but still be able to be scrolled elsewhere?
Thanks!
When you add an item to the JList you can use the method:
list.ensureIndexIsVisible(...)
where the index is the index of the last item in the list.

Scroll problem - doesn't scroll when I press

I have strange problem in Java. I have JScrollPane
paneScroll=new JScrollPane(nav,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
where nav is JPanel
public class ScrollableNavigationPanelZones extends JPanel {
private ButtonGroup buttonGroup = new ButtonGroup();
private static final long serialVersionUID = -455651438039139284L;
protected JViewport viewport;
private JPanel panel;
private int offset = 100;
public ScrollableNavigationPanelZones() {
super();
setLayout(new BorderLayout());
panel = new JPanel();
panel.setPreferredSize(new Dimension(0,160));
FlowLayout fl = new FlowLayout();
fl.setVgap(5);
fl.setAlignment(FlowLayout.LEFT);
panel.setLayout(fl);
add(panel,BorderLayout.CENTER);
}
}
and it shows scroll on right side but doesn't have that small box which to move ( I have arrows on top and bottom of scroll, but when I press I cannot scroll ). When I change in ScrollableNavigationPanelZones panel.setPreferredSize(new Dimension(0,16000)); it works but shows empty space. Why JScrollPane doesn't read real height of panel ? What is mistake ?
Can anybody help please ?
The scroll box is displayed only if there is something to scroll, i.e. if the preferred height of the panel exceeds the actual height of the scroll pane. Of course, if you set the preferred height to 16000 it will show much of empty space if the panel doesn't fill all the 16000 pixels with controls.
If the panel fits in the scroll pane, the scroll bar is by default hidden. The flag VERTICAL_SCROLLBAR_ALWAYS implies that the scroll bar is visible always, even if not needed. If you set the panel height to 160 pixels and the scroll pane is larger, you see the scroll bar (because it's ALWAYS) but no scroll box (because there is actually nothing to scroll).
It's a bit strange that the up and down buttons on the scroll bar are clickable even if there is nothing to scroll. Usually they should be greyed out in this case. Maybe there is some additional parameter in JScrollPane for this...

Categories