Java JScrollPane Detect Scroll Near Bottom - java

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

Related

How disable mouse wheel scroll on all component except parent

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

Swing Mouse scroll does not scroll drop down menu

I am trying to scroll down a drop down box. Altough mouseWheelListener is invoked, scrollBar is not moved and dropdown box is just closed after scrool event. Problem is value of scrollBar is not changed after setting in the event handler, it is always 0.
Here is the part of code:
public MyClas extends JPopupMenu implements AdjustmentListener, ContainerListener, ChangeListener{
public MyClass(){
.....
setLayout(new ScrollablePopupMenuLayout(this, defaultWidth));
JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
scrollBar.setFocusable(false);
scrollBar.addAdjustmentListener(this);
addContainerListener(this);
addMouseWheelListener(new MouseWheelListener(){
// Here is invoked after scrolling
public void mouseWheelMoved(MouseWheelEvent e){
{
int x = 1;
scrollBar.setValue(scrollBar.getValue() + x)
}
});
}
It worked after adding following line to the mouseWheelEvent method:
e.consume();

Can't add a mouse listener to a JScrollpane which contains a list

I have a JScrollPane (myListScroll) that is added to a JPanel (which in turn is added to another JPanel before being added to a JFrame). This JScrollPane (myListScroll) consists of a list of strings. I want to be able to handle mouse events when clicking on the different items in this list.
In the code below I want to try if something happens if I click i the JScrollpane but nothing happens. What is wrong? Why is "test" not written?
JScrollPane myListScrol = new JScrollPane(myList);
myListScrol.getViewport().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("test");
}
});
I should have added the listener to myList and not to myListScrol.

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. Swing. Absolute position

I need to set component's location in a window.
I need to draw component on a GlassPane near another component, which was clicked. I pass the component, which raises click event to some manager, there I want to get coordinates where to paint.
public void mouseClicked(MouseEvent e) {
ApplicationManager.getInstance().drawOnGlassPane((Component e.getSource());
}
public void drawOnGlassPane(final Component caller) {
mainFrame = (JFrame) SwingUtilities.getWindowAncestor(caller);
JPanel glassPane = (JPanel) mainFrame.getGlassPane();
glassPane.setVisible(true);
Point where = caller.getLocationOnScreen();
JButton btn = new JButton("on glass pane");
btn.setBounds((int) where.getX(), (int) (where.getY() + caller.getHeight()), 50, 20);
glassPane.add(btn);
}
}
The new component appears in a wrong place. How could I set correct location?
this question is described including example in the tutorial about How to Use Root Panes, another example here and here

Categories